Encoder Freezes Serial Port on Teensy 4.1

Status
Not open for further replies.
Hi Folks,

I'm setting up a new synth that uses 12 buttons, two encoders, and one potentiometer. I got pretty stuck with the encoders. I got the encoders working with example code. However, when I add the encoder lines to my main code, I have trouble with the serial monitor. When I reset the Teensy, the Serial monitor will work fine until I turn one of the encoders. After that, the serial monitor stops completely.

To clarify, I have serial messages that read out any time I push a button, turn a pot, or turn an encoder. So the messages work fine until I make a single turn on either encoder. I do not get the message that shold indicate a change in the encoder position. After that, I also stop receiving Serial messages from the buttons and pot. I am using VSCode if that matters.

Do any one you have any ideas of things I could check next? Right now I'm trying to slowly deconstruct my code to the complexity of the example code and the building it back up. I've found that if I comment out the encoder objects that fixes the issue but then I don't have encoders. Thank you for your help!

Code:
#include <Audio.h>
#include <SD.h>
#include <SerialFlash.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include <Bounce.h>
#include <Encoder.h>

//Engage debug messages
bool potDebug = true;
bool encoderDebug = true;
bool buttonDebug = true;
bool synthDebug = true;

//buttons, digital
const int numDPins = 16;
int dPins[numDPins] = {14,24,25,28,29,30,31,32,34,35,36,37,1,2,4,5};
int dVals[numDPins];
int dValsLag[numDPins];
Bounce dBounce[] = {
  Bounce(14,10),
  Bounce(24,10),
  Bounce(25,10),
  Bounce(28,10),
  Bounce(29,10),
  Bounce(30,10),
  Bounce(31,10),
  Bounce(32,10),
  Bounce(34,10),
  Bounce(35,10),
  Bounce(36,10),
  Bounce(37,10),
};

//hardware controls, analog
const int numAPins = 1;
int aPins[numAPins] = {A1};
int aValsRaw[numAPins];
int aVals[numAPins];
int aValsLag[numAPins];
int aFlutter = 20; //change in analog reading before we count it

//encoders
const int numEncs = 2;
int ePins[numEncs * 2] = {1,2,4,5};
Encoder myEncA(1,2);
Encoder myEncB(4,5);
long oldPositionA  = -999;
long newPositionA = 0;
long oldPositionB = -999;
long newPositionB = 0;

//LEDs
int ledPin = 13;

//**************
//MAIN FUNCTIONS
//**************
void readInputs() {
  //Read Digital Pins
  for (int i = 0; i < numDPins; i++) {
    dBounce[i].update();
  }

  //Read Analog Pins
  for (int i = 0; i < numAPins; i++) {
    aValsRaw[i] = analogRead(aPins[i]);
  }
  //Reject analog fluctuations
  for (int i = 0; i < numAPins; i++) {
    aValsLag[i] = aVals[i]; //store old value for refence

    //Update aVal if the change exceeds the flutter
    if (aValsRaw[i] > aVals[i] + aFlutter || aValsRaw[i] < aVals[i] - aFlutter) {
      aVals[i] = aValsRaw[i]; //store new value
      if (potDebug) {Serial.println(aVals[i]);}
    }
  }
}

void setup() {
  // put your setup code here, to run once:
  // initialize the digital pin as an output.
  pinMode(ledPin, OUTPUT);

  //Initialize Digital Pins
  for (int i = 0; i < numDPins; i++) {
    pinMode(dPins[i], INPUT_PULLUP);
    dBounce[i].update();
  }

  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  readInputs();

  //Display digital pin status
  for (int i = 0; i < numDPins; i++) {
    if (dBounce[i].fallingEdge()) {
      Serial.print("Press Pin ");
      Serial.println(dPins[i]);
    }
  }

  long newPositionA = myEncA.read();
  if (newPositionA != oldPositionA) {
    oldPositionA = newPositionA;
    Serial.print("Enc A Position ");
    if (encoderDebug) {Serial.println(newPositionA);}
    Serial.println(newPositionA);
  }
  
  long newPositionB = myEncB.read();
  if (newPositionB != oldPositionB) {
    oldPositionB = newPositionB;
    if (encoderDebug) {Serial.println(newPositionB);}
  }
}
 
Hi TeensyWolf, thanks for your quick reply. Is hwserial a library file that I edit or a function I call in compile time or run time? (I think I'm using the lingo right). Does hwserial apply if I am using the USB Port on Teensy 4.1 for the serial monitor? I get the impression that "USB Serial" applies to the USB port and "HW Serial" applies to serial channel broken out to the pins of the Teensy PCB.
 
Your dBounce array has only 12 entries but in the for loop in readInputs() you are calling update() for 16 entries.
 
HardwareSerialX.cpp, in the Teensy Core

X = 1 thru 8, pick the one(s) you are using

Edit this line (#38) which is 64 by default, here I've changed it to 256for my application.

Code:
#define SERIAL3_RX_BUFFER_SIZE     256// number of incoming bytes to buffer
 
Your dBounce array has only 12 entries but in the for loop in readInputs() you are calling update() for 16 entries.

This fixed it! Thank you for your help. I guess accessing an array outside of its length really messes things up.

TeensyWolf thanks for your help too. I'll keep the serial buffer concept in mind for future problems.
 
Status
Not open for further replies.
Back
Top