Two Blinking LEDs at different rates with the Scoop Library

Here is an example of doing two things at once on an Arduino Uno.

The red LED is blinking fast and the yellow LED is blinking at a slower rate. Each blinking operation is a separate task with its own loop() function. Each loop calculates a sine function which is applied to an analog output pin of the LED.

For this simple example, you could easily do this inside the main arduino loop() function. However, the projects get a little bigger, having separate tasks makes things a lot simpler to manage,

Here’s the interesting part of the source. It’s based on a Scoop template created by Fabrice Oudert.

// First Task - Fast Red LED
struct task1 :
SCoopTask<task1> {
static void setup() {}
static void loop() {
  for (int x=0; x<180; x++) {
    float sinVal = (sin(radians(x)));
    int ledVal = int(sinVal*255);
    analogWrite(redLedPin, ledVal);
    sleep(2); // this sleep defines the sine frequency
    }
  sleep(10); // this sleep add a down time between sines
  }
} task1;

The complete source code is in my bitbucket under BlinkingLedsScoopTest.

I will post the Fritzing schematic a little later…

Leave a Reply

Your email address will not be published. Required fields are marked *