Teensy 3.5 Stuck after Serial for sometime

Status
Not open for further replies.

isaacng97

New member
Hi, I connect 2 teensy 3.5 to make an spectrum analyzer. An audio board is connected to a master Teensy. I sent 4 bands of FFT to the slave Teensy using Serial TX pin 1. Since the data is in float, i converted it to int and parse it into a string and ended it with \n so that at the slave side I can detect an newline and split the data. However I noticed Teensy will stuck after sometimes. The slave data will be used to control the led array, somehow if i remove the led function, slave teensy will not stuck.

serial from master code:View attachment Teensy_FFT_receive_v2.0.ino
Code:
//serial from master
  if (Serial1.available()) {
    //        Serial.println(".");
    if (Serial1.read() == '\n') {
      int_1 = Serial1.parseInt();
      int_2 = Serial1.parseInt();
      int_3 = Serial1.parseInt();
      int_4 = Serial1.parseInt();
    }
  }


master to slave code:View attachment Teensy_FFT_v2.0.ino
Code:
 //convert float to int for slave
    //    Serial.println();
    int1 = 1000 * myFFT.read(47, 93); //2k to 4k
    int2 = 1000 * myFFT.read(94, 186); //4k to 8k
    int3 = 1000 * myFFT.read(187, 255); //8k to 11k
    int4 = 1000 * myFFT.read(256, 511); //11k to 22k

    //send to slave
    if (currentMillis - previousMillis_2 > 10) {
      // save the last time you blinked the LED
      previousMillis_2 = currentMillis;
      //      Serial.print(int1);
      Serial1.print(int1); Serial1.print(",");
      Serial1.print(int2); Serial1.print(",");
      Serial1.print(int3); Serial1.print(",");
      Serial1.print(int4); Serial1.print('\n');
    }
 
Quick look it seems the receive code reads through any data values throwing them away until '\n' is found with :: if (Serial1.read() == '\n')

Once '\n' is found it then expects to read the four integers.

It will be waiting for the next set of integers to be sent from the master - it seems like that is only 10 ms based on the snippet.

However if the values are not sent the slave will be waiting for : arduino.cc/reference/en/language/functions/communication/stream/streamsettimeout/ which is 1 seconds by default if it follows that indicated doc.
 
Thanks for replying. So my solution could be setting the timeout to be less than speed of data sending from the master, which means if master send at 10ms, timeout should be <10ms right?
 
That would be one way that might avoid long delay - but since the \n is at the end of the data it is backwards to wait for that to read the data right?

Alternatively reading each byte and parsing it would seem to be better.

after getting a \n until the first comma is int1, read those to a string and parse the number out, then repeat for int 2,3,4 - where int4 is followed by \n not comma.

Whenever a newline is received it is a complete transmission and if all 4 int's were found then they are good to use and restart the next set.
 
Status
Not open for further replies.
Back
Top