#define Q1 2 // D2
#define Q2 3 // D3
#define Q3 4 // D4
void setup() {
Serial.begin(9600);
pinMode(Q1, INPUT);
pinMode(Q2, INPUT);
pinMode(Q3, INPUT);
digitalWrite(Q1, HIGH);
digitalWrite(Q2, HIGH);
digitalWrite(Q3, HIGH);
}
unsigned char lastState;
void loop() {
unsigned char state = getStates();
if (state != lastState) {
if (state > 0) {
Serial.print("Pressed: ");
Serial.println(state, DEC);
}
lastState = state;
}
delay(100);
}
unsigned char getStates () {
if (digitalRead(Q1) == LOW) {
return 3;
} else if (digitalRead(Q2) == LOW) {
return 2;
} else if (digitalRead(Q3) == LOW) {
return 1;
}
digitalWrite(Q2, LOW);
pinMode(Q2, OUTPUT);
delay(1);
boolean s1 = digitalRead(Q1);
boolean s3 = digitalRead(Q3);
pinMode(Q2, INPUT);
digitalWrite(Q2, HIGH);
if (s1 == LOW) {
return 5;
} else if (s3 == LOW) {
return 4;
}
digitalWrite(Q1, LOW);
pinMode(Q1, OUTPUT);
delay(1);
s3 = digitalRead(Q3);
pinMode(Q1, INPUT);
digitalWrite(Q1, HIGH);
if (s3 == LOW) {
return 6;
}
return 0;
}