T3.5 - testing results without using Serial.print on the spot

Status
Not open for further replies.

gony

Well-known member
i want to examine output from teensy before connecting it to the main apparatus .
the output is being drawn from a sFIFO and sent to port D every 30 mcsec via timer function.
until now i used Serial.print everytime the output was sent but it slows down the process , i want to assign the output into an array and than print it all at once with a pushbutton .
i add the code , mylist is an empty array which is assigned with the output (GPIOD_PDIR) everytime the output is being overwritten .

the current code is not good , i can't track the exact problem but it is not consistent , i'm lost in a swirl of errors and corrections not really sure where am i getting . most of the time nothing is printed , but sometimes it prints out weird results , the counter j reaches 143 and most of the output is zeros .

i am looking for an alternative approach to testing the output , maybe a better way of assigning values to a list inside a function which is called by timer ?
if there's no better way . maybe someone can point out programming errors such as wrong declaration if variables ?


thank you

the code
Code:
#include <Bounce.h>
//make sure the libraries are downloaded to CPU
#define SIMPLEFIFO_LARGE

#include <SimpleFIFO.h>

// PTA (12 15 16) = [3 27 28] // ready signal , switch and push buttons for manual reward
// PTB (0 1 2 3) = [16 17 19 18] // For address bits
// PTC (0 1 2 3 4 5 6 7 8 9 10 11) = [15 22 23 9 10 13 11 12 35 36 37 38] // For digital inputs from NI cards
// PTD (0 1 2 3 4 5 6 7 15) = [2 14 7 8 6 20 21 5 54] //for digital output to plexon and one strobe signal
// PTE (25) = [34] // reward

byte inputPins[] = {3, 4, 16, 17, 19, 18, 15, 22, 23, 9, 10, 13, 11, 12, 35, 36, 37, 38};
byte outputPins[] = {2, 14, 7, 8, 6, 20, 21, 5, 54, 34};
byte mylist[100] = {}; 
unsigned int j=0;
bool isCharOn = false; //set to true when fifo bit was sent , to false when port cleared
unsigned int rewardON ;

elapsedMicros charTime;
elapsedMillis rewardTime;
int onDur = 1;
const int rewardPosition = 25;
const int Dready = 3 ;// data ready pin number
const int buttonPin = 28;
const int buttonPin2 = 27;
Bounce pushbutton = Bounce(buttonPin, 10);
Bounce switchbutton = Bounce(buttonPin2, 10);

IntervalTimer myTimerOne;
IntervalTimer myTimerTwo;
IntervalTimer myTimerThree;
//define a fifo
SimpleFIFO <long, 255> sFIFO;
//strobe_charOff func waits for ready signal to turn low and receive strobe signal on port B , than inserts value of port C to fifo . independently , checks onDur time passed since charOn turned high and turns it low . (better results recorded for a joint timer than seperate timers for strobe and charOff) 
void strobe_charOff(void) {
  if (GPIOB_PDIR == 7) {

    sFIFO.enqueue(GPIOC_PDIR & 0xFF);
    //Serial.println("check port C");
    //Serial.println(GPIOC_PDOR & 0xFF);
  }
   
}
//charout sends out the first fifo value in line and initializes timer

void charOut() {
  if (sFIFO.count() > 0){
    if (GPIOD_PDOR == 0x00) {
      GPIOD_PDOR = sFIFO.dequeue()|0x8000;
      //Serial.println("check port D");
      //Serial.print(char(GPIOD_PDOR & 0xFF));
      mylist[j] = GPIOD_PDIR & 0xFF;
      j=j+1;
      isCharOn=true;
      charTime = 0;//timer
    }
  }
  else {
    sFIFO.flush();
  }
}

//manual reward checks if buttons have been pressed-  two buttons , one for quick 25 milisecond dosage ,
//other for drain . drain sets the reward pin high for 7 second (more than neccesary) with an additional press setting it back low.
//method is similar to other functions , set pin high ,initialize timer and set timer threshold.
void ManualReward(void) {

  if (pushbutton.update()) {
    if (pushbutton.risingEdge()) {
      rewardON = 25;
      GPIOE_PDOR= 0x2000000;
      rewardTime = 0;//start counting until stop
      Serial.print("j=");Serial.print(j);
      for (unsigned int k = 0; k < j; k++) {
          Serial.println(char(mylist[k]));
          Serial.println(mylist[k]);
      }
    }
  }
  if (switchbutton.update()) {
    if (switchbutton.fallingEdge()) {
      //if (GPIOE_PDOR == 0x00 ){
      rewardON = 7000;
      GPIOE_PDOR= 0x2000000;
      rewardTime = 0;//start counting until stop
      }
      else {
         rewardON = 25;
         rewardTime = 0;
      }
   }
}
void setup() {
  Serial.begin(9600);while(!Serial);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  for (int pinnum = 0; pinnum < 18; pinnum++) {
     //pinMode(inputPins[pinnum], INPUT_PULLDOWN);
     pinMode(inputPins[pinnum], INPUT);
  }
  for (int pinnum = 0; pinnum < 10; pinnum++) {
     pinMode(outputPins[pinnum], OUTPUT);
  }
  //setting timers , strobe recalls every 1 microsecond since the strobe input is the fastest.
  //charOut is second in priority , but recalls every 100000 micros since every letter needs 'onDur' time to be sent
  //manual reward recalles every 10000 micros (insignificant) but least in priority so to not override any loops that collect or send data . 
  attachInterrupt(digitalPinToInterrupt(Dready), strobe_charOff , FALLING);
  myTimerTwo.priority(1);
  myTimerTwo.begin(charOut,30);
  myTimerThree.priority(16);
  myTimerThree.begin(ManualReward,10000);
  sFIFO.flush();
  
}
//loop functions set pins back low . duration of output signal can be changed in the setup section . (onDur)
void loop() {
  if (isCharOn && charTime > onDur) {
    //Serial.print(char(GPIOD_PDOR & 0xFF));
    GPIOD_PDOR = 0x00;
    isCharOn = false;
  }
  if ( GPIOE_PDOR>>rewardPosition && rewardTime > rewardON) {//if the reward output is being sent for the input time
    //digitalWriteFast(34,LOW);
    GPIOE_PDOR = 0x0;//stop reward
      }

}
 
For debugging at human speed perhaps setting the time scales down by a factor of 100 to 10,000 would help slow it down enough { to the tenth of a second range? } to see desired inputs and observe the operation.

If the system were slowed then operation would be more observable and debug-able with the help of Serial.print not clogging the system.

It isn't clear what is pushing the buttons? A human or machine?

Perhaps rather than extreme speed timers to poll - setting the pins of interest to trigger on interrupts would be better
 
Your array mylist[100] has room for 100 entries before you start overwriting other variables and crashing your program. You say j reaches a value of 143. You need to prevent j from going above 99.
 
yes i tried a different approach and it is resolved .
i reduced the speed of the output command because it is not that important , and then print just after every output .
thanks!
 
Status
Not open for further replies.
Back
Top