Trying to make a button dependent loop

Hello,

I'm new to writing but am trying to make a microswitch enabled loop. Basically, I want this loop to only run if the button is being held down. The second it is released, I need it to stop. The code I want is to have an LED illuminate in a certain sequence.

  1. LED is lit up and held on for 2 min
  2. LED is turned off for 1 second
  3. LED is lit on and held for 10 seconds
  4. LED is turned off for 1 sec.

I would like to have steps 3 and 4 repeated for a total of 5 min, then

  1. LED is lit up and held for 5 seconds
  2. LED is turned off for 1 second

I would like to have steps 5 and 6 repeated for a total of 5 min, then

  1. LED will flash at 1 second intervals until the microswitch is finally released.

I'm assuming I would want to use the millis() command rather than using delays so that the microswitch can stop the loop at the exact time it's released but this is confusing the hell out of. Any help would be greatly appreciated.

Thanks

Use the main loop. Poll the switch every time through, and check the millis to see what the LED should be doing.

Yes, if you need the sequence to stop as soon as the button is released, then millis() could be used. But it could also be done without millis(), using delay(), if you are not yet ready to make that step.

If you do not want to use millis(), but you want the sequence to end within a second of the switch being released, you would need to limit your delay() to a maximum of 1 second, and use a loop to achieve longer delays. You would also need to check if the switch is released after every delay(), and abort the sequence if it is. That could be done using the return command. The return command ends loop() early and goes back to the start.

So, for example, to light the led for 10s, off for 1s, for 5 minutes (which is about 27 cycles), you would write:

for (int i = 0; i < 27; i++) {
  digitalWrite(led, HIGH);
  for (int i = 0; i < 10, i++) {
    delay(1000);
    if (digitalRead(button) == LOW) return;
  }
  digitalWrite(led, LOW);
  delay(1000);
  if (digitalRead(button) == LOW) return;
}

PaulRB:
Yes, if you need the sequence to stop as soon as the button is released, then millis() could be used. But it could also be done without millis(), using delay(), if you are not yet ready to make that step.
If you do not want to use millis(), but you want the sequence to end within a second of the switch being released, you would need to limit your delay() to a maximum of 1 second, and use a loop to achieve longer delays. You would also need to check if the switch is released after every delay(), and abort the sequence if it is. That could be done using the return command. The return command ends loop() early and goes back to the start.

So, for example, to light the led for 10s, off for 1s, for 5 minutes (which is about 27 cycles), you would write:

for (int i = 0; i < 27; i++) {

digitalWrite(led, HIGH);
  for (int i = 0; i < 10, i++) {
    delay(1000);
    if (digitalRead(button) == LOW) return;
  }
  digitalWrite(led, LOW);
  delay(1000);
  if (digitalRead(button) == LOW) return;
}

Thanks for the replys

If I use the delay method, would it it continue to light the led if the button is released until the delay is over? Meaning, it wont look for the status change until delay time is finished? I need it to stop the light sequence as soon as as its released.

Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

...R

You may also need to build a "finite state machine" for this, as you're going through different states with different behaviours. This would require multiple timing loops as well.

There'd be a state for no button press, and one for each of the various steps of your sequence.

gufemur:
If I use the delay method, would it it continue to light the led if the button is released until the delay is over? Meaning, it wont look for the status change until delay time is finished?

Yes, but, as I tried to explain, if you limit your delays to 1 second, and check the button after each delay, the sequence will end no more than 1 second after the button is released.