Go Down

Topic: How can i run two sequences (IF's) at the same time? As oppose to one at a time? (Read 495 times) previous topic - next topic


Hi all,

Please forgive me for any stupid comments or questions, i got an Arduino UNO last night, some LED's resistors and switches, and have been playing around with it and watching lots of videos on youtube, so am very much a newbie first timer.

Ive got 3 amber leds, followed by a green led, lighting up and going out after a 1 second delay in sequence, ran by a switch that sets off the sequence. So it goes, amber1 on, 1 second delay,  amber 1 off amber 2 on, one second delay amber2 off amber 3 on, one second delay, amber 3 off green on. Then ledred comes on and flashes 4 or 5 times.

Ive also got another seperate redled2, which i have managed to get a seperate switch controlling turning it on/off.

The problem i have is, i cant turn the redled2 on whilst the first sequence is running. Well i can, but until the sequence has finished the led doesnt light up until it has.

So basically, what i want to do is have the ability to turn on redled2 at my leisure whether the other sequence is running or not.
Im pretty certain its something to do with my code, and my IF statement placing, but could anyone be kind enough to have a look for me please?

I am able to turn the redled2 lights on BEFORE i press the button to start the sequence, and it stays on. But if i turn it off mid sequence, it doesnt turn off until the sequence has finished.

I feel i am nearly there and have been moving my IF's my }'s and trying else if's but no luck.

Here is my code.

  Turns on an LED on for one second, then off for one second, repeatedly.

  This example code is in the public domain.

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
const int button1pin = 2;
const int button2pin = 7;
const int ledgreen = 12;
const int ledamber3 = 11;
const int ledamber2 = 10;
const int ledamber1 = 9;
const int ledred = 8;
const int ledred2 = 6;

int buttonState = 0;
int buttonState2 = 0;

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
pinMode(ledgreen, OUTPUT);
pinMode(ledamber3, OUTPUT);
pinMode(ledamber2, OUTPUT);
pinMode(ledamber1, OUTPUT);
pinMode(ledred, OUTPUT);
pinMode(ledred2, OUTPUT);
pinMode(button1pin, INPUT);
pinMode(button2pin, INPUT);


// the loop routine runs over and over again forever:
void loop() {
    // read the state of the pushbutton value:
  buttonState = digitalRead(button1pin);
  buttonState2 = digitalRead(button2pin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
  digitalWrite(ledamber1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(ledamber1, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(ledamber2, HIGH);
  digitalWrite(ledamber2, LOW);
  digitalWrite(ledamber3, HIGH);
  delay(1000);               // wait for a second
  digitalWrite(ledamber3, LOW);
  digitalWrite(ledgreen, HIGH);
  digitalWrite(ledgreen, LOW);
  digitalWrite(ledred, HIGH);
  digitalWrite(ledred, LOW);
  digitalWrite(ledred, HIGH);
  digitalWrite(ledred, LOW);
  digitalWrite(ledred, HIGH);
  digitalWrite(ledred, LOW);

  else if (buttonState2 == HIGH) {
  digitalWrite(ledred2, HIGH);}


  digitalWrite(ledamber1, LOW);
  digitalWrite(ledamber2, LOW);
  digitalWrite(ledamber3, LOW);
  digitalWrite(ledgreen, LOW);
  digitalWrite(ledred, LOW);
  digitalWrite(ledred2, LOW);





I wrote an extended demo of the concept in the Blink Without Delay example sketch in the first post in this Thread. It illustrates how to manage timing for several things at the same time.

Two or three hours spent thinking and reading documentation solves most programming problems.


Thanks guys.

Robin, im going to try that again, felt as though i followed it word for word, but didn't work as it should for me. Im obviously doing something wrong. Going to try and have another crack later today.

The 3 LED's were lighting up randomly as they should, but the other which is activated by the button on pin 7, was blinking when i pressed the switch, then when i pressed the switch again to turn it off, it stayed on but stopped blinking.

Need to give it a rest for a bit before i try again, thanks for the help :)

Go Up