uint8_t buttonPin[3] = {2, 3, 4};
uint8_t lastButtonState[3] = {1, 1, 1};;
unsigned long lastDebounceTime[3];
const uint16_t debounceDelay = 20;
void setup() {
Serial.begin(115200); Serial.println("hello There!\n\n");
for (uint8_t kk = 0; kk < 3; kk++)
pinMode(buttonPin[kk], INPUT_PULLUP);
}
void loop() {
for (uint8_t yy = 0; yy < 3; yy++) button(yy);
// delay(500); during testing
}
void button(byte i)
{
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin[i]);
if ((millis() - lastDebounceTime[i]) > debounceDelay) {
if (reading != lastButtonState[i]) {
lastDebounceTime[i] = millis();
lastButtonState[i] = reading;
if (lastButtonState[i] == LOW) // HIGH because when pressing the button, 5v will connect from the button to the pin. also the resistor is connected to the ground so when button is pressed, the path to least resistance is the volts
{
// Keyboard.press('x');
Serial.print(i); Serial.println(" pressed");
}
else
{
// Keyboard.release('x');
Serial.print(i); Serial.println(" released");
}
}
}
}
void buttonX(byte i)
{
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin[i]);
if (0) { // enabled during testing
Serial.print(i); Serial.print(" ");
Serial.print(reading); Serial.print(" ");
Serial.print(lastButtonState[i]); Serial.print(" ");
Serial.println("");
delay(300);
}
// if (reading != lastButtonState[i]) {
// lastDebounceTime[i] = millis();
// }
if ((millis() - lastDebounceTime[i]) > debounceDelay) {
// was buttonState, obvsly meant to be lastBuyttonState
if (reading != lastButtonState[i]) {
lastDebounceTime[i] = millis();
lastButtonState[i] = reading;
if (lastButtonState[i] == HIGH) // HIGH because when pressing the button, 5v will connect from the button to the pin. also the resistor is connected to the ground so when button is pressed, the path to least resistance is the volts
{
// Keyboard.press('x');
Serial.print(i); Serial.println(" released");
}
else
{
// Keyboard.release('x');
Serial.print(i); Serial.println(" pressed");
}
}
}
}