Sunday, February 28, 2010

Alternating Blinking (Tasks 11, 12, 13, 14, 16)

Basic program that will alternate between two LEDs flashing with no delay between On/Off

void setup() {
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
}

void loop()
{
digitalWrite(13, 1);
digitalWrite(12, 0);
delay(500);
digitalWrite(13, 0);
digitalWrite(12, 1);
delay(500);
}

You can alter the flashing pattern by changing the delay. For example:

void loop()
{
digitalWrite(13, 1);
digitalWrite(12, 0);
delay(250);
digitalWrite(13, 0);
digitalWrite(12, 1);
delay(1000);
}

This code will cause the LED on pin 13 to flash for a short period, then the LED on pin 12 to flash for a longer period of time. You could also cause the opposite by swapping the delay values.

This code is similar to the first program except there is a delay between each LED being turned on:
void loop()
{
digitalWrite(13, 1);
delay(500);
digitalWrite(12, 0);
delay(500);
digitalWrite(13, 0);
delay(500);
digitalWrite(12, 1);
delay(500);
}

This is yet another flashing pattern where one led flashes twice then the other once then repeat:
{
digitalWrite(13, 1);
delay(500);
digitalWrite(13, 0);
delay(500);
digitalWrite(13, 1);
delay(500);
digitalWrite(13, 0);
delay(500);
digitalWrite(12, 1);
delay(500);
digitalWrite(13, 0);
delay(500);
}

No comments:

Post a Comment