Thursday, February 25, 2010

Blink (First Arduino Program) (Tasks 7,8,9,10)

Here is my version of blink:
void setup() {
pinMode(13,OUTPUT);
}

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

It works off pin 13. Turns the LED on, waits ~500ms and turns the LED off, waits ~500ms again and then restarts the loop.

An easy way to vary this program would be to increase or decrease the speed of the blink. An example of code that would cause a long delay between short blinks would look like this

digitalWrite(13, 1);
delay(250);
digitalWrite(13, 0);
delay(1000);

This would turn the LED on for ~250ms and then turn it off for ~1 second. You could also do the opposite with this code

digitalWrite(13, 1);
delay(1000);
digitalWrite(13, 0);
delay(250);

By changing both the delays to 10ms, the LED appears to constantly stay on and not blink, at 20ms it appears to “flicker” and at 40ms it begins to flash.

No comments:

Post a Comment