#define LED 1//This pin goes to the LED  
 #define SWITCH 7//From Switch$  

void setup() {  
int state=0;//Initializing values of all three  
int val;  
int lastval=LOW;  

void loop(){  
      val=digitalRead(SWITCH);//Reads if switch is on or off  
      if(val==HIGH&&lastval==LOW){//Basically activates if the button was just pressed  
        state=1-state;//Changes state after button is pressed  
        digitalWrite(LED,state);//Enforces changed state to LED on or off  
        delay(50);//Just a simplistic debouncing  
      lastval=val;//Store last value  
  • SO basically, it does turn on and off sometimes, but not always on the button press. It seems a wee bit arbitrary. I wait for a second between presses. Any help at all is apprecaited – Phys_Alpha Aug 15 '17 at 20:35
  • Can you please post that code in a more readable format? I notice that you don't have any code to deal with switch bounce. That's something you should look up. Google "Arduino debounce". I also notice that you're not using the internal pull-up resistors. Do you have pull-up or pull-down resistors on your button? How is it wired? – Delta_G Aug 15 '17 at 20:54
  • Hey Delta_G, thank you for taking the time to respond. I have tried to redo the code on the question, but could not get the indents to work properly, sorry. I have used a delay to deal with the bouncing, in a very rudimentary fashion. I have it wired to a 10kOhm resistor in series with the LED between 1 and GND. Finally I have a wire going to a pushbutton from the 5V supply, and from the button to the Pin 7 (Input pin, or SWITCH in the code). – Phys_Alpha Aug 15 '17 at 21:22
  • 1
    Sounds like your input is floating. Read the Arduino tutorial on buttons. – Majenko Aug 15 '17 at 21:37
  • Thank you Mr. Majenko. I will read up on this and see what I can do about it. Would you have any suggestions about dealing with such an input, as I see several on google and was wondering which would be recommended. – Phys_Alpha Aug 15 '17 at 21:54

Based on your response to my comment, it sounds like you have a floating input. When the button is NOT pressed, the pin isn't connected to ground or 5V so it is floating and picking up whatever stray electrical signals happen to be bouncing around the room.

The easiest way to handle buttons on an Arduino is to wire them between the pin and ground. Then in the code you use INPUT_PULLUP instead of INPUT for the pinMode call. Once you've done that then the pin will reliably read HIGH when the button is NOT pushed and will read LOW when the button IS pushed. Note that this is backwards of how newbies tend to think, but is really the more common way to handle a button.

| improve this answer | |
  • Thank you very much Delta_G, the explanation was really helpful. Can the floating cause stray signals to interfere on the scale of a few volts as well? – Phys_Alpha Aug 15 '17 at 22:18

I think the problem is in the lastval assignment and checking. During the bouncing, lastval will probably change several times. Within these changes the if statement is checked. If an even amount of changes are detected, the LED will change 4 times (being OFF again if it was OFF and On when On). Even if the user clicked possibly only one time. When an uneven amount of changes are detected it will be ok. So it's kind of luck when it works.

Use a proper debouncing technique (checking for some time if the switch is high or low and act on that directly).

Also do not use a boolean to flip that way, if you want it use a proper boolean:

boolean state;


state = !state;

However, I suggest using something like:

digitalWrite(LED, state ? HIGH : LOW); // HIGH if state is true, else LOW

And change the if condition in a proper debouncing check (plenty of examples, e.g. Arduino debouncing switch).

| improve this answer | |
  • In the lastval assignment, I have pushed the assignment of the last val out by 50ms by delaying the running of the rest of the code. Shouldn't that mean that lastval only changes 50ms later? You are probably exactly right, as it does seem to be luck when it works, but I can't grasp why the delay wouldn't avoid it. Also, I will change the state as prescribed. Thanks a bunch Mr. Keijzers. – Phys_Alpha Aug 15 '17 at 21:35
  • You are right ... probably Majenko has the real reason: your input might be floating ... try adding a pullup or pull down resistor (as a test, print the state continuously, remove the delay temporarily and check if the button really shows false or true when you depress and press it). – Michel Keijzers Aug 15 '17 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.