I was just polling the pins, so it was triggering repeatedly when a button was pressed, but today at lunch I updated it to use the Bounce library and so it will only register the button once when it is pressed.
I want to clean it up, but,
#include <Bounce.h>
const int ledPin = 13;
const int buttonA = 2;
const int buttonB = 3;
const int buttonC = 4;
const int buttonD = 5;
const int buttonY = 6;
const int buttonN = 7;
Bounce bounceA = Bounce( buttonA,5 );
Bounce bounceB = Bounce( buttonB,5 );
Bounce bounceC = Bounce( buttonC,5 );
Bounce bounceD = Bounce( buttonD,5 );
Bounce bounceY = Bounce( buttonY,5 );
Bounce bounceN = Bounce( buttonN,5 );
boolean aPressed = false;
boolean bPressed = false;
boolean cPressed = false;
boolean dPressed = false;
boolean yPressed = false;
boolean nPressed = false;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonA,INPUT);
pinMode(buttonB,INPUT);
pinMode(buttonC,INPUT);
pinMode(buttonD,INPUT);
pinMode(buttonY,INPUT);
pinMode(buttonN,INPUT);
Serial.begin(9600);
}
void loop(){
bounceA.update ( );
bounceB.update ( );
bounceC.update ( );
bounceD.update ( );
bounceY.update ( );
bounceN.update ( );
digitalWrite(ledPin, LOW);
if(aPressed == false){
if (bounceA.read() == HIGH) {
digitalWrite(ledPin, HIGH);
Serial.println(String("2"));
aPressed = true;
}
}
if(aPressed == true){
if (bounceA.read() == LOW) {
digitalWrite(ledPin, LOW);
//Serial.println(String("2"));
aPressed = false;
}
}
if(bPressed == false){
if (bounceB.read() == HIGH) {
digitalWrite(ledPin, HIGH);
Serial.println(String("3"));
bPressed = true;
}
}
if(bPressed == true){
if (bounceB.read() == LOW) {
digitalWrite(ledPin, LOW);
bPressed = false;
}
}
if(cPressed == false){
if (bounceC.read() == HIGH) {
digitalWrite(ledPin, HIGH);
Serial.println(String("4"));
cPressed = true;
}
}
if(cPressed == true){
if (bounceC.read() == LOW) {
digitalWrite(ledPin, LOW);
cPressed = false;
}
}
if(dPressed == false){
if (bounceD.read() == HIGH) {
digitalWrite(ledPin, HIGH);
Serial.println(String("5"));
dPressed = true;
}
}
if(dPressed == true){
if (bounceD.read() == LOW) {
digitalWrite(ledPin, LOW);
dPressed = false;
}
}
if(yPressed == false){
if (bounceY.read() == HIGH) {
digitalWrite(ledPin, HIGH);
Serial.println(String("6"));
yPressed = true;
}
}
if(yPressed == true){
if (bounceY.read() == LOW) {
digitalWrite(ledPin, LOW);
yPressed = false;
}
}
if(nPressed == false){
if (bounceN.read() == HIGH) {
digitalWrite(ledPin, HIGH);
Serial.println(String("7"));
nPressed = true;
}
}
if(nPressed == true){
if (bounceN.read() == LOW) {
digitalWrite(ledPin, LOW);
nPressed = false;
}
}
}
In regard to "Is it possible to have 2 pressed at the same time?" , do you mean, register two simultaneous presses? No, so if buzzers A and B are pressed simultaneously, A will register as the button pressed unless you do it between polling A and B. So there is a slight benefit to being A over B, but also D over A. I calculate that the buttons are being polled at least every 10ms, so I think that is sufficient resolution, at least for my purposes.