Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 12 of 12

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

  1. #1
    Junior Member
    Join Date
    Nov 2019
    Posts
    6

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

    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);
    }

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,774
    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?

  3. #3
    Junior Member
    Join Date
    Nov 2019
    Posts
    6
    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.

  4. #4
    Junior Member
    Join Date
    Nov 2019
    Posts
    6
    Quote Originally Posted by PaulStoffregen View Post
    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?
    Is there anyone can help me?

  5. #5
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,075
    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?

  6. #6
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,774
    Quote Originally Posted by ttcaixp View Post
    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.

    Click image for larger version. 

Name:	file.png 
Views:	3 
Size:	34.8 KB 
ID:	18122

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

    Click image for larger version. 

Name:	DSC_0649_web.jpg 
Views:	4 
Size:	173.4 KB 
ID:	18123

  7. #7
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,774
    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.

    Click image for larger version. 

Name:	file.png 
Views:	4 
Size:	34.3 KB 
ID:	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.

  8. #8
    Junior Member
    Join Date
    Nov 2019
    Posts
    6
    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?

  9. #9
    Junior Member
    Join Date
    Nov 2019
    Posts
    6
    Quote Originally Posted by PaulStoffregen View Post
    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.

    Click image for larger version. 

Name:	file.png 
Views:	4 
Size:	34.3 KB 
ID:	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?

  10. #10
    Junior Member
    Join Date
    Nov 2019
    Posts
    6
    Quote Originally Posted by PaulStoffregen View Post
    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.

    Click image for larger version. 

Name:	file.png 
Views:	4 
Size:	34.3 KB 
ID:	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.

  11. #11
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,774
    Quote Originally Posted by ttcaixp View Post
    Do you know why such thing happens?
    No, I don't *know* why.

    But since this was a known bug in an older version of the core library, my guess would be that PlatformIO may still be using that older code from before this bug was fixed.

  12. #12
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,075
    Quote Originally Posted by ttcaixp View Post
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •