Entprellter Taster
Bei diesem Programm wird jedes Mal, nachdem der Taster gedrückt und wieder losgelassen wurde, der Zustand der LED geändert.
Bauteile:
- Arduino UNO
- 1 LED
- 1 Pushbutton
- 2 Widerstände (1x 200 Ohm, 1x 10 kOhm)
- 4 Kabel
- 1 Breadboard
Schaltskizze:
Code:
const int buttonPin = 2;
const int ledPin = 8;
int ledZustand = LOW;
int buttonZustand;
int letzterButtonZustand = HIGH;
long letzteDebounceZeit = 0;
long debounceDelay = 50;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ledZustand);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != letzterButtonZustand) {
letzteDebounceZeit = millis();
}
if ((millis() - letzteDebounceZeit) > debounceDelay) {
if (reading != buttonZustand) {
buttonZustand = reading;
if (buttonZustand == LOW) {
ledZustand = !ledZustand;
}
}
}
digitalWrite(ledPin, ledZustand);
letzterButtonZustand = reading;
}