Blink LED without delay() is not working in my code

Hi folks,

At first: this is the first time I'm programming the arduino.

I'm programming an Arduino MEGA 2560 to control the trackswitches and signals on my modelrailway. To test and play around I connected 2 relais and one LED to the arduino and basically everything is working.

The relais switch but the LED is not blinking.

Buttons one and two start the process. Button 3 resets it.

Every second time, the LED lights. But if I reset the process and start it again, it stays dark. Next time it lights. And so on.

Why isn't it blinking instead?

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin1 = 6;     // the number of the pushbutton1 pin
const int buttonPin2 = 2;     // the number of the pushbutton2 pin
const int buttonPin3 = 14;     // the number of the pushbutton3 pin
const int ledPin =  13;      // the number of the LED pin
const int relaisPin1 =  8;      // the number of the relais1 pin
const int relaisPin2 =  9;      // the number of the relais2 pin
const long interval = 550;           // interval at which to blink (milliseconds)

// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton1 status
int buttonState2 = 0;         // variable for reading the pushbutton2 status
int buttonState3 = 0;         // variable for reading the pushbutton3 status
int ledState = LOW;             // ledState used to set the LED
unsigned long previousMillis = 0;        // will store last time LED was updated

void setup() {
  // initialize the relais1 pin as an output:
  pinMode(relaisPin1, OUTPUT);
  // initialize the relais2 pin as an output:
  pinMode(relaisPin2, OUTPUT);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton1 pin as an input:
  pinMode(buttonPin1, INPUT);
  // initialize the pushbutton2 pin as an input:
  pinMode(buttonPin2, INPUT);
  // initialize the pushbutton3 pin as an input:
  pinMode(buttonPin3, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:

if (buttonState3 == HIGH){
delay(500);    
    // turn LED off:
    digitalWrite(ledPin, LOW); 
delay(1000);
// turn relais off:
    digitalWrite(relaisPin1, HIGH);
delay(1000);
    // turn relais2 off:
    digitalWrite(relaisPin2, HIGH);
 }



 if (buttonState1 == HIGH and buttonState2 == HIGH){
//wait 0,5 seconds
delay(500);  
//switch relais 1
digitalWrite(relaisPin1, LOW);
//wait 2 seconds
delay(2000);
//switch relais 2
digitalWrite(relaisPin2, LOW);    
//wait 2 seconds
delay(2000);     

unsigned long currentMillis = millis();    
if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
    
}
}

Thanks in advance

 //wait 0,5 seconds 
delay(2000);

Comment and code don't agree. Code and thread title don't agree.

AWOL: //wait 0,5 seconds delay(2000);

Comment and code don't agree. Code and thread title don't agree.

Thanks, I corrected the comment. Happened due to copy & paste.

I'm sorry, but what does not agree with the title? :o

T_E_C_H_N_O: I'm sorry, but what does not agree with the title? :o

The "without delay" bit.

AWOL: The "without delay" bit.

But I used the code from the tutorial "Blink without delay()" in my code and it is not working. So why is it wrong in the title? I'm sorry but I dont understand :(

T_E_C_H_N_O: But I used the code from the tutorial "Blink without delay()" in my code and it is not working. So why is it wrong in the title? I'm sorry but I dont understand :(

Your code is full of delays. Only your blink part is without delays.

Blink without delays is a way of thinking which you haven't understood, at least not yet.

Back to your code, what happens if you keep button 1&2 pressed without releasing them?

Gabriel_swe: Your code is full of delays. Only your blink part is without delays.

Blink without delays is a way of thinking which you haven't understood, at least not yet.

Back to your code, what happens if you keep button 1&2 pressed without releasing them?

Yeah I know about that delays and they are needey to have a delay between the switching of the relais (for the track switches) and the signals, to make it more realistic.

If I hold the buttons without releasing them, it does not change anything about the behavior of the LED.

Thanks for your help! :)

Delays are not needed. Blink without delay is a proof of concept. Of course a one liner like delay is quick to write. In autonomous programs I can see it as ok, but when user input is involved it will soon be annoying when nothing is happening because code is stuck waiting for delays to expire before processing your command.

I am on a small screen now and it gives no good overview of the code and hard to see matching brackets, but I thought something would be visible about the led if button 1&2 was pushed for 20 seconds or so.

First, give your buttons and relays more meaningful names. It makes it more obvious when working with the code what each part does.

Blink-without-delay means removing ALL THE DELAYS. The main loop executes thousands of times per second, every time it checks if it's time to do something new. For the tasks you're working on, that means you need to switch to a state machine. The button 3 state machine could look something like this...

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  //Then do the following sequence of actions...
  static int Button3Sequence = 0;
  static unsigned long Button3StartTime;
  if (Button3Sequence == 0 && buttonState3 == HIGH) {
    Button3Sequence = 1;
    Button3StartTime = millis();
  }
  if (Button3Sequence == 1 && millis()-Button3StartTime > 500) {
    // turn LED off:
    digitalWrite(ledPin, LOW);
    Button3Sequence = 2;
    Button3StartTime = millis();
  }
  if (Button3Sequence == 2 && millis()-Button3StartTime > 1000) {
    // turn relais off:
    digitalWrite(relaisPin1, HIGH);
    Button3Sequence = 3;
    Button3StartTime = millis();
  }
  if (Button3Sequence == 3 && millis()-Button3StartTime > 1000) {
    // turn relais2 off:
    digitalWrite(relaisPin2, HIGH);
    Button3Sequence = 0; //go back to the start and wait for another button press
  }

But you can make state machines much more complex than that. You might decide that pushing button 2 in the first 500ms while the LED is still on will cancel this action and go back to state 0. You might make it skip the delays and execute 'instantly' if button 3 is still held down when it gets to state 2. There's lots of choices.

Just to expand on MorganS comment…

Think of it as ‘Switch relays without delay()

Relay pins are no different than LED pins.
You need to apply the same understanding & timing logic to both.

Yeah I know about that delays and they are needed ...

I see. "I know sugar is bad for you but this recipe calls for sugar."

Make up your mind.

http://www.gammon.com.au/blink

To sum it up: I'm going to recreate the code. I didn't know/understand that the delay command stops the complete code.

As I said in my first post: I'm new to this! And instead of useless comments, I hoped to get real help.

Anyway: thanks Morgan and Nick, the link opened my eyes.

And instead of useless comments, I hoped to get real help.

Very good way of getting more help from people.

If you don't like a comment ignore it, move on.

.