Alternating leds

So... what i want to happen is that when I turn the arduino on, the control box led will light up (ledPin3)

When i press the button, a motor will start running (it is connected straight to the button and not via the arduino) and from that from that same button a signal will go to the button pin.

I want it to be a delay for one second, and then the top led (ledPin1) will start blinking with an interval of one second. I also want it to be a delay for 1,5 seconds and then the bottom led (ledPin2) will start blinking with the same interval. Since the bottom led strarts blinking ahlf a second later, but then blinks with the same interval, it should be alternating lights. (I will adjust the timing later, but just to get it running...)

What i've come up with so far is this but i don´t know if it makes any sense... Can i have two "void loop" commands? Do i need two digitalRead commands? Or can/should i have two digitalread within the same void loop?

Not even sure i use the right terminology...

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin1 =  3;      // the number of the LED pin top barrel
const int ledPin2 =  4;      // the number of the LED pin bottom barrel
const int ledPin3 =  5;      // the number of the LED pin control box

int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
 // initialize the LED pins as an output:
 pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
 // initialize the pushbutton pin as an input:
 pinMode(buttonPin, INPUT);

}

void loop() {
//Turns control box led on
digitalWrite(ledPin3, HIGH);
 // read the state of the pushbutton value:
 buttonState = digitalRead(buttonPin);

 // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
 if (buttonState == HIGH) {
   //wait1 second and then  turn LED on:
delay(1000);                  // waits for a second
   digitalWrite(ledPin1, HIGH);
delay(1000);                  // waits for a second
 digitalWrite(ledPin1, LOW);        // sets the digital pin 13 off
 delay(1000);                  // waits for a second
} else {
   // turn LED off:
   digitalWrite(ledPin1, LOW);
 }

void loop() {
 // read the state of the pushbutton value:
 buttonState = digitalRead(buttonPin);

 // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
 if (buttonState == HIGH) {
   //wait1,5 second and then  turn LED on:
delay(1500);                  // waits for a second
   digitalWrite(ledPin2, HIGH);
delay(1000);                  // waits for a second
 digitalWrite(ledPin2, LOW);        // sets the digital pin 13 off
 delay(1000);                  // waits for a second
} else {
   // turn LED off:
   digitalWrite(ledPin1, LOW);
 }

}

Can i have two "void loop" commands?No.

But you can have code tags.

Oh, i see

So when i use code tags the code will work! Thank you!

RandomCosplaySE:
So when i use code tags the code will work!

No, can’t guarantee that, but people will be more inclined to help.

As usual, removing the delay() calls will make your code more responsive to the buttons. Read up on timing with millis()

Yours is a fairly simple problem with a number of ‘states’ that occur after power on. Use that timer to step between those states.

You can have as many functions as you want but there must be exactly one called loop() because it is built-in to the way Arduino works.

The code you posted looks like the LEDs only blink while the button is held down but your description does not say that. I assume that once you press the button it just runs and runs.

Since nothing happens until the button is pressed you can just hang out in setup() until that happens. Once it does blink LED 1 and mark the time. Wait for half the blink interval, turn on LED 2 and mark the time then move on to loop(). The loop function is really simple, just toggle both LEDs every blink interval.

Below code is tested working - at least as I understand your description, it should be close enough to get you going. (BTW, you can select Tools->Auto Format to fix indentation).

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin1 =  3;      // the number of the LED pin top barrel
const int ledPin2 =  4;      // the number of the LED pin bottom barrel
const int ledPin3 =  5;      // the number of the LED pin control box

const unsigned long blinkInterval = 1000UL; // blink on/off time
unsigned long startBlink1;                  // mark start of blink interval
unsigned long startBlink2;

int buttonState = 0;         // variable for reading the pushbutton status


void setup() {
  // initialize the LED pins as an output:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);

  //Turns control box led on
  digitalWrite(ledPin3, HIGH);

  // wait until pushbutton is pressed. If it is, the buttonState is HIGH:
  while (digitalRead(buttonPin) == LOW);

  // wait after button press
  delay(1000);

  // turn on LED1
  digitalWrite(ledPin1, HIGH);

  // wait an interval
  delay(blinkInterval);

  // turn off LED1
  digitalWrite(ledPin1, LOW);

  // mark start of led1 interval
  startBlink1 = millis();

  // wait 1/2 interval
  delay(blinkInterval / 2);

  // turn on LED2 and mark start of interval
  digitalWrite(ledPin2, HIGH);
  startBlink2 = millis();

}

void loop() {

  // time to toggle led1?
  if ( (millis() - startBlink1) >= blinkInterval )
  {
    //  yes, toggle led and mark interval start
    digitalWrite(ledPin1, !digitalRead(ledPin1));
    startBlink1 = millis();

  }

  // time to toggle led2?
  if ( (millis() - startBlink2) >= blinkInterval )
  {
    //  yes, toggle led and mark interval start
    digitalWrite(ledPin2, !digitalRead(ledPin2));
    startBlink2 = millis();

  }

}

Thank you so much for your anwers!

I missed that part of my descripton. im sorry about that. It is true, the control box led should be on when the arduino is on. The blinking leds should start blinking after one second after the button is pushed and only blink when the putton is held down and then stop when the button is released.

I've changed the code a bit. Would this work?

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin1 =  3;      // the number of the LED pin top barrel
const int ledPin2 =  4;      // the number of the LED pin bottom barrel
const int ledPin3 =  5;      // the number of the LED pin control box

int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
 // initialize the LED pins as an output:
 pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
 // initialize the pushbutton pin as an input:
 pinMode(buttonPin, INPUT);

//Turns control box led on
digitalWrite(ledPin3, HIGH);

}

void loop() {
// read the state of the pushbutton value:
 buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
 if (buttonState == HIGH) {

//wait1 second and then  turn LED 1 on:
delay(1000);               

// turn on LED1
  digitalWrite(ledPin1, HIGH);

// wait an interval
delay(1000);         

// turn off LED1
 digitalWrite(ledPin1, LOW);  

//wait1,5 seconds and then  turn LED 2 on:
delay(1500);               

// turn on LED2
  digitalWrite(ledPin2, HIGH);

// wait an interval
delay(1000);         

// turn off LED2
 digitalWrite(ledPin2, LOW);  

} else {
   // turn LED off:
   digitalWrite(ledPin1, LOW);
   digitalWrite(ledPin2, LOW);
 }
}

RandomCosplaySE: I've changed the code a bit. Would this work?

What happened when you tried it?

The Arduino system is great for learning-by-doing which means this sort of sequence

  • write code
  • test the program
  • modify the code to try to fix errors
  • test again

and then if you can't get it to work ask for help.

A detailed description of what happens when you test the program is a great help for diagnosing problems.

...R

True! I'm not able to test until later tonight, but you are completely right. Will do a test later on!