Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 13 of 13

Thread: is my Teensy broken or Pins behaving strangely

  1. #1
    Junior Member
    Join Date
    Aug 2021
    Posts
    5

    is my Teensy broken or Pins behaving strangely

    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 Code:
    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);
        }
      }   
    }

  2. #2
    Junior Member
    Join Date
    Jul 2021
    Posts
    7
    I'm not exactly sure, but I think your code might be plagued by chatter - check out https://www.pjrc.com/teensy/td_libs_Bounce.html for more info.

  3. #3
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,750
    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

  4. #4
    Junior Member
    Join Date
    Aug 2021
    Posts
    5
    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 thisClick image for larger version. 

Name:	Bildschirmfoto 2021-08-03 um 20.00.20.png 
Views:	14 
Size:	57.2 KB 
ID:	25502
    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...

  5. #5
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,750
    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

  6. #6
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    9,333
    How does your RC Network look like? Can you post a schematic?

  7. #7
    Junior Member
    Join Date
    Aug 2021
    Posts
    5
    input pin is internal pullup, pushbutton connects to ground,
    1K Resistor between switch and Input, 0.1uF capacitor from pin (and resistor) to ground.

  8. #8
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    9,333
    Quote Originally Posted by Llevelynn View Post
    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 by Frank B; 08-03-2021 at 10:08 PM.

  9. #9
    Junior Member
    Join Date
    Aug 2021
    Posts
    5
    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

  10. #10
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    9,333
    Quote Originally Posted by Llevelynn View Post
    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.

  11. #11
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    9,333
    What means "recording" in your progrm. Is there something else connected?

  12. #12
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,750
    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

  13. #13
    Junior Member
    Join Date
    Aug 2021
    Posts
    5
    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...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •