arduino:arduino_crash_course:digital_input
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
arduino:arduino_crash_course:digital_input [2012/11/05 18:46] – [Hardware debouncing] mithat | arduino:arduino_crash_course:digital_input [2012/11/05 22:34] – [Software debouncing] mithat | ||
---|---|---|---|
Line 11: | Line 11: | ||
Typically, digital input signals are designed so that when HIGH they have a value equal to the supply voltage and when LOW they are equal to 0 volts. | Typically, digital input signals are designed so that when HIGH they have a value equal to the supply voltage and when LOW they are equal to 0 volts. | ||
- | We've already seen an example of Arduino digital inputs in the [[arduino: | + | We've already seen an example of Arduino digital inputs in the [[arduino: |
- | FIXME with const and boolean | + | <file c LightSwitchPullup2a.ino> |
+ | /* | ||
+ | | ||
+ | Turn an LED on and off. | ||
+ | | ||
+ | */ | ||
+ | |||
+ | const int pushButtonPin = 2; // connect the push button to digital pin 2 | ||
+ | const int ledPin = 13; // connect the LED to pin 13 | ||
+ | boolean | ||
+ | |||
+ | void setup() { | ||
+ | pinMode(pushButtonPin, | ||
+ | digitalWrite(pushButtonPin, | ||
+ | pinMode(ledPin, | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | buttonState = digitalRead(pushButtonPin); | ||
+ | |||
+ | // set LED state accordingly | ||
+ | // note the inverted logic resulting from using pullup resistors. | ||
+ | digitalWrite(ledPin, | ||
+ | } | ||
+ | </ | ||
===== Toggling state ===== | ===== Toggling state ===== | ||
- | Below is an attempt to write an Arduino program that //toggles// power to an LED when a button is pressed. In other words, if the button | + | Below is an attempt to write an Arduino program that //toggles// power to an LED when a button is pressed. In other words, if the pushbutton |
- | To implement this functionality, | + | To implement this functionality, |
Our first attempt at writing the program is below. Be cautioned that it won't work as expected. It is logically correct, but it doesn' | Our first attempt at writing the program is below. Be cautioned that it won't work as expected. It is logically correct, but it doesn' | ||
Line 44: | Line 68: | ||
boolean buttonState = digitalRead(pushButtonPin); | boolean buttonState = digitalRead(pushButtonPin); | ||
- | // if button goes down and before it was high | + | // if button goes down and before it was high... |
if (buttonState == LOW && lastButtonState == HIGH) { | if (buttonState == LOW && lastButtonState == HIGH) { | ||
if (ledState == HIGH) // toggle LED | if (ledState == HIGH) // toggle LED | ||
Line 53: | Line 77: | ||
digitalWrite(ledPin, | digitalWrite(ledPin, | ||
} | } | ||
- | | + | |
lastButtonState = buttonState; | lastButtonState = buttonState; | ||
} | } | ||
Line 81: | Line 105: | ||
boolean lastButtonState = HIGH; // value of buttonState from previous loop iteration | boolean lastButtonState = HIGH; // value of buttonState from previous loop iteration | ||
boolean buttonState = HIGH; | boolean buttonState = HIGH; | ||
+ | |||
+ | /* boolean debounce(int pinNum, boolean lastState) { | ||
+ | boolean nowState = digitalRead(pinNum); | ||
+ | if (lastState != nowState) { | ||
+ | delay(debounceTime); | ||
+ | nowState = digitalRead(pinNum); | ||
+ | } | ||
+ | return nowState; | ||
+ | } */ | ||
+ | |||
+ | boolean debounce(int pinNum) { | ||
+ | delay(debounceTime); | ||
+ | return digitalRead(pinNum); | ||
+ | } | ||
void setup() { | void setup() { | ||
Line 87: | Line 125: | ||
pinMode(ledPin, | pinMode(ledPin, | ||
digitalWrite(ledPin, | digitalWrite(ledPin, | ||
- | } | ||
- | |||
- | boolean debounce(boolean lastState) { | ||
- | boolean nowState = digitalRead(pushButtonPin); | ||
- | if (lastState != nowState) { | ||
- | delay(debounceTime); | ||
- | nowState = digitalRead(pushButtonPin); | ||
- | } | ||
- | return nowState; | ||
} | } | ||
void loop() { | void loop() { | ||
- | | + | // |
+ | buttonState = debounce(pushButtonPin); // read the input pin | ||
- | // if button goes down and before it was high | + | // if button goes down and before it was high... |
if (buttonState == LOW && lastButtonState == HIGH) { | if (buttonState == LOW && lastButtonState == HIGH) { | ||
- | buttonState = digitalRead(pushButtonPin); | ||
- | |||
ledState = !ledState; | ledState = !ledState; | ||
} | } | ||
Line 113: | Line 141: | ||
</ | </ | ||
- | More elaborate debouncing techniques have also been used. For example, you can measure the time between HIGH-to-LOW or LOW-to-HIGH transitions and when they have gotten long enough you can assume that the switch is no longer bouncing. We leave the reader to research and explore these. | + | More elaborate debouncing techniques have also been used. For example, you can measure the time between HIGH-to-LOW or LOW-to-HIGH transitions and when they have gotten long enough you can assume that the switch is no longer bouncing. We leave it to the reader to research and explore these. |
- | === Using a user-defined | + | === User-defined |
- | The the example above places the switch reading and debouncing code into its own **function**. One advantage of doing this is that it creates | + | The the example above places the switch reading and debouncing code into its own **function**. One advantage of doing this is that it creates clarity in the code by allowing |
==== Hardware debouncing ==== | ==== Hardware debouncing ==== |
arduino/arduino_crash_course/digital_input.txt · Last modified: 2017/12/06 01:13 by mithat