How to use moren than 2 interval timers at the same time on Teensy4

Status
Not open for further replies.

ttcaixp

Member
Hi, I have tired to use three interval timers at the same time on Teensy 4.
However, I only got responses from former two of them (myTimer and myTimer2 in the follwong code).
Could you help me to solve the problem?

Code:
IntervalTimer myTimer;
IntervalTimer myTimer2;
IntervalTimer myTimer3; 

void myTimerFunc();
void myTimer2Func();
void myTimer3Func();

int temp = 0;
int temp2 = 0;
int temp3 = 0;

void setup()
{
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);

  myTimer.begin(myTimerFunc, 2000);    
  myTimer2.begin(myTimer2Func, 2000); 
  myTimer3.begin(myTimer3Func, 5000);     
}

void loop()
{
}

void myTimerFunc()
{
   temp = !temp;
   digitalWrite(1, temp);
}

void myTimerFunc2()
{
   temp2 = !temp2;
   digitalWrite(2, temp2);
}

void myTimerFunc3()
{
   temp3 = !temp3;
   digitalWrite(3, temp3);
}
 
Which version of Teensyduino are you using. In Arduino, click Help > About to check.

This problem was fixed some time ago. Maybe you simply need to install the latest version to get the fix?
 
Hi, PaulStoffregen
My arduino version is 1.8.9, and teensyduino is 1.48.
I think the version of teensyduino is the latest one.

Is it related to the lib version?
If it is, whether it is possible for replacing my current lib by using the latest lib from github.
 
If the 'IntervalTimer ' from TeensyDuino is 1.48 that is the latest.

Which two work?

Does taking each of the working ones out in turn one at a time make the third one work?

Does it change anything if the time intervals are extended?
 
Is there anyone can help me?

Ok, I connected 3 LEDs and my oscilloscope to a Teensy 4.0, just for you.

First of all, your code from msg #1 doesn't even compile! In some places you use "myTimer2Func" & "myTimer3Func" (number in the middle), in others "myTimerFunc2" & "myTimerFunc3" (number on the end). Here's the code I actually ran:

Code:
IntervalTimer myTimer;
IntervalTimer myTimer2;
IntervalTimer myTimer3; 

void myTimerFunc();
void myTimer2Func();
void myTimer3Func();

int temp = 0;
int temp2 = 0;
int temp3 = 0;

void setup()
{
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);

  myTimer.begin(myTimerFunc, 2000);    
  myTimer2.begin(myTimer2Func, 2000); 
  myTimer3.begin(myTimer3Func, 5000);     
}

void loop()
{
}

void myTimerFunc()
{
   temp = !temp;
   digitalWrite(1, temp);
}

void myTimer2Func()
{
   temp2 = !temp2;
   digitalWrite(2, temp2);
}

void myTimer3Func()
{
   temp3 = !temp3;
   digitalWrite(3, temp3);
}

When this runs on here on a Teensy 4.0, my scope sees waveforms on all 3 pins.

file.png

Just so you can see how I tested, here's a photo of the hardware on my workbench.

DSC_0649_web.jpg
 
Since your code uses the same settings for 2 of the timers, I also tried a slight edit to configure the 2nd timer for a unique period.

Code:
  myTimer.begin(myTimerFunc, 2000);    
  myTimer2.begin(myTimer2Func, 3400); 
  myTimer3.begin(myTimer3Func, 5000);

With this change, I do indeed get 3 distinct waveforms.

file.png

Hopefully this can remove any doubt as to whether the 3 timers really are all working independently?

In all of these tests, the waveforms are much too fast for the human eye to see the LEDs blink. I also ran this test, which blinks the LEDs at a nice pace that makes visual confirmation without an oscilloscope easy to see.

Code:
  myTimer.begin(myTimerFunc, 200000);    
  myTimer2.begin(myTimer2Func, 340000); 
  myTimer3.begin(myTimer3Func, 500000);

Here's a quick video of those 3 LEDs blinking with this code running:


I still can't say why things are not working for you. The code you provided doesn't even compile. Maybe if you run this code with the errors fixed and use those slower numbers, you can confirm with 3 LEDs that is really does work.
 
Thank you very much!

Your test confirmed that teensy 4 can run three timers at the same time.
By the way, which IDE did you use?
I used PlatformIO IDE for running the code, and I got the abovementioned throubles.
Did you used original Arduino IDE?
 
Since your code uses the same settings for 2 of the timers, I also tried a slight edit to configure the 2nd timer for a unique period.

Code:
  myTimer.begin(myTimerFunc, 2000);    
  myTimer2.begin(myTimer2Func, 3400); 
  myTimer3.begin(myTimer3Func, 5000);

With this change, I do indeed get 3 distinct waveforms.

View attachment 18124

Hopefully this can remove any doubt as to whether the 3 timers really are all working independently?

In all of these tests, the waveforms are much too fast for the human eye to see the LEDs blink. I also ran this test, which blinks the LEDs at a nice pace that makes visual confirmation without an oscilloscope easy to see.

Code:
  myTimer.begin(myTimerFunc, 200000);    
  myTimer2.begin(myTimer2Func, 340000); 
  myTimer3.begin(myTimer3Func, 500000);

Here's a quick video of those 3 LEDs blinking with this code running:


I still can't say why things are not working for you. The code you provided doesn't even compile. Maybe if you run this code with the errors fixed and use those slower numbers, you can confirm with 3 LEDs that is really does work.

Thank you very much!

Your test confirmed that teensy 4 can run three timers at the same time.
By the way, which IDE did you use?
I used PlatformIO IDE for running the code, and I got the abovementioned throubles.
Did you used original Arduino IDE?
 
Since your code uses the same settings for 2 of the timers, I also tried a slight edit to configure the 2nd timer for a unique period.

Code:
  myTimer.begin(myTimerFunc, 2000);    
  myTimer2.begin(myTimer2Func, 3400); 
  myTimer3.begin(myTimer3Func, 5000);

With this change, I do indeed get 3 distinct waveforms.

View attachment 18124

Hopefully this can remove any doubt as to whether the 3 timers really are all working independently?

In all of these tests, the waveforms are much too fast for the human eye to see the LEDs blink. I also ran this test, which blinks the LEDs at a nice pace that makes visual confirmation without an oscilloscope easy to see.

Code:
  myTimer.begin(myTimerFunc, 200000);    
  myTimer2.begin(myTimer2Func, 340000); 
  myTimer3.begin(myTimer3Func, 500000);

Here's a quick video of those 3 LEDs blinking with this code running:


I still can't say why things are not working for you. The code you provided doesn't even compile. Maybe if you run this code with the errors fixed and use those slower numbers, you can confirm with 3 LEDs that is really does work.

Hi, PaulStoffregen

I just tried the same code on both Arduino IDE and PlatformIO IDE.
On Arduino IDE, the three timers works well.
However, On PlatformIO IDE, only timer1 and timer2 works, and the timer3 no response.

Do you know why such thing happens?

Thank you.
 
Thank you very much!

Your test confirmed that teensy 4 can run three timers at the same time.
By the way, which IDE did you use?
I used PlatformIO IDE for running the code, and I got the abovementioned throubles.
Did you used original Arduino IDE?


Just a note: Arduino is the targeted and tested IDE and build system. Working across all supported OS's it is easy for all to install target IDE and TeensyDuino versions and expect to get the same setup and functional behavior from installation to uploading.

There are some 4 or 10 ways to personalize the TeensyDuino install in modified build environments - the only way for all forum users and Paul to support and confirm expected functionality is on this common base IDE.

That goes not only for the end user - but also for creation/support of any of those ways that attempt to provide personalized modified build environments on any of the 3 core OS's using : PlatformIO, Vis_Studio, Vis_Code, Visual Micro, Command line, …

And the best thing is that PJRC making it work across the Teensy product line for that common Arduino IDE allows full access to years of development and refinement to work alike as possible on any Teensy whether ARM or AVR.

And the only apparent compromise to Teensy 100% on user task performance is that every exit of loop() calls Arduino defined yield() before re-entering loop() in order to feed the serialEvent() code for USB and UART devices.
 
Status
Not open for further replies.
Back
Top