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 05:46] – mithat | arduino:basic_interaction [2012/09/14 00:55] – mithat | ||
---|---|---|---|
Line 9: | Line 9: | ||
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 ===== |
+ | |||
+ | 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. | ||
+ | </ | ||
- | 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 off. | ||
<file c LightSwitch.ino> | <file c LightSwitch.ino> | ||
Line 19: | Line 31: | ||
*/ | */ | ||
- | // connect the push button to digital pin 2 | + | int pushButtonPin = 2; |
- | int pushButtonPin | + | int ledPin |
- | // connect the LED to pin 13 | + | |
- | int ledPin = 13; | + | |
void setup() { | void setup() { | ||
Line 33: | Line 43: | ||
// set LED state accordingly | // set LED state accordingly | ||
- | if (buttonState == HIGH) // if the button is pushed | + | if (buttonState == LOW) // if the button is pushed |
- | digitalWrite(ledPin, | + | digitalWrite(ledPin, |
- | else // otherwise | + | else |
- | digitalWrite(ledPin, | + | digitalWrite(ledPin, |
| | ||
// | // | ||
Line 52: | Line 62: | ||
*/ | */ | ||
- | // connect the push button to digital pin 2 | + | int pushButtonPin = 2; |
- | int pushButtonPin | + | int ledPin |
- | // connect the LED to pin 13 | + | |
- | int ledPin = 13; | + | |
void setup() { | void setup() { | ||
Line 64: | Line 72: | ||
void loop() { | void loop() { | ||
int buttonState = digitalRead(pushButtonPin); | int buttonState = digitalRead(pushButtonPin); | ||
- | digitalWrite(ledPin, | + | digitalWrite(ledPin, |
// | // | ||
} | } | ||
Line 77: | Line 85: | ||
*/ | */ | ||
- | // connect the push button to digital pin 2 | + | int pushButtonPin = 2; |
- | int pushButtonPin | + | int ledPin |
- | // connect the LED to pin 13 | + | |
- | int ledPin = 13; | + | |
void setup() { | void setup() { | ||
Line 88: | Line 94: | ||
void loop() { | void loop() { | ||
- | digitalWrite(ledPin, | + | digitalWrite(ledPin, |
// | // | ||
} | } | ||
</ | </ |
arduino/basic_interaction.txt · Last modified: 2012/09/14 01:19 by mithat