Two LEDs
In class it was asked how to go about blinking two LEDs on two different digital pins but at different speeds. What has to be done is that the time has to be broken down into segments. So for our example, the green led was to come on for 1 second and stay off for 1 second while the red LED fashes on and off 10 times in 1 second. This meant we had to turn on the red LED for 50ms and off for 50ms and do this 10 times. (50ms+50ms=100ms and 100ms x 10=1000ms or 1 second.) Instead of just turning on the green LED and delaying for 1 second, we run a for loop to flash the red LED for 1 second instead and then turn off the green LED, flash the red 10 times again and then start the loop all over again. Clear as mud, huh?
/* This example was a way to blink one LED slowly
while a second blinks quickly. Its important to think
about the timing in a top down program.
*/
int redLED = 13; // LED on digital pin 13
int greenLED = 12; // LED on digital pin 12
void setup() // run once
{
pinMode(redLED, OUTPUT); // sets pin 13 as output
pinMode(greenLED, OUTPUT); // sets pin 12 as output
}
void loop() // run over and over again
{
digitalWrite(greenLED, HIGH); // turns the green on
for(int i=0; i<10; i++) // blinks red 10 times
{
digitalWrite(redLED, HIGH); // turns the red on
delay(50);
digitalWrite(redLED, LOW); // turns the red off
delay(50);
}
digitalWrite(greenLED, LOW); // turns the green off
for(int i=0; i<10; i++) // blinks red 10 times
{
digitalWrite(redLED, HIGH); // turns the red on
delay(50);
digitalWrite(redLED, LOW); // turns the red off
delay(50);
}
}
Comments (0)
You don't have permission to comment on this page.