Old debounce sketch

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
tatanka
 
Posts: 62
Joined: Tue Jul 03, 2012 9:06 am

Old debounce sketch

Post by tatanka »

Hello Everybody,
Working through some button troubles and came across this old blog post from 2009 from LadyAda about debouncing several buttons.

But when I copy and pasted into my IDE 1.0.5, lots of errors starting flying. Was wondering if you had a newer version or could tell me why I'm getting the errors.

Code: Select all

#define DEBOUNCE 10  // button debouncer, how many ms to debounce, 5+ ms is usually plenty

// here is where we define the buttons that we'll use. button "1" is the first, button "6" is the 6th, etc
byte buttons[] = {14, 15, 16, 17, 18, 19}; // the analog 0-5 pins are also known as 14-19
// This handy macro lets us determine how big the array up above is, by checking the size
#define NUMBUTTONS sizeof(buttons)
// we will track if a button is just pressed, just released, or 'currently pressed' 
volatile byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];

void setup() {
  byte i;
  
  // set up serial port
  Serial.begin(9600);
  Serial.print("Button checker with ");
  Serial.print(NUMBUTTONS, DEC);
  Serial.println(" buttons");

  // pin13 LED
  pinMode(13, OUTPUT);
 
  // Make input & enable pull-up resistors on switch pins
  for (i=0; ipinMode(buttons[i], INPUT);
    digitalWrite(buttons[i], HIGH);
  }

  // Run timer2 interrupt every 15 ms 
  TCCR2A = 0;
  TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20;

  //Timer2 Overflow Interrupt Enable
  TIMSK2 |= 1<<TOIE2;

}

SIGNAL(TIMER2_OVF_vect) {
  check_switches();
}

void check_switches()
{
  static byte previousstate[NUMBUTTONS];
  static byte currentstate[NUMBUTTONS];
  static long lasttime;
  byte index;

  if (millis() // we wrapped around, lets just try again
     lasttime = millis();
  }
  
  if ((lasttime + DEBOUNCE) > millis()) {
    // not enough time has passed to debounce
    return; 
  }
  // ok we have waited DEBOUNCE milliseconds, lets reset the timer
  lasttime = millis();
  
  for (index = 0; index digitalRead(buttons[index]);   // read the button
    
    /*     
    Serial.print(index, DEC);
    Serial.print(": cstate=");
    Serial.print(currentstate[index], DEC);
    Serial.print(", pstate=");
    Serial.print(previousstate[index], DEC);
    Serial.print(", press=");
    */
    
    if (currentstate[index] == previousstate[index]) {
      if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
          // just pressed
          justpressed[index] = 1;
      }
      else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
          // just released
          justreleased[index] = 1;
      }
      pressed[index] = !currentstate[index];  // remember, digital HIGH means NOT pressed
    }
    //Serial.println(pressed[index], DEC);
    previousstate[index] = currentstate[index];   // keep a running tally of the buttons
  }
}


void loop() {
  for (byte i = 0; i if (justpressed[i]) {
      justpressed[i] = 0;
      Serial.print(i, DEC);
      Serial.println(" Just pressed"); 
      // remember, check_switches() will CLEAR the 'just pressed' flag
    }
    if (justreleased[i]) {
      justreleased[i] = 0;
      Serial.print(i, DEC);
      Serial.println(" Just released");
      // remember, check_switches() will CLEAR the 'just pressed' flag
    }
    if (pressed[i]) {
      Serial.print(i, DEC);
      Serial.println(" pressed");
      // is the button pressed down at this moment
    }
  }
}


It gives me an error for expected constructor, destructor before 'if'. I did find at the beginning of the check_switches function an if statement that doesn't appear to be complete. I kind of assumed it was supposed to be

if (millis() < lasttime){

but that didn't solve all the problems. Any help would be appreciated.

Thanks.
Tim

User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: Old debounce sketch

Post by adafruit_support_mike »

Could you post the first few lines of errors you get please?

The latest version of the Arduino IDE is especially strict about type checking, and all compilers are a bit like the centipede who tripped over its shoelace: the first error is what causes most of the trouble, and the rest just describe where the pieces landed.

User avatar
tatanka
 
Posts: 62
Joined: Tue Jul 03, 2012 9:06 am

Re: Old debounce sketch

Post by tatanka »

The first few lines I get for error are:
sketch_apr11a:8: error: expected constructor, destructor, or type conversion before 'if'
sketch_apr11a.ino: In function 'void setup()':
sketch_apr11a:23: error: 'ipinMode' was not declared in this scope
and it highlights this line:

Code: Select all

byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];
Tim

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Old debounce sketch

Post by adafruit_support_bill »

Not sure where the 'i' came from, but I believe you want pinMode in your setup: http://arduino.cc/en/Reference/pinMode

Code: Select all

  // Make input & enable pull-up resistors on switch pins
  for (i=0; ipinMode(buttons[i], INPUT);
    digitalWrite(buttons[i], HIGH);
  }

User avatar
tatanka
 
Posts: 62
Joined: Tue Jul 03, 2012 9:06 am

Re: Old debounce sketch

Post by tatanka »

I found that as well, but when I 'fixed' it, I then received this error:

Code: Select all

sketch_apr11a:8: error: expected constructor, destructor, or type conversion before 'if'
sketch_apr11a.ino: In function 'void setup()':
sketch_apr11a:23: error: could not convert 'pinMode(buttons[((int)i)], 0u)' to 'bool'
sketch_apr11a:24: error: expected `)' before ';' token
sketch_apr11a.ino: At global scope:
Since this is not my code, I could easily start running in circles trying to bring it up to date. I was hoping maybe LadyAda had a 'master' copy since it appears the one that got posted to the website seems to have gotten 'corrupted' with the latest IDE revisions.

Thanks anyway.

Tim

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Old debounce sketch

Post by adafruit_support_bill »

It is a 5 year old post. I'm pretty sure it is not archived anywhere other than the blog.

It appears to be an incomplete 'for' loop statement. It should look like this:

Code: Select all

  // Make input & enable pull-up resistors on switch pins
  for (i=0; i < NUMBUTTONS; i++)
  {
    pinMode(buttons[i], INPUT);
    digitalWrite(buttons[i], HIGH);
  }

User avatar
tatanka
 
Posts: 62
Joined: Tue Jul 03, 2012 9:06 am

Re: Old debounce sketch

Post by tatanka »

It took a few modifications, but wasn't too bad. I think. Here is the debounce code; it compiles and appears to work.

Code: Select all

#define DEBOUNCE 20  // button debouncer, how many ms to debounce, 5+ ms is usually plenty

// here is where we define the buttons that we'll use. button "1" is the first, button "6" is the 6th, etc
byte buttons[] = {4, 9, 12, A0}; // the analog 0-5 pins are also known as 14-19
// This handy macro lets us determine how big the array up above is, by checking the size
#define NUMBUTTONS sizeof(buttons)
// we will track if a button is just pressed, just released, or 'currently pressed' 
volatile byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];

void setup() {
  byte i;
  
  // set up serial port
  Serial.begin(9600);
  Serial.print("Button checker with ");
  Serial.print(NUMBUTTONS, DEC);
  Serial.println(" buttons");

  // pin13 LED
  pinMode(13, OUTPUT);
 
  // Make input & enable pull-up resistors on switch pins
  for (i=0; i < NUMBUTTONS; i++)
  { pinMode(buttons[i], INPUT);
    digitalWrite(buttons[i], HIGH);
  }

  // Run timer2 interrupt every 15 ms 
  TCCR2A = 0;
  TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20;

  //Timer2 Overflow Interrupt Enable
  TIMSK2 |= 1<<TOIE2;

}

SIGNAL(TIMER2_OVF_vect) {
  check_switches();
}

void check_switches()
{
  static byte previousstate[NUMBUTTONS];
  static byte currentstate[NUMBUTTONS];
  static long lasttime;
  byte index;

  if (millis() < lasttime){ // we wrapped around, lets just try again
     lasttime = millis();
  }
  
  if ((lasttime + DEBOUNCE) > millis()) {
    // not enough time has passed to debounce
    return; 
  }
  // ok we have waited DEBOUNCE milliseconds, lets reset the timer
  lasttime = millis();
  
  for (index = 0; index < NUMBUTTONS; index++){
    currentstate[index] = digitalRead(buttons[index]);   // read the button
    
    /*     
    Serial.print(index, DEC);
    Serial.print(": cstate=");
    Serial.print(currentstate[index], DEC);
    Serial.print(", pstate=");
    Serial.print(previousstate[index], DEC);
    Serial.print(", press=");
    */
    
    if (currentstate[index] == previousstate[index]) {
      if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
          // just pressed
          justpressed[index] = 1;
      }
      else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
          // just released
          justreleased[index] = 1;
      }
      pressed[index] = !currentstate[index];  // remember, digital HIGH means NOT pressed
    }
    //Serial.println(pressed[index], DEC);
    previousstate[index] = currentstate[index];   // keep a running tally of the buttons
  }
}


void loop() {
  for (byte i = 0; i < NUMBUTTONS; i++){
  
    if (justpressed[i]) {
      justpressed[i] = 0;
      Serial.print(i, DEC);
      Serial.println(" Just pressed"); 
      // remember, check_switches() will CLEAR the 'just pressed' flag
    }
    if (justreleased[i]) {
      justreleased[i] = 0;
      Serial.print(i, DEC);
      Serial.println(" Just released");
      // remember, check_switches() will CLEAR the 'just pressed' flag
    }
    if (pressed[i]) {
      Serial.print(i, DEC);
      Serial.println(" pressed");
      // is the button pressed down at this moment
    }
  }
}


No too work on making it a standalone function.......

Tim

thiagoennes
 
Posts: 11
Joined: Mon Jul 21, 2014 7:38 am

Re: Old debounce sketch

Post by thiagoennes »

Thank you VERY much Tatanka, i was kicking myself trying to work with this code. It really looks corrupted, there are things like :
"
if (millis() // we wrapped around, lets just try again
lasttime = millis();
}
"
that are not even in syntax. Maybe the blogpost could be updated so people won't lose so much time?!
Again, THANK YOU.
And thank ladyada for the original code!
:)

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”