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 23:29] – mithat | arduino:arduino_crash_course:digital_input [2012/11/12 18:29] – [Software debouncing] mithat | ||
---|---|---|---|
Line 68: | Line 68: | ||
boolean buttonState = digitalRead(pushButtonPin); | boolean buttonState = digitalRead(pushButtonPin); | ||
- | // if button goes down and before | + | // if button goes down and previously |
if (buttonState == LOW && lastButtonState == HIGH) { | if (buttonState == LOW && lastButtonState == HIGH) { | ||
if (ledState == HIGH) // toggle LED | if (ledState == HIGH) // toggle LED | ||
Line 101: | Line 101: | ||
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) { | boolean debounce(int pinNum) { | ||
Line 124: | Line 115: | ||
void loop() { | void loop() { | ||
- | // buttonState = debounce(pushButtonPin, | ||
buttonState = debounce(pushButtonPin); | buttonState = debounce(pushButtonPin); | ||
- | // if button goes down and before | + | // if button goes down and previously |
if (buttonState == LOW && lastButtonState == HIGH) { | if (buttonState == LOW && lastButtonState == HIGH) { | ||
ledState = !ledState; | ledState = !ledState; | ||
Line 151: | Line 141: | ||
===== Multiple state ===== | ===== Multiple state ===== | ||
- | The example above is a program that is essentially in of two states: the LED is on or off. Below are two examples where the program has multiple states. The first is a push-button | + | The example above is a program that is essentially in of two states: the LED is on or off. Below are two examples where the program has multiple states. The first is a timer-driven and the second a push-button |
+ | |||
+ | <file c MultipleLEDsTimer.ino> | ||
+ | /* | ||
+ | | ||
+ | Cycle through three LEDs with a timer. | ||
+ | */ | ||
+ | |||
+ | const int delayTime = 500; // time to wait between LED changes | ||
+ | const int led0 = 13; // connect an LED to pin 13 | ||
+ | const int led1 = 12; // connect an LED to pin 12 | ||
+ | const int led2 = 11; // connect an LED to pin 11 | ||
+ | |||
+ | int ledState; | ||
+ | |||
+ | void setup() { | ||
+ | // make LED pins outputs | ||
+ | pinMode(led0, | ||
+ | pinMode(led1, | ||
+ | pinMode(led2, | ||
+ | |||
+ | // initialize state | ||
+ | ledState = 0; | ||
+ | |||
+ | // turn the correct LED on and the others off | ||
+ | digitalWrite(led0, | ||
+ | digitalWrite(led1, | ||
+ | digitalWrite(led2, | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | delay(delayTime); | ||
+ | ledState = (ledState + 1) % 3; // cycle through 0,1,2 | ||
+ | |||
+ | // turn the correct LED on and the others off | ||
+ | if (ledState == 0) | ||
+ | { | ||
+ | digitalWrite(led0, | ||
+ | digitalWrite(led1, | ||
+ | digitalWrite(led2, | ||
+ | } | ||
+ | else if (ledState == 1) | ||
+ | { | ||
+ | digitalWrite(led0, | ||
+ | digitalWrite(led1, | ||
+ | digitalWrite(led2, | ||
+ | } | ||
+ | else if (ledState == 2) | ||
+ | { | ||
+ | digitalWrite(led0, | ||
+ | digitalWrite(led1, | ||
+ | digitalWrite(led2, | ||
+ | } | ||
+ | else // turn all LEDs on to indicate an error state. | ||
+ | { | ||
+ | digitalWrite(led0, | ||
+ | digitalWrite(led1, | ||
+ | digitalWrite(led2, | ||
+ | } | ||
+ | } | ||
+ | </ | ||
<file c MultipleLEDsPushButton.ino> | <file c MultipleLEDsPushButton.ino> | ||
Line 196: | Line 246: | ||
buttonState = debounce(pushButtonPin); | buttonState = debounce(pushButtonPin); | ||
- | // if button goes down and before | + | // if button goes down and previously |
if (buttonState == LOW && lastButtonState == HIGH) { | if (buttonState == LOW && lastButtonState == HIGH) { | ||
ledState = (ledState + 1) % 3; // cycle through 0,1,2 | ledState = (ledState + 1) % 3; // cycle through 0,1,2 | ||
Line 231: | Line 281: | ||
</ | </ | ||
- | <file c MultipleLEDsTimer.ino> | + | ===== Switch-case selection structure ===== |
+ | |||
+ | Both examples above use the //nested if-else// selection structure. Arduino' | ||
+ | |||
+ | <file c MultipleLEDsTimer2.ino> | ||
/* | /* | ||
- | MultipleLEDsTimer | + | MultipleLEDsTimer2 |
Cycle through three LEDs with a timer. | Cycle through three LEDs with a timer. | ||
+ | This version uses a switch-case selection structure. | ||
*/ | */ | ||
Line 252: | Line 307: | ||
// initialize state | // initialize state | ||
ledState = 0; | ledState = 0; | ||
- | | + | |
// turn the correct LED on and the others off | // turn the correct LED on and the others off | ||
digitalWrite(led0, | digitalWrite(led0, | ||
Line 264: | Line 319: | ||
// turn the correct LED on and the others off | // turn the correct LED on and the others off | ||
- | | + | |
- | | + | |
- | digitalWrite(led0, | + | |
- | digitalWrite(led1, | + | digitalWrite(led1, |
- | digitalWrite(led2, | + | digitalWrite(led2, |
- | } | + | |
- | else if (ledState == 1) | + | |
- | { | + | digitalWrite(led0, |
- | | + | digitalWrite(led1, |
- | digitalWrite(led1, | + | digitalWrite(led2, |
- | digitalWrite(led2, | + | |
- | } | + | |
- | else if (ledState == 2) | + | digitalWrite(led0, |
- | { | + | digitalWrite(led1, |
- | | + | digitalWrite(led2, |
- | digitalWrite(led1, | + | |
- | digitalWrite(led2, | + | |
- | } | + | digitalWrite(led0, |
- | else // turn all LEDs on to indicate an error state. | + | digitalWrite(led1, |
- | { | + | digitalWrite(led2, |
- | | + | break; |
- | digitalWrite(led1, | + | |
- | digitalWrite(led2, | + | |
} | } | ||
} | } | ||
+ | </ |
arduino/arduino_crash_course/digital_input.txt · Last modified: 2017/12/06 01:13 by mithat