Skip to content

Project 30

This project demonstrates how to use interrupt driven code to deal with the problem of controlling the timing of events by using delays – where you get stuck “waiting” in a delay, and are unable to do anything else while you wait.

This demo uses an external library, the MsTimer2 library, which you will need to install.  See the Arduino Reference section under libraries for more info.

This code will blink three LEDS at different tempos simultaneously by using an “interrupt” function called by the MsTimer2 library, which decrements event counters which provide “alarm clocks” which “go off” when they hit 0.  The code then waits for the clock to hit 0, do what it wants to do, then resets the counter with the number of Milliseconds before the next event linked to that counter.

You can use as many counters as you need for discreet events with this system.  Add a counter that check a pushbutton switch every 10ms, and have it turn/off one of the flashing LEDS.  Then try adding another counter which checks a potentiometer also each 10 ms, and varies the rate of one of the LEDs based on this.

You can download the demo file here:

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: