Monday, December 15, 2014

LED blink without delay, arduino timer interrupt

Sometimes we need to do two things at once. For example we might want to blink an LED (or some other time-sensitive function) while reading a button press or other input. In this case, we can't use delay(), or you'd stop everything else the program while the LED blinked. The program might miss the button press if it happens during the delay(). This sketch demonstrates how to blink the LED without using delay(). It keeps track of the last time the Arduino turned the LED on or off. Then, each time through loop(), it checks if a long enough interval has passed. If it has, it toggles the LED on or off. 
Arduino blink without delay circuit:
Arduino blink without delay circuit code
We can make this LED blink without delay using tow different codes: 
Arduino blink without delay code1:
This code below uses the millis() function, a command that returns the number of milliseconds since the Arduino board started running its current program, to blink an LED.
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(13, OUTPUT);      
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;  
    // toggle the state of pin 13:
    digitalWrite(13, !digitalRead(13));
  }
}


Arduino blink without delay code2:
Resetting the millis() function is not easy and this function stay running and until it reaches the limit value of a 4 byte unsigned number (about 49 continuous days)  then it returns to 0 where our function can not affect anymore 
This code uses timer1 interrupt every 1000 ms to make our led blink.
  int timer1_value;
   void setup()
  {
  pinMode(13, OUTPUT);

  // initialize timer1 
  noInterrupts();           // disable all interrupts
  TCCR1A = 0;
  TCCR1B = 0;

  // Set timer1_value to the correct value for our interrupt interval
  timer1_value = 3036;   // preload timer 65536-16MHz/(256*1Hz)
  
  TCNT1 = timer1_value;   // preload timer
  TCCR1B |= (1 << CS12);    // 256 prescaler 
  TIMSK1 |= (1 << TOIE1);   // enable timer overflow interrupt
  interrupts();             // enable all interrupts
 }

 ISR(TIMER1_OVF_vect)        // interrupt service routine 
 {
  TCNT1 = timer1_value;   // preload timer
  digitalWrite(13, !digitalRead(13));
 }

  void loop()
 {
  // write your program...
 }