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:44] – [Polling versus interrupts] mithat | arduino:basic_interaction [2012/09/14 00:43] – 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 pushButton | + | int ledPin |
- | // connect the LED to pin 13 | + | |
- | int ledPin = 13; | + | |
void setup() { | void setup() { | ||
- | pinMode(pushButton, INPUT); | + | pinMode(pushButtonPin, INPUT); |
- | pinMode(ledPin, | + | pinMode(ledPin, |
} | } | ||
void loop() { | void loop() { | ||
- | | + | int buttonState = digitalRead(pushButtonPin); // read the input pin |
- | | + | |
// set LED state accordingly | // set LED state accordingly | ||
Line 53: | Line 62: | ||
*/ | */ | ||
- | // connect the push button to digital pin 2 | + | int pushButtonPin = 2; |
- | int pushButton | + | int ledPin |
- | // connect the LED to pin 13 | + | |
- | int ledPin = 13; | + | |
void setup() { | void setup() { | ||
- | pinMode(pushButton, INPUT); | + | pinMode(pushButtonPin, INPUT); |
- | pinMode(ledPin, | + | pinMode(ledPin, |
} | } | ||
void loop() { | void loop() { | ||
- | int buttonState = digitalRead(pushButton); // read the input pin | + | int buttonState = digitalRead(pushButtonPin); // read the input pin |
digitalWrite(ledPin, | digitalWrite(ledPin, | ||
// | // | ||
Line 78: | Line 85: | ||
*/ | */ | ||
- | // connect the push button to digital pin 2 | + | int pushButtonPin = 2; |
- | int pushButton | + | int ledPin |
- | // connect the LED to pin 13 | + | |
- | int ledPin = 13; | + | |
void setup() { | void setup() { | ||
- | pinMode(pushButton, INPUT); | + | pinMode(pushButtonPin, INPUT); |
- | pinMode(ledPin, | + | pinMode(ledPin, |
} | } | ||
void loop() { | void loop() { | ||
- | digitalWrite(ledPin, | + | digitalWrite(ledPin, |
// | // | ||
} | } | ||
</ | </ |
arduino/basic_interaction.txt · Last modified: 2012/09/14 01:19 by mithat