Weird digital write behavior in TeensyDelay library and digitalWrite in interupts

Status
Not open for further replies.
I have this weird behavior when working with TeensyDelay to trigger a digitalWriteFast on a teensy 3.1 board (digitalWriteFast is a small library to use the FTM clock as a single shot timer)

With the following code, all interrupts are called (a and b variables are continuously updated and remain in sync), but the digitalWriteFast function is only called on the first iteration (led blinks once and stays off). If I remove the first = !first; line from timer() then everything works fine (but timer won't run for the 50ms I need, only 40ms, for some reason, beyond that it aliases down)

Timer() is a timer interrupt function that reschedules itself every 40ms in this case and should call SignalUp every second iterations in send a rising edge, and that in turn schedules SignalDown for the falling edge

Any ideas?
Thanks

Code:
#include <TeensyDelay.h>

volatile int a = 0;
volatile int b = 0;

void Timer() {
    static bool first = true;
    
    TeensyDelay::trigger(40000, 0);

    if (first)
        TeensyDelay::trigger(1000, 1);

    first = !first;
}

void SignalUp() {
    digitalWriteFast(LED_BUILTIN, HIGH);
    TeensyDelay::trigger(5000, 2);
    a++;
}

void SignalDown() {
    digitalWriteFast(LED_BUILTIN, LOW);
    b++;
}

void setup() 
{
    Serial.begin(9600);
        
    pinMode(LED_BUILTIN, OUTPUT);
    
    TeensyDelay::begin();
    TeensyDelay::addDelayChannel(Timer, 0);
    TeensyDelay::addDelayChannel(SignalUp, 1);
    TeensyDelay::addDelayChannel(SignalDown, 2);

    Timer();
}

void loop()
{
    Serial.print(a);
    Serial.print(" ");
    Serial.println(b);

    delay(500);
}
 
TeensyDelay isn't one of the many libs that comes with Teensyduino, so you need to tell us exactly where to get this one if you expect anyone to look at this problem.
 
Status
Not open for further replies.
Back
Top