I'm fairly new to all of this and hope this is the right place, but I've been trying to design something that would basically simulate a traffic light.
The Green LED is supposed to come on for 20 seconds, then the Yellow LED for 5 seconds, and the Red LED for 30 seconds, and then keep repeating.
There is also a switch that, when active, will make the Yellow LED flash on and off by itself until the switch is turned off and it will go back to the Green -> Yellow -> Red sequence.
I got this to work using delays, but doing that I can only wait until the end of the loop to switch to the blinking yellow light. I want to be able to switch between the two immediately when I press the button.
The code I've been using for this is below.
if you want to keep it simple, check out interrupts. It's like a mailman:
Normally, without interrupts:
You need to check the mail (button pressed) a lot...
With an interrupt:
Doorbell rings. You get mail.
See how useful they can be?
So is Blink_Without_Delay. In the IDE, press File-->Examples-->Digital-->Blink Without Delay
Then clicky on it.
If you want to keep it simple, I recommend that you don't use interrupts unless/until absolutely necessary - they introduce lots of design issues and make your code much more complex. They are certainly not needed here. What you need is a finite state machine to deal with the lamp outputs, and non-blocking code similar to the blink without delay example to handle the timed actions in states where they are relevant. A very simple way to implement a finite state machine which works well in situations like this is to use a variable to hold the current state and a switch / case statement to implement the behaviour relevant to the current state. This is the sort of thing I mean: