arduino:arduino_crash_course:basic_interaction
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
arduino:arduino_crash_course:basic_interaction [2012/11/03 20:16] – mithat | arduino:arduino_crash_course:basic_interaction [2015/02/13 22:28] – mithat | ||
---|---|---|---|
Line 9: | Line 9: | ||
In an interrupt scheme, the microcontroller does essentially nothing in its main loop, but it is directed to do something specific when an input source jostles it into action. | In an interrupt scheme, the microcontroller does essentially nothing in its main loop, but it is directed to do something specific when an input source jostles it into action. | ||
- | Of the two, polling is probably easier to get started with. Following is a simple example of using polling. | + | Of the two, polling is probably easier to get started with. You can use both techniques at the same time as well. |
+ | |||
+ | Following is a simple example of using polling. | ||
===== Switch-controlled LED ===== | ===== Switch-controlled LED ===== | ||
- | This example uses polling to determine the state of a switch. If the switch is pressed, Arduino will turn an LED on. If it is not pressed, it will turn the LED off. | + | The following examples use polling to determine the state of a switch. If the switch is pressed, |
- | <WRAP center round todo 60%> | + | ==== With external pulldown resistors ==== |
The following example requires **pulldown resistors** on the input switch. | The following example requires **pulldown resistors** on the input switch. | ||
- | |||
- | For use with **pullup resistors**, | ||
- | |||
- | <code c> | ||
- | digitalWrite(pushButtonPin, | ||
- | </ | ||
- | </ | ||
- | |||
<file c LightSwitch.ino> | <file c LightSwitch.ino> | ||
Line 30: | Line 24: | ||
| | ||
Turn an LED on and off. | Turn an LED on and off. | ||
- | |||
| | ||
*/ | */ | ||
Line 36: | Line 29: | ||
int pushButtonPin = 2; // connect the push button to digital pin 2 | int pushButtonPin = 2; // connect the push button to digital pin 2 | ||
int ledPin = 13; // connect the LED to pin 13 | int ledPin = 13; // connect the LED to pin 13 | ||
+ | int buttonState; | ||
void setup() { | void setup() { | ||
Line 43: | Line 37: | ||
void loop() { | void loop() { | ||
- | | + | buttonState = digitalRead(pushButtonPin); |
- | | + | |
if (buttonState == HIGH) // if the button is pushed | if (buttonState == HIGH) // if the button is pushed | ||
digitalWrite(ledPin, | digitalWrite(ledPin, | ||
Line 51: | Line 45: | ||
digitalWrite(ledPin, | digitalWrite(ledPin, | ||
- | // | + | // |
} | } | ||
- | |||
</ | </ | ||
- | Notice the use of an '' | + | Notice the use of an '' |
A more compact version of the above that eliminates the if-else statement: | A more compact version of the above that eliminates the if-else statement: | ||
Line 64: | Line 57: | ||
| | ||
Turn an LED on and off. | Turn an LED on and off. | ||
+ | | ||
*/ | */ | ||
int pushButtonPin = 2; // connect the push button to digital pin 2 | int pushButtonPin = 2; // connect the push button to digital pin 2 | ||
int ledPin = 13; // connect the LED to pin 13 | int ledPin = 13; // connect the LED to pin 13 | ||
+ | int buttonState; | ||
void setup() { | void setup() { | ||
Line 75: | Line 70: | ||
void loop() { | void loop() { | ||
- | | + | buttonState = digitalRead(pushButtonPin); |
- | digitalWrite(ledPin, | + | digitalWrite(ledPin, |
- | // | + | // |
} | } | ||
</ | </ | ||
Line 87: | Line 82: | ||
| | ||
Turn an LED on and off. | Turn an LED on and off. | ||
+ | | ||
*/ | */ | ||
int pushButtonPin = 2; // connect the push button to digital pin 2 | int pushButtonPin = 2; // connect the push button to digital pin 2 | ||
int ledPin = 13; // connect the LED to pin 13 | int ledPin = 13; // connect the LED to pin 13 | ||
+ | int buttonState; | ||
void setup() { | void setup() { | ||
Line 98: | Line 95: | ||
void loop() { | void loop() { | ||
- | digitalWrite(ledPin, | + | digitalWrite(ledPin, |
- | // | + | // |
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== With internal pullup resistors ==== | ||
+ | |||
+ | Using extenal resistors as part of switch state detection is so common that the microchip that is at the heart of the Arduino has built-in pullup resistors that can be turned on manually. Internal pullup resistors can be enabled with: | ||
+ | |||
+ | <code c> | ||
+ | digitalWrite(pushButtonPin, | ||
+ | </ | ||
+ | |||
+ | Here are the first two versions above but modified to use **internal pullup resistors**. | ||
+ | |||
+ | |||
+ | <file c LightSwitchPullup.ino> | ||
+ | /* | ||
+ | | ||
+ | Turn an LED on and off. | ||
+ | | ||
+ | */ | ||
+ | |||
+ | int pushButtonPin = 2; // connect the push button to digital pin 2 | ||
+ | int ledPin = 13; // connect the LED to pin 13 | ||
+ | int buttonState; | ||
+ | |||
+ | void setup() { | ||
+ | pinMode(pushButtonPin, | ||
+ | digitalWrite(pushButtonPin, | ||
+ | pinMode(ledPin, | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | buttonState = digitalRead(pushButtonPin); | ||
+ | |||
+ | // set LED state accordingly | ||
+ | // becasue we are using pullup resistors, the logic is inverted; | ||
+ | // in other words, pressed produces LOW, un-pressed produces HIGH. | ||
+ | if (buttonState == LOW) // if the button is pushed | ||
+ | digitalWrite(ledPin, | ||
+ | else // otherwise | ||
+ | digitalWrite(ledPin, | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <file c LightSwitchPullup2.ino> | ||
+ | /* | ||
+ | | ||
+ | Turn an LED on and off. | ||
+ | | ||
+ | */ | ||
+ | |||
+ | int pushButtonPin = 2; // connect the push button to digital pin 2 | ||
+ | int ledPin = 13; // connect the LED to pin 13 | ||
+ | int buttonState; | ||
+ | |||
+ | void setup() { | ||
+ | pinMode(pushButtonPin, | ||
+ | digitalWrite(pushButtonPin, | ||
+ | pinMode(ledPin, | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | buttonState = digitalRead(pushButtonPin); | ||
+ | |||
+ | // set LED state accordingly | ||
+ | // becasue we are using pullup resistors, the logic is inverted; | ||
+ | // in other words, pressed produces LOW, un-pressed produces HIGH. | ||
+ | digitalWrite(ledPin, | ||
} | } | ||
</ | </ |
arduino/arduino_crash_course/basic_interaction.txt · Last modified: 2017/12/06 01:05 by mithat