Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Vorhergehende ÜberarbeitungLetzte Überarbeitung | |||
— | project:arduino-kickstarter-105 [20.11.2013 21:27] – paalsteek | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
+ | Vielleicht ist dir bei dem Programmbeispiel zur seriellen Kommunikation aufgefallen, | ||
+ | Das ist nicht schlimm wenn man etwas tun will, //solange// der Taster gedrückt ist. Will man aber eine Aktion //jedes Mal wenn der Taster gedrückt wird// ausführen, kann das Prellen zu Problemen führen: Obwohl der Taster nur einmal gedrückt wurde, nimmt der Arduino aufgrund des Prellens an, der Taster wäre zweimal schnell hintereinander betätigt worden. | ||
+ | |||
+ | Problematisch ist es zum Beispiel, wenn man mit dem Taster eine LED durch einmaliges Antippen des Tasters ein oder ausschalten will: Es kann passieren, dass die LED nur kurz aufblitzt und gleich wieder ausgeht. Um das zu verhindern, müssen wir den Taster // | ||
+ | |||
+ | Baue zum Testen nochmal die Schaltung aus dem " | ||
+ | |||
+ | {{: | ||
+ | |||
+ | Zur Anzeige werden wir die interne LED des Arduino verwenden, die fest an Pin 13 verdrahtet und mit " | ||
+ | |||
+ | === Das Programm === | ||
+ | |||
+ | // constants won't change. They' | ||
+ | // set pin numbers: | ||
+ | const int buttonPin = 2; // the number of the pushbutton pin | ||
+ | const int ledPin = 13; // the number of the LED pin | ||
+ | | ||
+ | // Variables will change: | ||
+ | int ledState = HIGH; // the current state of the output pin | ||
+ | int buttonState; | ||
+ | int lastButtonState = LOW; // the previous reading from the input pin | ||
+ | | ||
+ | // the following variables are long's because the time, measured in miliseconds, | ||
+ | // will quickly become a bigger number than can be stored in an int. | ||
+ | long lastDebounceTime = 0; // the last time the output pin was toggled | ||
+ | long debounceDelay = 50; // the debounce time; increase if the output flickers | ||
+ | | ||
+ | void setup() { | ||
+ | pinMode(buttonPin, | ||
+ | pinMode(ledPin, | ||
+ | } | ||
+ | | ||
+ | void loop() { | ||
+ | // read the state of the switch into a local variable: | ||
+ | int reading = digitalRead(buttonPin); | ||
+ | |||
+ | // check to see if you just pressed the button | ||
+ | // (i.e. the input went from LOW to HIGH), | ||
+ | // long enough since the last press to ignore any noise: | ||
+ | |||
+ | // If the switch changed, due to noise or pressing: | ||
+ | if (reading != lastButtonState) { | ||
+ | // reset the debouncing timer | ||
+ | lastDebounceTime = millis(); | ||
+ | } | ||
+ | |||
+ | if ((millis() - lastDebounceTime) > debounceDelay) { | ||
+ | // whatever the reading is at, it's been there for longer | ||
+ | // than the debounce delay, so take it as the actual current state: | ||
+ | buttonState = reading; | ||
+ | } | ||
+ | |||
+ | // set the LED using the state of the button: | ||
+ | digitalWrite(ledPin, | ||
+ | | ||
+ | // save the reading. | ||
+ | // it'll be the lastButtonState: | ||
+ | lastButtonState = reading; | ||
+ | } | ||
+ | |||
+ | Zu Beginn des Programms werden einige Konstanten und Variablen definiert. Die Konstanten speichern, wo LED und Taster angeschlossen sind. In den Variablen merken wir uns, ob die LED gerade an oder aus sein soll, ob der Taster gedrückt ist, und ob er im Schritt zuvor gedrückt war (warum wir das brauchen kommt gleich). | ||
+ | |||
+ | Der Setup-Teil schaltet nun den Pin 2 als Eingang und den Pin 13 als Ausgang. | ||
+ | |||
+ | Im Loop wird zu Beginn des Schleifendurchlaufs die Taste mittels '' | ||
+ | |||
+ | Das Programm tut also folgendes: Im Ruhezustand liegt am Pin 2 die " | ||
+ | |||
+ | **Frage:** Was passiert, wenn das debounceDelay zu groß gewählt ist? Was, wenn es zu klein ist? | ||
+ | |||
+ | **Transferaufgabe: |