Beginners‎ > ‎

led blink without delay

In the previous example (led blink), we have blinked a led diode, now we are going to do the same but using other way. In this example we are going to do the same, but instead of using the delay function (what stops your program), we are going to use millis one, and then the processor could run another task.

WHAT DO YOU NEED




SCHEMATIC



BOARD

SOFTWARE

// test blink with Pinguino

#define PIC18F4550

#define LED 0


void setup()

{
pinMode(LED,OUTPUT);
}

void ledhandle()
{
  if(millis()-lastchange>500) {
     lastchange=millis();
     // now change the value of the led
     // it could be done with the line:
     // digitalWrite(LED,digitalRead(LED)^1);
     // but in order to be clearer:
     if(digitalRead(
LED))
       digitalWrite(
LED,LOW);
     else
       digitalWrite(
LED,HIGH);
  } 
}


void loop()
{
ledhandle();
}

Default processor in Pinguino IDE is 18F2550. If 18F4550 is used, the #define 18F4550 directive must be used.
A pinguino program is divided in a setup function, which is executed once at the beginning of the program. The loop function loops consecutively. This is where your program will test and change state of input and output.
In this example, the setup function defines pin 0 type. The pin 0 drives the led, so it is used as an output. The pinMode instruction is used to define pin type. In this case, pinMode(0,OUTPUT); defines pin 0 as an output.
Now we can wire our led on pin 0. The state of a digital pin can be 0 or LOW  ( 0 Volt ) or 1 or HIGH ( 5 Volt ).
loop() routine is called continiusly, the only thing that this routine make is to call ledhandle() routine.
ledhandle the first thing that do is to evaluate if 500ms have passed from the last time we change the led (millis()-lastchange>500), if that occurs then update the time the last change has happened (lastchange=millis();), and change the value of the led... if previously it was on, put it off, and if it was off, put it on.


DO-IT-YOURSELF

- change pin and modify the program to blink the led on another pin.
- put another led on another pin and blink the 2 leds.
- Make the 2 leds blink asyncronous (for example, one blinking each 500ms, and another 750ms)




Comments