// Button Example One (active high)
//
// Button not pressed: signal is LOW
// Button pressed : signal is HIGH
//
// The resistor keeps pin 8 low when the button
// is not pressed.
// The resistor is a pulldown resistor.
//
// Button Example One: https://wokwi.com/projects/397990618860958721
// Button Example Two: https://wokwi.com/projects/397990611031240705
//
// 16 May 2024, by Koepel, Public Domain.
const int buttonPin = 8;
int oldValue = LOW; // default/idle value for pin 8 is low.
void setup()
{
Serial.begin(115200);
Serial.println("Press the button.");
// Initialize the pin for reading the button.
pinMode(buttonPin, INPUT);
}
void loop()
{
// Read the value of pin 8.
int newValue = digitalRead(buttonPin);
// Check if the value was changed,
// by comparing it with the previous value.
if(newValue != oldValue)
{
if(newValue == HIGH)
{
Serial.println("The button is pressed.");
}
else
{
Serial.println("The button is released.");
}
// Remember the value for the next time.
oldValue = newValue;
}
// Slow down the sketch.
// Also for debouncing the button.
delay(100);
}