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/05 22:13] – mithat | arduino:arduino_crash_course:basic_interaction [2015/02/13 22:27] – mithat | ||
---|---|---|---|
Line 15: | Line 15: | ||
===== Switch-controlled LED ===== | ===== Switch-controlled LED ===== | ||
- | 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. | + | The following examples use polling to determine the state of a switch. If the switch is pressed, |
- | ==== Using external pulldown resistors ==== | + | ==== With external pulldown resistors ==== |
The following example requires **pulldown resistors** on the input switch. | The following example requires **pulldown resistors** on the input switch. | ||
Line 39: | Line 39: | ||
buttonState = digitalRead(pushButtonPin); | buttonState = digitalRead(pushButtonPin); | ||
- | | + | |
if (buttonState == HIGH) // if the button is pushed | if (buttonState == HIGH) // if the button is pushed | ||
digitalWrite(ledPin, | digitalWrite(ledPin, | ||
Line 45: | Line 45: | ||
digitalWrite(ledPin, | digitalWrite(ledPin, | ||
- | // | + | // |
} | } | ||
</ | </ | ||
- | Notice the use of an '' | + | Notice the use of an '' |
A more compact version of the above that eliminates the if-else statement: | A more compact version of the above that eliminates the if-else statement: | ||
Line 100: | Line 100: | ||
</ | </ | ||
- | ==== Using internal | + | ==== With internal |
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: | 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: | ||
Line 132: | Line 132: | ||
// 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 158: | Line 159: | ||
void loop() { | void loop() { | ||
- | buttonState = !digitalRead(pushButtonPin); | + | buttonState = digitalRead(pushButtonPin); |
- | digitalWrite(ledPin, | + | |
+ | // set LED state accordingly | ||
+ | // becasue 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