Hi Folks,
I am trying to implement a timer interrupt and I am having trouble getting started.
I am trying TimerOne and TimerThree from here:
https://www.pjrc.com/teensy/td_libs_TimerOne.html
on a teensy 3.1 .
A question:
When I run the TimerOne interrupt example
even when I change the value set in
Timer1.initialize(150000);
I can't get the blink interval of the LED to change - If I want an interval of say, 1 sec, I change the value to
Timer1.initialize(1000000);
but the LED still blinks fast.
When I look in the serial monitor, I see that blink count gets repeated a number of times before incrementing (Edit: now I understand this is just related to the delay length in the main loop and how that relates to the frequency of the interrupt)
Thanks for all your help, much appreciated!
I am trying to implement a timer interrupt and I am having trouble getting started.
I am trying TimerOne and TimerThree from here:
https://www.pjrc.com/teensy/td_libs_TimerOne.html
on a teensy 3.1 .
A question:
When I run the TimerOne interrupt example
Code:
#include <TimerOne.h>
// This example uses the timer interrupt to blink an LED
// and also demonstrates how to share a variable between
// the interrupt and the main program.
const int led = LED_BUILTIN; // the pin with a LED
void setup(void)
{
pinMode(led, OUTPUT);
Timer1.initialize(150000);
Timer1.attachInterrupt(blinkLED); // blinkLED to run every 0.15 seconds
Serial.begin(9600);
}
// The interrupt will blink the LED, and keep
// track of how many times it has blinked.
int ledState = LOW;
volatile unsigned long blinkCount = 0; // use volatile for shared variables
void blinkLED(void)
{
if (ledState == LOW) {
ledState = HIGH;
blinkCount = blinkCount + 1; // increase when LED turns on
} else {
ledState = LOW;
}
digitalWrite(led, ledState);
}
// The main program will print the blink count
// to the Arduino Serial Monitor
void loop(void)
{
unsigned long blinkCopy; // holds a copy of the blinkCount
// to read a variable which the interrupt code writes, we
// must temporarily disable interrupts, to be sure it will
// not change while we are reading. To minimize the time
// with interrupts off, just quickly make a copy, and then
// use the copy while allowing the interrupt to keep working.
noInterrupts();
blinkCopy = blinkCount;
interrupts();
Serial.print("blinkCount = ");
Serial.println(blinkCopy);
delay(100);
}
even when I change the value set in
Timer1.initialize(150000);
I can't get the blink interval of the LED to change - If I want an interval of say, 1 sec, I change the value to
Timer1.initialize(1000000);
but the LED still blinks fast.
When I look in the serial monitor, I see that blink count gets repeated a number of times before incrementing (Edit: now I understand this is just related to the delay length in the main loop and how that relates to the frequency of the interrupt)
Thanks for all your help, much appreciated!
Last edited: