/*
Pull-down & Pull-up buttons
*/
const int PD_BUTTON_PIN = 8;
const int PU_BUTTON_PIN = 7;
int oldPDval = HIGH; // pull-down is normally LOW
int oldPUval = LOW; // pull-up is normally HIGH
void setup() {
Serial.begin(115200);
pinMode(PD_BUTTON_PIN, INPUT); // INPUT needs external resistor
pinMode(PU_BUTTON_PIN, INPUT_PULLUP); // INPUT_PULLUP doesn't...
Serial.println("The pull-down button needs Vcc, ground, and an external pull-down resistor.");
Serial.println("The pull-up button needs only ground, and doesn't need an external resistor.");
Serial.println("Push a button to see its active state.\n");
}
void loop() {
int pdVal = digitalRead(PD_BUTTON_PIN);
int puVal = digitalRead(PU_BUTTON_PIN);
if (pdVal != oldPDval) { // if the value changed
oldPDval = pdVal; // save the new value
Serial.print("Pull-down button value: ");
Serial.println(pdVal ? "HIGH" : "LOW");
delay(20); // debounce
}
if (puVal != oldPUval) {
oldPUval = puVal;
Serial.print("Pull-up button value: ");
Serial.println(puVal ? "HIGH" : "LOW");
delay(20); // debounce
}
}
PULL-DOWN
PULL-UP