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/03 20:51] – mithat | arduino:arduino_crash_course:basic_interaction [2012/11/03 21:10] – [Using internal pullups] mithat | ||
---|---|---|---|
Line 13: | 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 todo 60%> | + | ==== Using external pulldown resistors ==== |
The following example requires **pulldown resistors** on the input switch. | The following example requires **pulldown resistors** on the input switch. | ||
- | |||
- | For use with **pullup resistors**, | ||
- | |||
- | <code c> | ||
- | digitalWrite(pushButtonPin, | ||
- | </ | ||
- | </ | ||
- | |||
<file c LightSwitch.ino> | <file c LightSwitch.ino> | ||
Line 52: | Line 44: | ||
// | // | ||
} | } | ||
- | |||
</ | </ | ||
Line 105: | Line 96: | ||
==== Using internal pullups ==== | ==== 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**. | Here are the first two versions above but modified to use **internal pullup resistors**. | ||
- | <file c LightSwitch.ino> | + | <file c LightSwitchPullup.ino> |
/* | /* | ||
- | LightSwitch | + | LightSwitchPullup |
Turn an LED on and off. | Turn an LED on and off. | ||
| | ||
Line 134: | Line 131: | ||
else // otherwise | else // otherwise | ||
digitalWrite(ledPin, | 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