#define ledPin 9
#define buttonPin 3
uint8_t buttonRead;
uint8_t oldButtonRead = 1;
const uint8_t debounceTime = 10; // milliseconds
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT); // With pulldown resistor added
}
void loop() {
buttonRead = digitalRead(buttonPin); // buttonRead will start as 0
// - - - - Debounce Section - - - -
// Serial.print("buttonRead 1: "); Serial.println(buttonRead);
// Serial.print("oldButtonRead 1: "); Serial.println(oldButtonRead);
if (buttonRead != oldButtonRead) { // has it changed since last time?
oldButtonRead = buttonRead; // remember for next time
// Serial.print("buttonRead: "); Serial.println(buttonRead);
// Serial.print("oldButtonRead: "); Serial.println(oldButtonRead);
delay (debounceTime); // debounce
// - - - - - - - - - - - - - - - - -
if (buttonRead == 0) {
digitalWrite(ledPin, LOW);
Serial.print("Button released: "); Serial.println(buttonRead);
}
if (buttonRead == 1) {
digitalWrite(ledPin, HIGH);
Serial.print("Button pushed: "); Serial.println(buttonRead);
}
}
}