Saturday, November 16, 2013

An example using one of the Arduino Hardware timers

Circuit Layout for Timer1 Examples


Timer1 Example 1

Timer1 Example 1 (Video)


Timer1 Example 1 (code)

/* This example employs Timer1 to blink one of three LEDs. The timer is initialized and called
during the setup() function which repeatedly calls a function that alternately flashes the
other two LEDs. The timer function continues without interruption when the loop operates.
*/
#include <TimerOne.h>
// This example uses the timer interrupt to blink an LED
// This allows the main program to do "other stuff"
const int blinkLed = 7; // the pin with a LED that should blink
const int buttonLed1 = 5; // led controlled by button
int blinkState = LOW;
int buttonPin = 2;
boolean buttonVal = 1;
void setup(void)
{
pinMode(blinkLed, OUTPUT);
pinMode(buttonLed1, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
Timer1.initialize(150000);
Timer1.attachInterrupt(blinkLED); // blinkLED to run every 0.15 seconds
}
// The main program controls an LED attached to a button
void loop(void){
buttonVal = digitalRead(buttonPin);
if(buttonVal == 0){
digitalWrite(buttonLed1, HIGH);
delay(2000);
}
else{digitalWrite(buttonLed1, LOW);}
}
void blinkLED(void)
{
if (blinkState == LOW) {
blinkState = HIGH;
} else {
blinkState = LOW;
}
digitalWrite(blinkLed, blinkState);
}

Timer1 Example 2 


Timer1 Example 2 (Video)



Timer1 Example 2 (code)

++++++++++++++++++++

Timer1 Example 3

Timer1 Example 3 (video)



Timer1 Example 3 (code)

/* This example employs Timer1 to blink one of three LEDs. The timer is called
within a For Loop which repeatedly calls a function that alternately flashes the
other two LEDs. The timer is "turned on" runs for a specified portion of the For Loop
and is then "turned off".
*/
#include <TimerOne.h>
const int blinkLed = 3; // the pin with a LED that should blink
const int buttonLed1 = 4; // led controlled by button
const int buttonLed2 = 5; // led controlled by button
const int buttonPin = 2; // button
int blinkState = LOW;
boolean buttonVal = 1;
void setup()
{
pinMode(blinkLed, OUTPUT);
pinMode(buttonLed1, OUTPUT);
pinMode(buttonLed2, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
Timer1.initialize(100000); // timer period set to 10,000 us = 100 ms = 0.1 s
}
// The main program controls an LED attached to a button
// The two Timer1 calls show how the interrupt can be paused from a call within the loop
void loop()
{
buttonVal = digitalRead(buttonPin);
if(buttonVal == 0){ // begin if button push has been detected
for(int i=1; i<20; i++){
// set beginning and ending conditions for timer function
if(i == 5){Timer1.attachInterrupt(blinkLED);}
if(i == 15){
Timer1.stop();
digitalWrite(blinkLed,LOW);
}
// flash the other two LED's
flashLEDs();
}
}
digitalWrite(buttonLed2,LOW); // Turn off the LED that is still on at end of For Loop
}
void blinkLED()
{
if (blinkState == LOW) {
blinkState = HIGH;
} else {
blinkState = LOW;
}
digitalWrite(blinkLed, blinkState);
}
void flashLEDs(){
digitalWrite(buttonLed1, HIGH);
digitalWrite(buttonLed2, LOW);
delay(100);
digitalWrite(buttonLed1, LOW);
digitalWrite(buttonLed2, HIGH);
delay(100);
}
++++++++++++++++++++

No comments:

Post a Comment