Why won't my code work?!


I am fairly new to basic ARduino programming and have small project in the works. In order to re-familiarize myself to coding for Arduino, I am simply trying to make an LED turn off and on when an SPST switch is flipped. Simple right? Yeah, no. I have included my code below. The problem I have is on start, the LED lights up and stays on! When I hit the switch, it just gets brighter. Funny thing, if I connect the switch to the GND pin, the LED can be controlled- but in and opposite manner. Also, when using GND, the LED may turn off or on, but every now and then, it will turn on or off- depending on what state its' in. FYI, I'm using an Arduino Mega 2560. Thanks for the help!

const int led_1 = 31;       //LED cathode is connected to PIN 31 and anode to GND
const int switch_1 = 44;       //Single SPST switch is connected to PIN 44 and the 5V pin

int switch_1_State = 0;       //Initial "switch_1_State" is LOW

void setup() {
  pinMode(led_1, OUTPUT);       //led_1 is set as an OUTPUT
  pinMode(switch_1, INPUT);       //switch_1 is set as an INPUT

void loop () {
  switch_1_State = digitalRead(switch_1);       //Read state of switch and store in "switch_1_State." Will either be a HIGH or LOW
  digitalWrite(led_1, switch_1_State);       //Tell led_1 to either be HIGH or LOW depending on state of "switch_1_State"

//Problem is LED is always lit. When turning the switch to the "ON" position, the LED just becomes a little bit brighter. When turning to "OFF," LED just dims slightly.
//If switch is connected to GND instead of 5V, LED turns off an on sporatically, opposite to the switch's ON and OFF state. Explain?

dalucero1 year ago
I think you need a pull down resistor for your switch. Study the SCHEMATIC in this example:
He could try digitalwrite (switch1, TRUE) and put a pull up in circuit.
shooternash (author) 1 year ago
Yes to the resistor, the same effect happens just at an altogether dimmer state. I get the exact same results when using the button example- could this be a board problem? When using the example sketch, the LED responds to when I put a leader into the pin socket, it is not even hooked up to GND or 5V.
I think you need to post your circuit.
Got a current limiting resistor on the LED ?????
Um... all you gotta do is use the button example. It works the same with either a switch or a button.

But the problem with your code is you have no comparative statement. Nothing that checks to see if the switch_1_State is high or low and telling the system what to do in either case. You need an if statement in there to check if the button is high or low. Compare your sketch to the Button example sketch and see what your missing.
No, what he's done is to read the switch state and transfer it to the LED state. A comparison statement isn't needed.


Get More Out of Instructables

Already have an Account?


PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!