shouldnt both LEDs go off after delay?

Hi,

In this statement below, shouldnt both LED go off after the delay?

1 of them is remaing HIGH.

Why?

  if ((buttonState3 == HIGH) && (buttonState7 == HIGH) && (buttonState8 == LOW))

  {
    digitalWrite(ledPinUp, HIGH);
    digitalWrite(ledPinMiddle, HIGH);
    delay (10000);
  }
  else if ((buttonState3 == HIGH) && (buttonState7 == LOW) && (buttonState8 == HIGH)) {
    digitalWrite(ledPinDown, HIGH);
    digitalWrite(ledPinMiddle, HIGH);
    delay (10000);

Neither light should go off after the delay with the code you've posted, because nowhere in that code is there any digitalWrite(whatever,LOW). Did you mean to post more code? It looks like there's more after that, including stuff that would happen after delay.

this is the full sketch

// set pin numbers:
const int buttonUp = 2;       // the number of the pushbutton pin for UP
const int buttonMiddle = 3;   // the number of pushbotton pin for middle
const int buttonDown = 4;     // the number of the pushbutton pin for Down
const int ledPinUp = 12;      // the led output for UP Position
const int ledPinMiddle = 13;  // the led output for MIDDLE Position
const int ledPinDown = 11;    // the led output for DOWN Position
const int limitup = 7;
const int limitdown = 8;

// inputs that will change:
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState7 = 0;
int buttonState8 = 0;
void setup() {
  Serial.begin(9600);
  pinMode(ledPinMiddle, OUTPUT),     //output for MIDDLE LED
  pinMode(ledPinUp, OUTPUT);        //output for led UP
  pinMode(ledPinDown, OUTPUT);      //output for led DOWN
  pinMode(buttonUp, INPUT);         //trigger for led UP
  pinMode(buttonDown, INPUT);        //trigger for led Down
  pinMode(limitup, INPUT_PULLUP);       //signal switch for buttonmiddle
  pinMode(limitdown, INPUT_PULLUP);      //signal limit switch for buttonmiddle
}

void loop() {
  // read the state of the pushbutton value:
  buttonState2 = digitalRead(buttonUp);
  buttonState3 = digitalRead(buttonMiddle);
  buttonState4 = digitalRead(buttonDown);
  buttonState7 = digitalRead(limitup);
  buttonState8 = digitalRead(limitdown);

  if (buttonState2 == HIGH) {
    digitalWrite(ledPinUp, HIGH);
    delay (5000);

  }
  else {
    digitalWrite(ledPinUp, LOW);
  }


  if ((buttonState3 == HIGH) && (buttonState7 == HIGH) && (buttonState8 == LOW))

  {
    digitalWrite(ledPinUp, HIGH); 
    digitalWrite(ledPinMiddle, HIGH);
    
    
    delay (5000);
  }
  else if ((buttonState3 == HIGH) && (buttonState7 == LOW) && (buttonState8 == HIGH)) {
    digitalWrite(ledPinDown, HIGH);
    digitalWrite(ledPinMiddle, HIGH);
    delay (5000);
  }

  if (buttonState4 == HIGH) {
    digitalWrite(ledPinDown, HIGH);
    delay (5000);
  }
  else {
    digitalWrite(ledPinDown, LOW);
  }
}

when Pressing the UP and Down, I had an "else LOW" command.. But when I press the MiddleButton, I never put an Else code command, in that part..

Thank You. I got that!.. I just have to start learning how arduino thinks.. :slight_smile:

I had to add an Else digitalwrite (middlepin LOW)

Looks like you got sorted on the issue you asked about.

You may be abusing delay - maybe this is what you want, but I doubt it.

You're checking the buttons once, then going through those if's...

If button2 is high, it'll turn on UP LED, then wait 5 seconds, otherwise it will turn it off and wait 0.

Then, if those three button states that got recorded (possibly recorded 5 seconds ago now) match the second set of conditions, it will turn on Up and Middle LEDs, and pause for 5 seconds.

And so on. By the time you get to the bottom, that test of button 4 could be looking at the state of the button you recorded 10 seconds ago.

The consequences of the delays may be contributing to confusion - for example, say you press button 2, then try to press button 4. It won't check for button 4 until it's through that loop.

There's an example of the alternative when that's not acceptable in blink-without-delay, and a lot of information here on "doing multiple things at once", since it's a very common thing to be confused by when starting out with Arduino.

hi,

yes, I know that delay, litterally halts the processor, until the delay time pass.

However in this particular case, this is what I want. I do not want that anything happens, if someone presses another button, before the time delay has elapsed.

I would like to write a state machine, but cant find my way there yet!! been looking at other sketches to get an idea, but it seems that sometimes, it gets more confusing. Oh well, practice makes perfect I guess.. long way to tipperrary..hehe

intertronic:
hi,

yes, I know that delay, litterally halts the processor, until the delay time pass.

You don't know. In fact, delay() is a function that uses millis() in a do nothing loop. Unless someone has changed the implementation.

hmm.. Did not understand this. What do you mean by uses millis in a do nothing loop?

intertronic:
hmm.. Did not understand this. What do you mean by uses millis in a do nothing loop?

void delay(unsigned long ms)
{
  unsigned long start = millis();
	
  while (millis() - start <= ms)
    ;
}

would like to write a state machine, but cant find my way there yet!!

It will be worth learning how to use a state machine. Write down each state that the system can be in and assign each a number. For each case write down what has to happen to change the state to another one and the state number to move to. For instance, you may be in state 3 waiting for a button press or a period of time to elapse. The button press might move to state 4 whilst the period elapsing might move to state 0.

With that done you can set about writing the program. I find it easier to use switch/case than a whole lot of ifs. None of the code you write should hold up execution of the program so no use of delay(). Each time round loop() only the code for the current state will be executed because of the switch/case based on the current state.

For timing events, each time the case is entered as you go round loop() again you check whether the waiting period has elapsed and if so, change the state number to that of the target state and set up any entry conditions for the new state such as the start time for a new wait, turn on an LED etc. Next time through loop() the code for the new state will be executed and you go on like that moving from state to state based on events such as inputs or time passing.