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 revision | ||
arduino:arduino_crash_course:digital_input [2012/11/05 18:59] – [Toggling state] mithat | arduino:arduino_crash_course:digital_input [2017/12/06 01:13] (current) – [Digital Input] mithat | ||
---|---|---|---|
Line 3: | Line 3: | ||
The Arduino literature refers to " | The Arduino literature refers to " | ||
- | An Arduino digital input is one that responds to two different levels; anything above a certain | + | An Arduino digital input is one that responds to two different levels: anything above a certain |
<WRAP center round important 60%> | <WRAP center round important 60%> | ||
Line 9: | Line 9: | ||
</ | </ | ||
- | 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. | + | Digital output |
- | 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 | + | We have also added the '' |
+ | |||
+ | <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 ===== | ||
Line 44: | Line 70: | ||
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 53: | Line 79: | ||
digitalWrite(ledPin, | digitalWrite(ledPin, | ||
} | } | ||
- | | + | |
lastButtonState = buttonState; | lastButtonState = buttonState; | ||
} | } | ||
Line 62: | Line 88: | ||
==== Software debouncing ==== | ==== Software debouncing ==== | ||
- | A very simple approach debouncing is to introduce a delay whenever a relevant switch press is detected. The amount of delay will vary from switch to switch, so experimentation will likely be required. | + | A very simple approach |
- | + | ||
- | FIXME general | + | |
- | + | ||
- | FIXME add pind number to debounce function | + | |
<file c LightToggleDebounced.ino> | <file c LightToggleDebounced.ino> | ||
Line 81: | Line 103: | ||
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) { | ||
+ | delay(debounceTime); | ||
+ | return digitalRead(pinNum); | ||
+ | } | ||
void setup() { | void setup() { | ||
Line 87: | Line 114: | ||
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(lastButtonState); // read the input pin | + | buttonState = debounce(pushButtonPin); // read the input pin |
- | // if button goes down and before | + | // if button goes down and previously |
if (buttonState == LOW && lastButtonState == HIGH) { | if (buttonState == LOW && lastButtonState == HIGH) { | ||
- | buttonState = digitalRead(pushButtonPin); | ||
- | |||
ledState = !ledState; | ledState = !ledState; | ||
} | } | ||
Line 113: | Line 129: | ||
</ | </ | ||
- | 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 ==== | ||
Line 126: | Line 142: | ||
===== Multiple state ===== | ===== Multiple state ===== | ||
- | (multiple lights on/off) | ||
+ | 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 driven three-light blinky-thing. | ||
+ | |||
+ | <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> | ||
+ | /* | ||
+ | | ||
+ | Cycle through three LEDs with a pushnutton | ||
+ | */ | ||
+ | |||
+ | const int pushButtonPin = 2; // connect the push button to digital pin 2 | ||
+ | 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 | ||
+ | |||
+ | const int debounceTime = 5; // number of millisecods to delay after button press | ||
+ | |||
+ | int ledState; | ||
+ | boolean lastButtonState = HIGH; // value of buttonState from previous loop iteration | ||
+ | boolean buttonState = HIGH; // start by assuming button is unpressed | ||
+ | |||
+ | boolean debounce(int pinNum) { | ||
+ | delay(debounceTime); | ||
+ | return digitalRead(pinNum); | ||
+ | } | ||
+ | |||
+ | void setup() { | ||
+ | pinMode(pushButtonPin, | ||
+ | digitalWrite(pushButtonPin, | ||
+ | |||
+ | // 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() { | ||
+ | buttonState = debounce(pushButtonPin); | ||
+ | |||
+ | // if button goes down and previously it was high... | ||
+ | if (buttonState == LOW && lastButtonState == HIGH) { | ||
+ | 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, | ||
+ | } | ||
+ | | ||
+ | lastButtonState = buttonState; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Switch-case selection structure ===== | ||
+ | |||
+ | Both examples above use the //nested if-else// selection structure. Arduino' | ||
+ | |||
+ | <file c MultipleLEDsTimer2.ino> | ||
+ | /* | ||
+ | | ||
+ | Cycle through three LEDs with a timer. | ||
+ | This version uses a switch-case selection structure. | ||
+ | */ | ||
+ | |||
+ | 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 | ||
+ | switch (ledState) { | ||
+ | case 0: | ||
+ | digitalWrite(led0, | ||
+ | digitalWrite(led1, | ||
+ | digitalWrite(led2, | ||
+ | break; | ||
+ | case 1: | ||
+ | digitalWrite(led0, | ||
+ | digitalWrite(led1, | ||
+ | digitalWrite(led2, | ||
+ | break; | ||
+ | case 2: | ||
+ | digitalWrite(led0, | ||
+ | digitalWrite(led1, | ||
+ | digitalWrite(led2, | ||
+ | break; | ||
+ | default: | ||
+ | digitalWrite(led0, | ||
+ | digitalWrite(led1, | ||
+ | digitalWrite(led2, | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | </ |
arduino/arduino_crash_course/digital_input.1352141965.txt.gz · Last modified: 2012/11/05 18:59 by mithat