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 revisionNext revisionBoth sides next revision | ||
arduino:arduino_crash_course:basic_interaction [2012/11/03 21:07] – mithat | arduino:arduino_crash_course:basic_interaction [2012/11/05 22:16] – [Using internal pullups] 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 ===== | ||
Line 27: | 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 34: | Line 37: | ||
void loop() { | void loop() { | ||
- | | + | buttonState = digitalRead(pushButtonPin); |
// set LED state accordingly | // set LED state accordingly | ||
Line 59: | Line 62: | ||
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 66: | Line 70: | ||
void loop() { | void loop() { | ||
- | | + | buttonState = digitalRead(pushButtonPin); |
digitalWrite(ledPin, | digitalWrite(ledPin, | ||
// | // | ||
Line 83: | Line 87: | ||
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 100: | Line 105: | ||
<code c> | <code c> | ||
- | digitalWrite(pushButtonPin, | + | digitalWrite(pushButtonPin, |
</ | </ | ||
Line 115: | Line 120: | ||
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 123: | Line 129: | ||
void loop() { | void loop() { | ||
- | | + | buttonState = digitalRead(pushButtonPin); |
// set LED state accordingly | // set LED state accordingly | ||
- | // note the inverted | + | // becasue we are using pullup resistors, |
+ | // in other words, pressed produces LOW, un-pressed produces HIGH. | ||
if (buttonState == LOW) // if the button is pushed | if (buttonState == LOW) // if the button is pushed | ||
digitalWrite(ledPin, | digitalWrite(ledPin, | ||
Line 143: | Line 150: | ||
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 151: | Line 159: | ||
void loop() { | void loop() { | ||
- | | + | buttonState = digitalRead(pushButtonPin); |
- | digitalWrite(ledPin, | + | |
+ | // Bbcasue 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