User Tools

Site Tools


arduino:arduino_crash_course:basic_interaction

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
arduino:arduino_crash_course:basic_interaction [2012/11/03 21:10] – [Using internal pullups] mithatarduino:arduino_crash_course:basic_interaction [2017/12/06 01:05] (current) mithat
Line 9: Line 9:
 In an interrupt scheme, the microcontroller does essentially nothing in its main loop, but it is directed to do something specific when an input source jostles it into action. In an interrupt scheme, the microcontroller does essentially nothing in its main loop, but it is directed to do something specific when an input source jostles it into action.
  
-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. We won't learn how to work with interrupts in this section, but it's good to know that you can use both techniques in the same program.
  
-===== Switch-controlled LED =====+===== Polling example: 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, the Arduino will turn an LED on. If it is not pressed, it will turn the LED off.
  
-==== 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 27: Line 27:
 int pushButtonPin = 2;  // connect the push button to digital pin 2 int pushButtonPin = 2;  // connect the push button to digital pin 2
 int ledPin = 13;        // connect the LED to pin 13 int ledPin = 13;        // connect the LED to pin 13
 +int buttonState;        // stores current button state 
  
 void setup() { void setup() {
Line 34: Line 35:
  
 void loop() { void loop() {
-  int buttonState = digitalRead(pushButtonPin);  // read the input pin+  buttonState = digitalRead(pushButtonPin);  // read the input pin
  
-    // set LED state accordingly+  // set LED state accordingly
   if (buttonState == HIGH)       // if the button is pushed   if (buttonState == HIGH)       // if the button is pushed
     digitalWrite(ledPin, HIGH);  // turn the LED on     digitalWrite(ledPin, HIGH);  // turn the LED on
Line 42: Line 43:
     digitalWrite(ledPin, LOW);   // turn the LED off     digitalWrite(ledPin, LOW);   // turn the LED off
  
-  //delay(1);        // delay in between reads for stability (?)+  //delay(1);        // delay between reads for stability (?? I don't remember why this got in here.)
 } }
 </file> </file>
  
-Notice the use of an ''if-else'' statement. The ''if-else'' statement is an example of **flow control**.+Notice the use of an ''if-else'' statement. The ''if-else'' statement is an example of **control flow**.
  
 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 59: Line 60:
 int pushButtonPin = 2;  // connect the push button to digital pin 2 int pushButtonPin = 2;  // connect the push button to digital pin 2
 int ledPin = 13;        // connect the LED to pin 13 int ledPin = 13;        // connect the LED to pin 13
 +int buttonState;        // stores current button state 
  
 void setup() { void setup() {
Line 66: Line 68:
  
 void loop() { void loop() {
-  int buttonState = digitalRead(pushButtonPin);  // read the input pin+  buttonState = digitalRead(pushButtonPin);  // read the input pin
   digitalWrite(ledPin, buttonState);             // turn the LED on or off   digitalWrite(ledPin, buttonState);             // turn the LED on or off
-  //delay(1);        // delay in between reads for stability (?)+  //delay(1);        // delay between reads for stability (?? I don't remember why this got in here.)
 } }
 </file> </file>
Line 83: Line 85:
 int pushButtonPin = 2;  // connect the push button to digital pin 2 int pushButtonPin = 2;  // connect the push button to digital pin 2
 int ledPin = 13;        // connect the LED to pin 13 int ledPin = 13;        // connect the LED to pin 13
 +int buttonState;        // stores current button state 
  
 void setup() { void setup() {
Line 91: Line 94:
 void loop() { void loop() {
   digitalWrite(ledPin, digitalRead(pushButtonPin));  // read the input pin and turn the LED on or off   digitalWrite(ledPin, digitalRead(pushButtonPin));  // read the input pin and turn the LED on or off
-  //delay(1);        // delay in between reads for stability (?)+  //delay(1);        // delay between reads for stability (?? I don't remember why this got in here.)
 } }
 </file> </file>
  
-==== Using internal pullups ====+==== With internal pullup resistors ====
  
 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 115: Line 118:
 int pushButtonPin = 2;  // connect the push button to digital pin 2 int pushButtonPin = 2;  // connect the push button to digital pin 2
 int ledPin = 13;        // connect the LED to pin 13 int ledPin = 13;        // connect the LED to pin 13
 +int buttonState;        // stores current button state 
  
 void setup() { void setup() {
Line 123: Line 127:
  
 void loop() { void loop() {
-  int buttonState = digitalRead(pushButtonPin);  // read the input pin+  buttonState = digitalRead(pushButtonPin);  // read the input pin
  
   // set LED state accordingly   // set LED state accordingly
-  // note the inverted logic+  // becasue we are using pullup resistors, the logic is inverted; 
 +  // 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, HIGH);  // turn the LED on     digitalWrite(ledPin, HIGH);  // turn the LED on
Line 143: Line 148:
 int pushButtonPin = 2;  // connect the push button to digital pin 2 int pushButtonPin = 2;  // connect the push button to digital pin 2
 int ledPin = 13;        // connect the LED to pin 13 int ledPin = 13;        // connect the LED to pin 13
 +int buttonState;        // stores current button state 
  
 void setup() { void setup() {
Line 151: Line 157:
  
 void loop() { void loop() {
-  int buttonState = !digitalRead(pushButtonPin);  // read the input pin +  buttonState = digitalRead(pushButtonPin);  // read the input pin 
-  digitalWrite(ledPin, buttonState);              // turn the LED on or off+  
 +  // 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, !buttonState);        // turn the LED on or off
 } }
 </file> </file>
arduino/arduino_crash_course/basic_interaction.1351977040.txt.gz · Last modified: 2012/11/03 21:10 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki