Bad "IF Else" code?

Using a momentary switch, I just want to pulse an LED on for 5 seconds and then off again until the button is pushed again. Nothing is happening when the button is pushed.
What am I missing?

int Runbutton = 0; 

void setup() {
pinMode(52,INPUT);
pinMode(50,OUTPUT);

}

void loop() {
Runbutton = digitalRead(52);



if (Runbutton == HIGH) {
  
  digitalWrite(4,HIGH);
  delay(5000);
  digitalWrite(4,LOW);
}
else {
  digitalWrite(4,LOW);
  
}
}

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you're using the Arduino Web Editor you will not have access to this useful tool but it's still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.

Please remove unnecessary blank lines from your code before posting to the forum. One or two to separate code into logical sections is fine but large spaces for no reason or random blank lines just make for more scrolling when we're trying to read your code.

You are writing to pin 4 but you never set it as an output. You set pin 50 as an output but you're not using it. I recommend defining variables with your pin numbers so that you won't have problems with the pin numbers not being consistent throughout the code.

If that wasn't the cause of the problem then we need to know how you have your circuit wired up.

You need to detect when the button becomes pressed rather than when it is pressed.

Look at the StateChangeDetection example

It would help too if pin 4 was an OUTPUT

(deleted)

I never knew about the Tools>Auto Format…that is awesome! Thank you for that tip!

I corrected my error above in regards to the digitalWrite to the incorrect pin and the program worked as expected. Thanks to all for the assistance!

I’ve since changed the board from MEGA to micro (didn’t have a Micro around at the time) and everything is working…sorta.

  • I have 5V going to the momentary switch (just a simple pushbutton, not a tactile switch) and the other lead going to pin 2.
  • Got a lead going from pin 3 to (POS) leg of LED and a lead going from (NEG) leg to GND.

The problem is that the LED is turning on and off (after 5 seconds) randomly. Sometimes it’s because I touch or bump the wire going to the input. A coworker said something to the effect of using a pull up or pull down wire but I’m not really understanding that.

UKHeliBob:
You need to detect when the button becomes pressed rather than when it is pressed.

Look at the StateChangeDetection example

It would help too if pin 4 was an OUTPUT

What is the StateChangeDetection example? Where would I find it? Is this what’s randomly triggering the circuit?

(deleted)

What is the StateChangeDetection example? Where would I find it?

Google "Arduino State Change Example" if you don't know where to find it. Come on now, you're not helpless.

What is the StateChangeDetection example? Where would I find it?

Have you looked at the examples in the IDE ?

UKHeliBob:
Have you looked at the examples in the IDE ?

Something else I learned about the IDE today! I've never really explored that part of the software before. I'll have to start diving through that stuff as soon as I can.

Here's the new code. It works! (Thanks spycatcher2k!) I would have never thought of inverting the input signal like that. Even condensing the code makes things more sensible to me.

void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(3, OUTPUT);
}
void loop() {
  if (digitalRead(2) == HIGH) {
    digitalWrite(3, LOW);
  }
  else {
    digitalWrite(3, HIGH);
    delay(5000);
    digitalWrite(3, HIGH);
  }
}

Auto format and IDE program examples... Definitely learning a lot from you folks today!

Thank you!

    digitalWrite(3, HIGH);
    delay(5000);
    digitalWrite(3, HIGH);

take pin 3 HIGH
wait 5 seconds
take pin 3 HIGH again ?

UKHeliBob:

    digitalWrite(3, HIGH);

delay(5000);
    digitalWrite(3, HIGH);



take pin 3 HIGH
wait 5 seconds
take pin 3 HIGH again ?

Good point. I didn't see that...odd thing is that the program still worked. I changed it to LOW after the delay...the program still worked as intended with no change. Then I took out that line after the delay completely and the program still functions as intended. A bit confusing but I'll work through it.