arduino: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:basic_interaction [2012/09/13 04:58] – [Polling versus interrupts] mithat | arduino:basic_interaction [2012/09/14 00:45] – mithat | ||
---|---|---|---|
Line 5: | Line 5: | ||
There are two primary ways that a microcontroller (like the Arduino) can respond to changes in its inputs. One is by **polling**, | There are two primary ways that a microcontroller (like the Arduino) can respond to changes in its inputs. One is by **polling**, | ||
- | With a polling setup, the mircocontoller | + | In a polling setup, the mircocontoller explicitly |
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. | ||
- | ===== Switch controlled LED ===== | + | ===== Switch-controlled LED ===== |
- | TODO | + | 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. |
+ | <WRAP center round important 60%> | ||
+ | The following examples will need switches with pullups or internal pullups turned on with | ||
+ | <code c> | ||
+ | digitalWrite(pushButtonPin, | ||
+ | </ | ||
+ | |||
+ | I suspect internal pullups is the better way to go but possibly harder to explain. | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | <file c LightSwitch.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, | ||
+ | pinMode(ledPin, | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | int buttonState = digitalRead(pushButtonPin); | ||
+ | |||
+ | // set LED state accordingly | ||
+ | if (buttonState == HIGH) // if the button is pushed | ||
+ | digitalWrite(ledPin, | ||
+ | else // otherwise | ||
+ | digitalWrite(ledPin, | ||
+ | | ||
+ | // | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Notice the use of an '' | ||
+ | |||
+ | A more compact version of the above that eliminates the if-else statement: | ||
+ | |||
+ | <file c LightSwitch2.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, | ||
+ | pinMode(ledPin, | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | int buttonState = digitalRead(pushButtonPin); | ||
+ | digitalWrite(ledPin, | ||
+ | // | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | And an even more compact version: | ||
+ | |||
+ | <file c LightSwitch3.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, | ||
+ | pinMode(ledPin, | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | digitalWrite(ledPin, | ||
+ | // | ||
+ | } | ||
+ | </ |
arduino/basic_interaction.txt · Last modified: 2012/09/14 01:19 by mithat