is my Teensy broken or… Pins behaving strangely

Status
Not open for further replies.

Llevelynn

Member
hi there, i'm new to this forum and programming.

I have a teensy 4.0 and the issue is:
(non working code below, baut please follow this principle:)

i have 16 pushbuttons attached to the teensy.
the buttons should count up a variable (looperStati) with each press.

The code works, but on pins 6, 9, 10 and 13 the Variable always starts with 1!, not with 0.
or: with i++ always one more than it should be.

i tried different codes
swapped button.

same result.

Even weirder: when reset, everything except 6,9,10,13 works
after powerOn: every pini is one too hight!

ideas?

HTML:
String schalter [] = {"LO1", "LO2", "LO3", "LO4", "PB1", "PB2", "PB3", "PB4", "SO1", "SO2", "SO3", "SO4", "FX1", "FX2", "CLR", "S16"};
bool schalterA [16];

unsigned long timers [16];
unsigned long longClick = 700;

byte looperStati [16];

void looperDown (int i){
      timers[i] = millis();
} 

void looperUp (int i){
  if ((millis() - timers[i]) < longClick) {
    if (looperStati[i] == 0) {
        Serial.print (schalter[i]);
        Serial.println (" Recording");
        looperStati[i]++;
      }
    else if (looperStati[i] == 1) {
        Serial.print (schalter[i]);
        Serial.println (" Playing");
        looperStati[i]++;
    }
    else if (looperStati[i] == 2) {
        Serial.print (schalter[i]);
        Serial.println (" Stopped");
        looperStati[i]++;
    }
     else if (looperStati[i] == 3) {
        Serial.print (schalter[i]);
        Serial.println (" Playing");
        looperStati[i]++;
    }
    else if (looperStati[i] == 4) {
        Serial.print (schalter[i]);
        Serial.println (" Stopped");
        looperStati[i] = 3;
    }
   }

if (((millis() - timers[i]) >= longClick) && looperStati[i] >= 3) {
      Serial.print (schalter[i]);
      Serial.println (" Recording");
      looperStati[i] = 1;
      }      
}

void setup() {
  for(int i=0; i<16; i++){
    pinMode(i+5, INPUT_PULLUP);
    schalterA[i] = HIGH;
    looperStati[i] = 0;
    }
  
  Serial.begin(9600);
}

void loop() {


  for (int i = 0; i < 16; i++)
  {
    bool inputVal = digitalRead(i + 5);
    if (schalterA[i] != inputVal)
    {
      schalterA[i] = inputVal;
      //Serial.println(schalter[i]);

      if (inputVal)
        looperUp (i);
      else
        looperDown (i);
    }
  }   
}
 
Code:
void looperUp (int i){
  if ((millis() - timers[i]) < longClick) {

Shouldn't that be > or >= ?
As it is, it will respond to contact bounce (chatter). As @wootie11 suggests, have a look at the Bounce library and let it handle the bouncing contacts.

Pete
 
hi @Wootie11 , hi el supremo, thanks for your reply,
my buttons are hardware debounced with a RC-network.
I checked with the bounce-checker behind the link wootie posted.
No chatter.
BUT: have a look at this Bildschirmfoto 2021-08-03 um 20.00.20.png
pin 6 starts at 2.

And could chatter be an explanation for EVERY switch to start at 2 AFTER Power up and at 1 after reset with button?
puzzled...
 
Shouldn't that be > or >= ?
That is the problem. The click sequence seems to make sense once that change is made. It might also help to reduce longClick to something like 100 (or even less).

Pete
 
input pin is internal pullup, pushbutton connects to ground,
1K Resistor between switch and Input, 0.1uF capacitor from pin (and resistor) to ground.
 
input pin is internal pullup, pushbutton connects to ground,
1K Resistor between switch and Input, 0.1uF capacitor from pin (and resistor) to ground.

Yes, this is why you see the different behaviour between power on and reset.
The cap needs some time to load on power on (you use "pullup"). After a reset it is loaded already.
Just add a delay to setup() to make it consistent for poweron/reset - after pinMode()
 
Last edited:
Ok, that makes sense!
thanks a lot

but does not explain the behaviour of pin 6,9,10,13, I think.
I swapped the switches, swapped the Capacitors, tried without Capacitors...

Is there something different to these Pins that might require different Cap/Res Values?

strange stuff
 
Ok, that makes sense!
thanks a lot

but does not explain the behaviour of pin 6,9,10,13, I think.
I swapped the switches, swapped the Capacitors, tried without Capacitors...

Is there something different to these Pins that might require different Cap/Res Values?

strange stuff

Do they work if you disconnect the caps, resistors and buttons? use a wire and short them to ground then, to test it.
 
I figured out why you had 700ms for the longClick so ignore my comments in message #5.
I've added code to use the Bounce library and it works well on a T4 here.
If you use the Bounce library you can get rid of the external RC components.
As far as I can see, there is no reason for there to be anything special about any of the pins (6,9,10,13) except that pin 13 already has the builtin LED on it which can sometimes cause problems when it is used for other purposes.

Code:
// Add Bounce library
#include <Bounce.h>
Bounce *buttons[16];
// debounce period in milliseconds
const int ms = 10;

String schalter [] = {"LO1", "LO2", "LO3", "LO4", "PB1", "PB2", "PB3", "PB4", "SO1", "SO2", "SO3", "SO4", "FX1", "FX2", "CLR", "S16"};
bool schalterA [16];

unsigned long timers [16];
unsigned long longClick = 700;

byte looperStati [16];

void looperDown (int i)
{
  timers[i] = millis();
}

void looperUp (int i)
{
  if ((millis() - timers[i]) < longClick) {
    if (looperStati[i] == 0) {
      Serial.print (schalter[i]);
      Serial.println (" Recording");
      looperStati[i]++;
    } else if (looperStati[i] == 1) {
      Serial.print (schalter[i]);
      Serial.println (" Playing");
      looperStati[i]++;
    } else if (looperStati[i] == 2) {
      Serial.print (schalter[i]);
      Serial.println (" Stopped");
      looperStati[i]++;
    } else if (looperStati[i] == 3) {
      Serial.print (schalter[i]);
      Serial.println (" Playing");
      looperStati[i]++;
    } else if (looperStati[i] == 4) {
      Serial.print (schalter[i]);
      Serial.println (" Stopped");
      looperStati[i] = 3;
    }
  }

  if (((millis() - timers[i]) >= longClick) && looperStati[i] >= 3) {
    Serial.print (schalter[i]);
    Serial.println (" Recording");
    looperStati[i] = 1;
  }
}

void setup()
{
  for(int i=0; i<16; i++) {
    pinMode(i+5, INPUT_PULLUP);
    schalterA[i] = HIGH;
    looperStati[i] = 0;
    // Add a Bounce object for each button
    buttons[i] = new Bounce(i+5,ms);
  }

  Serial.begin(9600);
  delay(2000);
  Serial.println("Start");
}

void loop()
{
  // Update each button
  for(int i = 0; i < 16; i++) {
    buttons[i]->update();
    if(buttons[i]->risingEdge()) looperUp (i);
    if(buttons[i]->fallingEdge())looperDown(i);
  }
}

Pete
 
thanks everyone, i Will test the whole thing again without my debouncing circuits and the Software debounce.
If I ever find the time, that is...
 
Status
Not open for further replies.
Back
Top