Hardware 9
Hardware
1.6: Using delay
We want our microcontroller to blink, but right now it just stays on. We have to tell the microcontroller to
turn on and o. But, we have to leave some time in between so that we can actually see the blinking.
One way to do this is to use the “delay” function. Delay means to wait, or to stop for a time. You use it like
this:
delay(500);
The arguments for delay are the time in milliseconds (1/1000th of a second) that you want to wait for. I’m
telling it to wait 500 milliseconds, or half of a second.
In our program we want to turn on the LED, then wait, then turn it o, then wait. This means our program
will look like this:
voidsetup(){
//putyoursetupcodehere,torunonce:
pinMode(13,OUTPUT);
//setpin13tooutput,pin13isthebuilt-in
led
}
voidloop(){
//putyourmaincodehere,torunrepeatedly:
digitalWrite(13,HIGH);//turnontheled
delay(500);//wait
digitalWrite(13,LOW);//turnotheled
delay(500);//wait
}
Now we can try verifying again. If there are errors try putting slashes before a line, making it a comment,
and verifying again. If the error goes away, you know it was in the line you commented out, otherwise you
can look at other lines.
We can upload again. If everything is correct, you should see your microcontroller’s LED blinking once a
second. (Refer to Lesson 1 Figure 3)
Try changing things around in the program, such as the blinking time. See what works, and what doesn’t.
(You can also look at the program under le->examples->01.Basics->Blink.