Help with this multiple buttons code

Hi, I need a simple and reliable method for 6-buttons on one or two pins. Could someone please help me with this or provide link(s) to a better alternative.

I found the adafruit code below, but it spits at following line with “can’t convert a void to bool…”

  for (i=0; ipinMode(buttons[i], INPUT);

I have searched and tried to change stuff and in desperation removed that line and hard coded the pin assignments.

  pinMode(buttons[0],INPUT);
  pinMode(buttons[1],INPUT);
  ...

But then I get an error “expected unqualified-id before ‘volatile’” at

  TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20;

Here’s the adafruit stuff.
Thanks.

#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
    }
  }
}

(deleted)

Looks alot like you have attempted to copy something which was HTML formatted and this gives you a lot of errors because “<” is considered a HTML tag opener. You should probably find a better way to copy code from the interweb :wink:

spycatcher2k:
Please post a link to where you got this nonsense.

"adafruit multiple buttons" google returns it as the first item.

Danois90:
Looks alot like you have attempted to copy something which was HTML formatted and this gives you a lot of errors because “<” is considered a HTML tag opener. You should probably find a better way to copy code from the interweb :wink:

Thanks, but that is what is actually visible on the web-page. Second example down.

You’re right. But unfortunately the code on that page isn’t right.

I guess that line may have been intended to be something like -

  for (i=0; i<NUMBUTTONS; i++){
    pinMode(buttons[i], INPUT);

but it’s just a guess and who knows what else is wrong with the code.

Steve

(deleted)

(deleted)