Millis and Pulsin or IntervalTimer or RXInterrupt.

jean

Active member
Hello,
sorry for my english,:rolleyes:
I use a teensy 4.0 the outputs numbered 2 to 10 are used to control leds with a PWM symbol.
Each led and controlled by a "state machine" with millis functions (9 millis defined randomly by state machine) triggering conditions such as on off long stop short stop.

The 8 outputs work very well at the same time, that's exactly what I want.

My problem comes when I want to control everything with an impulse from an RC receiver (input number 15 on the Teensy 4.0 board) using Pulsin or IntervalTimer or RXInterrupt.
independently the program works but when I add RXInterrupt for example, it sends me wrong values (reinitialization of interrupts?)

I have the same type of program on a nano card but with 4 outputs, 1 input which uses ISR but from what I have seen and tested it is not compatible 8 bits against 32 bits

Below is the code used with the Nano card, I don't know how to update it for Tennsy 4.0
I would like to replace Arduino by Tennsy it's so much better...

Thank you for possibly helping me if you have an idea of the problem!

Code:
ISR(PCINT0_vect)
{
    current_time = micros();

    // Channel 1 -------------------------------------------------
    if (PINB & B00000001) {                                        // Is input 8 high ?
        if (previous_state[CHANNEL1] == LOW) {                     // Input 8 changed from 0 to 1 (rising edge)
            previous_state[CHANNEL1] = HIGH;                       // Save current state
            timer[CHANNEL1]          = current_time;               // Start timer
        }
 
Just a thought and may not be the best way to do it, but the T4 is SO much faster than the Nano,
I think you could interrogate each line individually using DigReadFast just as quick as reading the whole port on the Nano.
Others will be able to steer you towards port input and output.
I do know that port input and output is 32 bits wide on the T4.x.
 
hello BriComp, thank you for your answer, I don't know DigReadFast, I'll see if I find examples to see if it can help me find a working solution.
 
You can use attachInterrupt, here is a simple explaination. https://forum.pjrc.com/threads/42800-Teensy-Interrupts-for-Dummies

You can see in that example that you do not need to read what pin caused the interrupt and you can have separate interrupt functions for each digital pin.

Also with the Teensy 4.0 you need to be careful of the voltage of the signal from your RC receiver, you may need some resistors to reduce the signal to 3.3 volts.

Edit: I guess you will need to read the pin if you use CHANGE or you could have two interrupt functions, one on falling and one on rising.
 
thank you rcarr for your answer,
actually I did not think of 3.3V ... I am in 6.4V
I tested like this but it stops the console.

attachInterrupt(digitalPinToInterrupt(15), test, CHANGE);
}
void test(){
if(micros()-presmillis >=intervale){
cont++;
presmillis=micros();
}
}
 
You can use two 1K resistors as a voltage divider to reduce 6.4 volts to 3.2 volts. Hopefully you haven't damaged pin 15 on your Teensy but you should keep that in mind as a possible future cause of problems.

For more help you should post your complete program, and use the code tags, the # in the strip menu.

Assuming you are using a receiver that drives standard analog servo's, the valid signal will be approximately 1000 to 2000 micros long and there is a 20 millisecond period between pulses. The way you have written your test function you will measure both. You can use that method but will need to separate the desired short pulse from the longer 20 millisecond wait. Something like:

Code:
void test(){
uint32_t temp;
static uint32_t previous_tm;

   temp = micros() - previous_tm;
   previous_tm = micros();

   if( temp >= 800 && temp <= 2200 ){
      valid_signal = temp;
      valid_flag = 1;
   }
}

and then in loop() you would check valid_flag for true, process valid_signal, and set valid_flag to zero.
 
Hello, thank you all for your help, I found why it was working badly, the signal connection was faulty on the breadboard.
once connected with a good connection, my problem had disappeared. I've been looking for three days.....

Thank you RCARR I preferred to modify my code inspired by your example, it's much cleaner.

now i would check my connections before posting a help request.
Again thank you to all of you.
 
I should have looked twice when I was testing it separately and it worked.
but I questioned my work first rather than the material.
;)
Again thank you to all of you.
 
Back
Top