Blink two LEDs with two push buttons

[text]Hello, im very new to this so im sorry if the solution is simple. I want to be able to press a button1 and LEDs 1 to flash, press button 2 and LEDs 2 to flash. If I press both buttons I want the LEDs to alternate flashing. I also want the servo to be continuously running. I have it working slightly but with issues due to using the delay(), i am not sure how to incorporate millis() into this while doing everything I need. Any help or guidance with the code would be appreciated, I have been trying to do this by myself for some time. 


[#include <Servo.h>
Servo myServo;

int delayValue = 100;
int ledPin1=9;
int ledPin2=8;
int buttonPin1= 12;
int buttonPin2= 11;
int pos=0;

void setup() { 
  
  pinMode(ledPin1, OUTPUT); 
  pinMode(buttonPin1, INPUT);
  pinMode(ledPin2, OUTPUT); 
  pinMode(buttonPin2, INPUT);  

  myServo.attach(4);
  
}
void loop() { 
//LED
  int buttonState1 = digitalRead(buttonPin1);
  if (buttonState1 == HIGH) { 
    digitalWrite(ledPin1, HIGH);
    delay(delayValue);
    digitalWrite(ledPin1, LOW);
    delay(delayValue);
  } else { 
    digitalWrite(ledPin1, HIGH);
  } 
  int buttonState2 = digitalRead(buttonPin2);
  if (buttonState2 == HIGH) { 
    digitalWrite(ledPin2, HIGH);
    delay(delayValue);
    digitalWrite(ledPin2, LOW);
    delay(delayValue);
  } else { 
    digitalWrite(ledPin2, HIGH);
  } 
  //Servo Sweep
  for (pos = 0; pos <= 180; pos += 1) {
    myServo.write(pos);              
    delay(20);                      
  }
  for (pos = 180; pos >= 0; pos -= 1) { 
    myServo.write(pos);              
    delay(20);       
}
}

You want to remove your delay() calls. Look at the Blink without Delay example and the State Change Detection example in the IDE (File->Examples->02.Digital->....) to see how you can do it.

You can also check out the Several Things at a Time

Here is your problem: an Arduino can only do one thing at a time. They way you have coded it up now, any task (eg blink led1) wil completely occupy the Arduino and other tasks (the other led, the servo) will not work.

Indeed, you want to get rid of any delay calls in your program. The Several Things at a Time thread posted here is a very good place to start.

Honestly there a multiple ways to do this, they are all fine. You might want to use interrupt calls on the buttons, and use some sort of timer to blink the leds.

TimMJN:
You might want to use interrupt calls on the buttons, and use some sort of timer to blink the leds.

You might, but it would be a very silly idea!

Paul__B:
You might, but it would be a very silly idea!

Depending on how important and smooth the servo movement has to be, it might not even be that silly IMO. Any reason why you find it silly?

Because the problem with buttons is the need to de-bounce them.

If you use interrupts, that means you generate a new interrupt on every bounce of the contact, so you have to decipher a fusillade of interrupts.

Detection of a stable button press occurs a set time - about 5 milliseconds is usual - after the button no longer bounces, so the problem is to detect that interval after the last interrupt. Now how does that make sense?

My implementation of button debouncing is to poll the button on every pass through the loop. Implicitly, the loop must be constructed so that nothing delays it - as has been explained above, you cannot use "delay()" or "while". On each pass (numerous per millisecond) you determine whether the button is still in the alternate to its "previous" state and see how many millis() have passed since it made that change; when the specified time has passed while it did not revert to the previous state, then you register the change as genuine.

This procedure as a "state machine" takes negligible time in the loop() so many other similarly efficient actions may be concurrently performed, thus the "Several Things at a Time" topic - with no recourse to interrupts. :grinning:

Paul__B:
Because the problem with buttons is the need to de-bounce them.

Of course I agree that using a state approach is more convenient, when the system allows for it. I have no idea what OP is actually trying to achieve, so I cannot comment on that. I was merely trying to highlight that there are multiple ways out there to tackle this kind of problem. ¯_(ツ)_/¯

Also, I always have had pretty good results using hardware debouncing.