|click to enlarge|
Let us take a look at the example in Arduino's IDE. This is in Digital - BlinkWithoutDelay.
If you have already done the Blink experiment, which is the first experiment that everyone learns in Arduino, you will remember that we blink an LED connected to Arduino's port 13. The method used for this is the delay(); function used in the program that we upload on the Arduino's board.
This is a similar experiment, only difference being the method used to create the pause/delay. We do not use the delay() function here. Instead we use a function called millis(); Which counts the number of milli seconds since the current program started on the board.
We create a constant long integer to store the value of delay that we want to create. We then store the current value of the millis() in an unsigned long integer, and then compare this integer with our constant value. As soon as the millis() exceeds the fixed milli seconds, we change the state of the LED from ON to OFF or OFF to ON. We also store this value of the millis() in another unsigned long integer as the previous value. We can then deduct the previous value from the current value and compare it with the fixed value to get the exact time in each iteration of the program. Turning On or Off the LED with each iteration of the program.
You should be able to learn the program in the IDE. I have created a stripped down version of the same program, so as to help you to understand the program in the bare minimum form.
|click to enlarge|
The connections of this experiment are easy. I have used a small breadboard to connect a LED and a 220 Ohms resistor. you can connect the resistor to either end of the LED. We will connect the negative end of the LED, that is the shorter leg (the Cathode) to Gnd Pin on Arduino. The Positive leg of the LED, the longer leg, the Anode will be connected to pin 13 on Arduino. Notice that pin 13 and Gnd are adjacent pins on Arduino.
Once you connect the Arduino board to the computer and upload the program you can see the LED blinking with a constant blink rate. This is the 1000 mili second that we have assigned as a constant in the program. You can try to change the blink rate by changing this constant.