Blinking leds!!!!

Hi all

I would like to ask a question regarding a blinking led sketch i am trying. I am new to the arduino uno and the C language. I am trying to expand a little on the blinking led sketch from the Arduino web site.

OK

What I am trying to do is get 2 leds to work independently of each other. For eg, green flash 300 milli on/off and the red to flash 1 sec on/off without interfering with each other.

My sketch so far is:

int pin12 = 12; // green led
int pin11 = 11; // red led
int timer = 50;

void setup() {

pinMode(pin12, OUTPUT); // green
pinMode(pin11, OUTPUT); // red
}

void loop() {
digitalWrite(pin12, HIGH);
delay(timer);
digitalWrite(pin12, LOW);
delay(timer);

digitalWrite(pin11, HIGH);
delay(50);
digitalWrite(pin11, LOW);
delay(50);
}

this sketch will flash the leds but if I change the speeds in milliseconds then it alters both.

Any ideas where I should go with this please.

Thanks

Don

See http://bit.ly/16HvsXv :smiley:

Thank you very much. works a treat and now just need to learn from the code.

Nothing to do with your original question, but assigning the value of 12 to a variable called pin12 does not help much with reading your code. You may as well not bother. If, however, you were to name the variables (or even better make them consts) green and red it would be easier to understand exactly what was going on later.

Donmerrick:
Thank you very much. works a treat and now just need to learn from the code.

You're very welcome! Let us know if you have questions on the code. Some suggestions:

  1. Study the BlinkWithoutDelay example sketch first which blinks a single LED. Once that is well understood, move on to two LEDs. The main aim is to avoid calling the delay() function altogether.
  2. When composing a post, use the # button to put code tags around code to make it appear as below; it's much easier to read that way.
  3. While I strongly recommend understanding BlinkWithoutDelay first, also be aware that there are libraries that can simplify these sorts of tasks. The example below uses one of them. I think there are more in the playground.
#include <Timer.h>        //http://github.com/JChristensen/Timer

const int GRN_LED = 12;
const int RED_LED = 11;
const unsigned long GRN_PERIOD = 300;
const unsigned long RED_PERIOD = 1000;
Timer t;

void setup()
{
    pinMode(GRN_LED, OUTPUT);
    pinMode(RED_LED, OUTPUT);
    t.oscillate(GRN_LED, GRN_PERIOD, HIGH);
    t.oscillate(RED_LED, RED_PERIOD, HIGH);
}

void loop()
{
    t.update();
}

Thank you Helibob, I will try to study it.