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

Thread: IntervalTimer question on Teensy 3.2

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Posts
    2

    IntervalTimer question on Teensy 3.2

    Hello. I've been trying to use the IntervalTimer library to manage the "main timing" functionality of a voltage sequencer im designing. The question I have is basically about how this library works.

    It looks like I can't use the begin function inside of the main loop. This is kida confusing to me cause if its not posible to initialize the timer inside the loop, how is the .end() function usable for freeing a timer in order to use it for another object? This might be a dumb question but I can't find more documentation/information than the one I already have. This is an example of what I tested.

    As you can see I make the .begin() function to run one time.

    void loop()
    if(first){
    main_timer.begin(tim, t);
    analogWrite(pin_CV, round(sequence[i]));
    }else{
    first = 0;
    analogWrite(pin_CV, round(sequence[i]));
    }

    Thanks in advanced.

  2. #2
    Senior Member BriComp's Avatar
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    412
    It makes your code more readable if you enclose it within code tags.
    You are more likely to get some help then.
    It's not difficult!!
    Code:
    void loop()
         if(first){
              main_timer.begin(tim, t);
              analogWrite(pin_CV, round(sequence[i]));
         }else{
              first = 0;
              analogWrite(pin_CV, round(sequence[i]));
         }

  3. #3
    Quote Originally Posted by roomed View Post
    It looks like I can't use the begin function inside of the main loop
    Please provide the entire program so that we can see what you're doing. Select all of the text in your INO program, then copy and paste into a forum message. After you paste, use your mouse to select all of the code, then click on the '#' symbol in the menu above. This formats the code to make it easier for others to read, as @BriComp said.

  4. #4
    Here is an example sketch with IntervalTimer and begin() is called from loop(). It works.

    Code:
    #include <IntervalTimer.h>
    
    IntervalTimer main_timer;
    
    volatile uint32_t main_timer_count;
    
    void main_timer_ISR( void )
    {
      main_timer_count++;
    }
    
    void setup()   {                
      while (!Serial) {} // wait for USB serial ready  
    }
    
    uint32_t first = 1; // detect first execution of loop()
    uint32_t prev_count; // to compute change in main_timer_count
    
    void loop() {
      if (first == 1) { // first time in loop()?
        main_timer.begin( main_timer_ISR, 1000 );
        first = 0; // make sure main_timer.begin() gets called only once
      }
      delay( 1000 ); // wait one second
      Serial.println( main_timer_count - prev_count ); // print number of main_timer interrupts
      prev_count = main_timer_count; // save for next time
    }

  5. #5
    Junior Member
    Join Date
    Sep 2021
    Posts
    2
    Quote Originally Posted by BriComp View Post
    It makes your code more readable if you enclose it within code tags.
    You are more likely to get some help then.
    It's not difficult!!
    Quote Originally Posted by joepasquariello View Post
    Please provide the entire program so that we can see what you're doing. Select all of the text in your INO program, then copy and paste into a forum message. After you paste, use your mouse to select all of the code, then click on the '#' symbol in the menu above. This formats the code to make it easier for others to read, as @BriComp said.
    Sorry for the improper post, won't happen again. Thanks for the help. I managed to find the problem in my code.

    Again, thank you very much for the quick response and sorry for doing an improper post.

Posting Permissions

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