Hi
this is a nice little project with lots of learning possibilities.
There are several things wrong with your code, but instead of correcting them for you, i would suggest a couple of things.
The first thing would that you take a look at the "BlinkWithoutDelay" Example on your IDE or take a look at this page:
The thing here is that the delay() function (which you are using several times) works like a "pause" on your sketch. this means that while one of the delays is going on, nothing else can happen (no button reads, no turning leds on or off,...).
The BlinkWithoutDelay, like the name tells, explains a nice technic to achieve a delay effect without using the delay() function. Very useful! ![]()
Even though you are using only 2 leds and 2 buttons (i am sorry if i am wrong, bu i am assuming that you are doing it to learn something...), i would also encourage you to read about Arrays.
Arrays are like lists of things. For instance, when you are making a shopping list you don't take a paper and write "butter", another paper to write "milk" and another paper to write "eggs". You take ONE paper (your list) where you will write all the elements you want.
The same could happen with your LEDs. instead of having them separate, you could create an array (a "list") and put all your leds inside. maybe another array for the buttons,...
Here is a nice page about this topic:
Another idea.
What you are doing now with your code is something like if the button is pressed "bla bla bla".
But do you actually mean the "bla bla bla" to happen all the time when the button is pressed (what would happen if the button is pressed for 5 minutes?) or do you want it to happen when the button "becomes" pressed (the moment when it went from "not being pressed" to "being pressed")?
For this there is something called State Change Detection. It's not complicated but it is very usefull.
You can learn about it here:
another thing i would advise you to try, would be to write a separate function for the "blinking of the leds".
Like this, when you want an LED to blink, instead of having to write the code for it, you just need to "call" the blink function (which will have all the blinking information/code) for that specific LED.
if would maybe look something like this:
void setup() {
// your setup here
}
void loop() {
// all your loop code here
// when a button is pressed:
blink (ledx); // this will run the function "blink" to the "ledx" LED
}
//function to blink the leds
void blink (ledToBlink) {
// your blink code here
digitalWrite(ledToBlink,HIGH)
//and so on. Try to use the Blink Without Delay ideas... ;)
}
Hmmm, i realize this is a lot of info just to make two leds blink, but i guess you will learn some cool basic stuff which you then allow you to program more complicated things!
![]()
Hope it helps you!
![]()