Teensy 3.0 IntervalTimer interrupts

Status
Not open for further replies.

GeoffT

New member
Since re-install of Arduino 1.05 and Teensyduino 1.14, I am unable to service interrupts by IntervalTimer on Teensy 3.0.

Here is a stripped down sketch I think should work. Could someone with a Teensy 3.0 please run it and reply if the LED goes OFF after 1 second?

void smpltimerisr(void);
const int ledPin = 13; // Teensy3 has LED on 13
volatile unsigned long smplidx = 0;

void setup(){
pinMode(ledPin, OUTPUT);
IntervalTimer smpltimer;
smplidx = 0;
smpltimer.begin(smpltimerisr, 1000000);
}
void loop(){
if(smplidx == 0)
digitalWrite(ledPin, HIGH);
else
digitalWrite(ledPin, LOW);
}

void smpltimerisr(void)
{
smplidx++;
}

//The above compiles fine, loads and executes, but the LED stays ON, indicating no interrupts active; any ideas?
 
You must declare smpltimer as global. When you declare it as local to setup() it will disappear as soon as you exit from the setup function.

Pete
 
Yes, Pete is absolutely right.

This used to work, sometimes, but only if the memory previously used by the timer object wasn't overwritten after it no longer exists (but the bytes are still physically present in the RAM, available to be overwritten by other functions). In most cases, using other functions would overwrite that memory, causing continued usage of the timer to crash your program. 1.14 fixed that bug by properly ending the timer when the object goes out of scope. The result is some incorrect programs that used to work because nothing happened to overwrite the free memory now do not continue using the timer.

Just make the timer a global object, or create it on the heap with "new", so it continues to exist after your setup() function ends.
 
Thanks for the explanation. I'll try the fix as soon as I get home. I also earlier and mistakenly posted this in Project Guidance.
Geoff
 
void smpltimerisr(void);
const int ledPin = 13; // Teensy3 has LED on 13
volatile unsigned long smplidx = 0;
IntervalTimer smpltimer;
void setup(){
pinMode(ledPin, OUTPUT);

smplidx = 0;
smpltimer.begin(smpltimerisr, 1000000);
}
void loop(){
if(smplidx == 0)
digitalWrite(ledPin, HIGH);
else
digitalWrite(ledPin, LOW);
}

void smpltimerisr(void)
{
smplidx++;
if(smplidx==1000000){
smplidx=0;}
}

Ok what am I missing with using interrupts if I wanted to have the LED toggle every 10 secs?

Without using another interrupt?
 
void smpltimerisr(void);
const int ledPin = 13; // Teensy3 has LED on 13
volatile unsigned long smplidx = 0;
IntervalTimer smpltimer;
void setup(){
pinMode(ledPin, OUTPUT);

smplidx = 0;
smpltimer.begin(smpltimerisr, 1000000);
}
void loop(){
if(smplidx == 0)
digitalWrite(ledPin, HIGH);
else
digitalWrite(ledPin, LOW);
}

void smpltimerisr(void)
{
smplidx++;
if(smplidx==1000000){
smplidx=0;}
}

Ok what am I missing with using interrupts if I wanted to have the LED toggle every 10 secs?

Without using another interrupt?

You are only turning the LED on at exactly smplidx = 0. You should change:

if(smplidx == 0)

to:

if(smplidx >= 500000)

Also, I think you want another decade to get 10 secs instead of 1 sec.
 
Negative ghost rider.... (as far as I can tell.....)

CODE:
Code:
void smpltimerisr(void);
const int ledPin = 13; // Teensy3 has LED on 13
volatile unsigned long smplidx = 0;
IntervalTimer smpltimer;
void setup(){
pinMode(ledPin, OUTPUT);

smplidx = 0;
smpltimer.begin(smpltimerisr, 1000000);
}
void loop(){
//if(smplidx >= 500000)
if(smplidx == 0)
digitalWrite(ledPin, HIGH);
else
digitalWrite(ledPin, LOW);
if(smplidx >= 5)
smplidx=0;
}

void smpltimerisr(void)
{
smplidx++;
}

Ok so I now better understand the timer is happening ever second and is doing whatever is in the ISR() I guess is what you "could call it"

So to have the item toggle

Code:
void smpltimerisr(void);
const int ledPin = 13; // Teensy3 has LED on 13
volatile unsigned long smplidx = 0;
IntervalTimer smpltimer;
void setup(){
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
smplidx = 0;
smpltimer.begin(smpltimerisr, 500000);
}
void loop(){

if(smplidx == 0)
{digitalWrite(ledPin, HIGH);}
if(smplidx == 1){
digitalWrite(ledPin, LOW);}
}
void smpltimerisr(void){
switch (smplidx) {
    case 0:
      smplidx=1;
      break;
    case 1:
      smplidx=0;
      break;
}}
 
Last edited:
This might work for you. If you want it to change every 10s, then just do a counter and it will switch for every even/odd count. I can't test this code at the moment since running a 2hr test with my teensy right now, but it my mind it makes sense.
Code:
const int ledPin = 13; // Teensy3 has LED on 13
volatile unsigned long smplidx = 0;
volatile counter = 0;
IntervalTimer smpltimer;

void setup(){
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
smpltimer.begin(smpltimerisr, 10000000); //seconds = Value/1000000
}

void loop(){
// the division of two numbers:
  if (Counter % 2 == 0) { //every even count set high
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW); //if an odd count then there will be a remainder of 1
  }
}

void smpltimerisr(void){
Counter++;
}
 
Last edited:
Yep that should work and if you wanted to 1/2 that you could do 1/2 that timer I am assuming

Code:
const int ledPin = 13; // Teensy3 has LED on 13
volatile unsigned long smplidx = 0;
int Counter = 0;
IntervalTimer smpltimer;

void setup(){
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
smpltimer.begin(smpltimerisr, 500000); //1/2 sec tick
}

void loop(){
// the division of two numbers:
  if (Counter % 2 == 0) { //every even count set high
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW); //if an odd count then there will be a remainder of 1
  }
}

void smpltimerisr(void){
Counter++;
}
 
Last edited:
Status
Not open for further replies.
Back
Top