timeout for serial.println

Status
Not open for further replies.

kteepa

Member
Hey All. I'm using the Teensy 4.1 to sample a set of sensors for graduate work. Currently I have it sample at 4kHz (once every 250us) and print the sensor readings. However, sometimes there is some sort of delay that happens where the teensy didn't sample for a couple of seconds. I'm assuming that it is not dropped packets but is instead a result of the teensy waiting at the serial.print line for things to transfer before it goes on in the loop. I'm assuming this because once it starts sampling again, the delay from the last sample is not a multiple of the sampling rate

How do I make it so that teensy only spends a few microseconds waiting for serial.print to work and then, if it is still waiting, to give up and continue the loop?

Basically, I would rather miss a sensor reading or two than have the sampling be shifted by something other than 250us

Here is my code:
Code:
#include <TimeLib.h>
#include <Bounce2.h>
#include <Keyboard.h>

// general timers
elapsedMicros SinceStart;
elapsedMicros LastScan;

// for sampling rate
unsigned long ScanSpace = 250;

// pin labels
const int FSR1 = 14;
int FSR1_val;
const int FSR2 = 15;
int FSR2_val;
const int FSR3 = 16;
int FSR3_val;
const int FSR4 = 17;
int FSR4_val;
const int FSR5 = 18;
int FSR5_val;

const int EEG = 40;
int EEG_val;

const int PhotoD = 41;
int PhotoD_val;

const int Key1 = 2;
int Key1_val;
int lastKey1 = LOW;
const int Key2 = 3;
int Key2_val;
int lastKey2 = LOW;
const int Key3 = 4;
int Key3_val;
int lastKey3 = LOW;
const int Key4 = 5;
int Key4_val;
int lastKey4 = LOW;
const int Key5 = 6;
int Key5_val;
int lastKey5 = LOW;

Bounce debouncer1 = Bounce(); 
Bounce debouncer2 = Bounce(); 
Bounce debouncer3 = Bounce(); 
Bounce debouncer4 = Bounce(); 
Bounce debouncer5 = Bounce(); 

const int debounce_time = 5;

String Report;


void setup() {
  setSyncProvider(getTeensy3Time);
  
  pinMode(FSR1,INPUT_DISABLE);
  pinMode(FSR2,INPUT_DISABLE);
  pinMode(FSR3,INPUT_DISABLE);
  pinMode(FSR4,INPUT_DISABLE);
  pinMode(FSR5,INPUT_DISABLE);

  pinMode(PhotoD,INPUT_DISABLE);
  
  pinMode(EEG, INPUT_DISABLE);
  
  pinMode(Key1,INPUT);
  debouncer1.attach(Key1);
  debouncer1.interval(debounce_time); // interval in ms
  pinMode(Key2,INPUT);
  debouncer2.attach(Key2);
  debouncer2.interval(debounce_time); // interval in ms
  pinMode(Key3,INPUT);
  debouncer3.attach(Key3);
  debouncer3.interval(debounce_time); // interval in ms
  pinMode(Key4,INPUT);
  debouncer4.attach(Key4);
  debouncer4.interval(debounce_time); // interval in ms
  pinMode(Key5,INPUT);
  debouncer5.attach(Key5);
  debouncer5.interval(debounce_time); // interval in ms
  
  Serial.begin(9600); 
  Keyboard.begin();

  delay(10000);
  Serial.println("EEG,PhotoD,FSR1,FSR2,FSR3,FSR4,FSR5,Key1,Key2,Key3,Key4,Key5,ElapsedMicros,Ticks,RTC");
  
}

void loop() {
  

  if (LastScan >= ScanSpace){

    LastScan = 0;
    
    FSR1_val = analogRead(FSR1);
    FSR2_val = analogRead(FSR2);
    FSR3_val = analogRead(FSR3);
    FSR4_val = analogRead(FSR4);
    FSR5_val = analogRead(FSR5);

    PhotoD_val = analogRead(PhotoD);
    PhotoD_val = analogRead(PhotoD);
    PhotoD_val = analogRead(PhotoD);

    EEG_val = analogRead(EEG);
    EEG_val = analogRead(EEG);
    EEG_val = analogRead(EEG);
    EEG_val = analogRead(EEG);

    debouncer1.update();
    debouncer2.update();
    debouncer3.update();
    debouncer4.update();
    debouncer5.update();

    Key1_val = debouncer1.read();
    Key2_val = debouncer2.read();
    Key3_val = debouncer3.read();
    Key4_val = debouncer4.read();
    Key5_val = debouncer5.read();


    // print timestamps: EEG_val, PhotoD_val, ..., FSR1_val, FSR2_val, ...,  Key1_val, ..., micros(), ARM_DWT_CYCCNT, RTC
    Report = String(EEG_val)+","+String(PhotoD_val)+","+String(FSR1_val)+","+String(FSR2_val)+","+String(FSR3_val)+","+String(FSR4_val)+","+String(FSR5_val)+","+String(Key1_val)+","+String(Key2_val)+","+String(Key3_val)+","+String(Key4_val)+","+String(Key5_val)+","+String(SinceStart)+","+String(ARM_DWT_CYCCNT)+","+String(now());
    Serial.println(Report);

    
    // keyboard interaction with computer
    if (Key1_val == HIGH && lastKey1 == LOW) {
      Keyboard.write('1');
    }
    if (Key2_val == HIGH && lastKey2 == LOW) {
      Keyboard.write('2');
    }
    if (Key3_val == HIGH && lastKey3 == LOW) {
      Keyboard.write('3');
    }
    if (Key4_val == HIGH && lastKey4 == LOW) {
      Keyboard.write('4');
    }
    if (Key5_val == HIGH && lastKey5 == LOW) {
      Keyboard.write('5');
    }

    lastKey1 = Key1_val;
    lastKey2 = Key2_val;
    lastKey3 = Key3_val;
    lastKey4 = Key4_val;
    lastKey5 = Key5_val;

  }
}


time_t getTeensy3Time()
{
  return Teensy3Clock.get();
}
 
Last edited by a moderator:
How do I make it so that teensy only spends a few microseconds waiting for serial.print to work and then, if it is still waiting, to give up and continue the loop?

Basically, I would rather miss a sensor reading or two than have the sampling be shifted by something other than 250us

You can use Serial.availableForWrite() to check how much space is remaining in Teensy's transmit buffer.
 
Status
Not open for further replies.
Back
Top