arduino:arduino_crash_course:basic_interaction
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
arduino:arduino_crash_course:basic_interaction [2012/09/14 01:17] – created mithat | arduino:arduino_crash_course:basic_interaction [2012/11/03 21:10] – [Using internal pullups] mithat | ||
---|---|---|---|
Line 3: | Line 3: | ||
===== Polling versus interrupts ===== | ===== Polling versus interrupts ===== | ||
- | There are two primary ways that a microcontroller (like the Arduino) | + | There are two common techniques used to make a microcontroller (like the Arduino) respond to changes in its inputs. One is **polling**, |
- | In a polling setup, the mircocontoller explicitly examines all its input sources during its main loop to see what state each one is in and then it responds accordingly. In an interrupt scheme, the microcontroller does essentially nothing in its main loop and gets instructed | + | In a polling setup, the mircocontoller explicitly examines all its input sources during its main loop to see what state each input is in, and then it responds accordingly. |
+ | |||
+ | In an interrupt scheme, the microcontroller does essentially nothing in its main loop, but it is directed | ||
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. Following is a simple example of using polling. | ||
Line 11: | Line 13: | ||
===== 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, Arduino will turn an LED on. If it is not pressed, it will turn the LED off. |
- | + | ||
- | <WRAP center round important 60%> | + | |
- | The following examples will need switches with external pullups or internal pullups turned on via: | + | |
- | + | ||
- | <code c> | + | |
- | digitalWrite(pushButtonPin, | + | |
- | </ | + | |
- | + | ||
- | I suspect internal pullups is the better way to go but possibly harder to explain. | + | |
- | + | ||
- | Alternately, | + | |
- | </ | + | |
+ | ==== Using external pulldown resistors ==== | ||
+ | The following example requires **pulldown resistors** on the input switch. | ||
<file c LightSwitch.ino> | <file c LightSwitch.ino> | ||
Line 31: | Line 22: | ||
| | ||
Turn an LED on and off. | Turn an LED on and off. | ||
+ | | ||
*/ | */ | ||
Line 44: | Line 36: | ||
int buttonState = digitalRead(pushButtonPin); | int buttonState = digitalRead(pushButtonPin); | ||
- | | + | |
- | if (buttonState == LOW) // if the button is pushed | + | if (buttonState == HIGH) |
digitalWrite(ledPin, | digitalWrite(ledPin, | ||
else // otherwise | else // otherwise | ||
digitalWrite(ledPin, | digitalWrite(ledPin, | ||
- | | + | |
// | // | ||
} | } | ||
Line 62: | Line 54: | ||
| | ||
Turn an LED on and off. | Turn an LED on and off. | ||
+ | | ||
*/ | */ | ||
Line 74: | Line 67: | ||
void loop() { | void loop() { | ||
int buttonState = digitalRead(pushButtonPin); | int buttonState = digitalRead(pushButtonPin); | ||
- | digitalWrite(ledPin, | + | digitalWrite(ledPin, |
// | // | ||
} | } | ||
Line 85: | Line 78: | ||
| | ||
Turn an LED on and off. | Turn an LED on and off. | ||
+ | | ||
*/ | */ | ||
Line 96: | Line 90: | ||
void loop() { | void loop() { | ||
- | digitalWrite(ledPin, | + | digitalWrite(ledPin, |
// | // | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Using internal pullups ==== | ||
+ | |||
+ | 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 | ||
+ | |||
+ | void setup() { | ||
+ | pinMode(pushButtonPin, | ||
+ | digitalWrite(pushButtonPin, | ||
+ | pinMode(ledPin, | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | int buttonState = digitalRead(pushButtonPin); | ||
+ | |||
+ | // set LED state accordingly | ||
+ | // note the inverted logic | ||
+ | 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 | ||
+ | |||
+ | void setup() { | ||
+ | pinMode(pushButtonPin, | ||
+ | digitalWrite(pushButtonPin, | ||
+ | pinMode(ledPin, | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | int buttonState = !digitalRead(pushButtonPin); | ||
+ | digitalWrite(ledPin, | ||
} | } | ||
</ | </ |
arduino/arduino_crash_course/basic_interaction.txt · Last modified: 2017/12/06 01:05 by mithat