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

Next revision
Previous revision
arduino:arduino_crash_course:basic_interaction [2012/09/14 01:17] – created mithatarduino:arduino_crash_course:basic_interaction [2017/12/06 01:05] (current) mithat
Line 3: Line 3:
 ===== Polling versus interrupts ===== ===== Polling versus interrupts =====
  
-There are two primary ways that a microcontroller (like the Arduino) can respond to changes in its inputs. One is by **polling**, the other is by **interrupts**.+There are two common techniques used to make a microcontroller (like the Arduino) respond to changes in its inputs. One is **polling**, the other is **interrupts**.
  
-In a polling setup, the mircocontoller explicitly examines all its input sources during its main loop to see what state each one is in and then it responds accordingly. In an interrupt scheme, the microcontroller does essentially nothing in its main loop and gets instructed to do something when an input source jostles it into action.+In a polling setup, the mircocontoller explicitly examines all its input sources during its main loop to see what state each input is inand then it responds accordingly.
  
-Of the twopolling is probably easier to get started with. Following is a simple example of using polling.+In an interrupt scheme, the microcontroller does essentially nothing in its main loopbut it is directed to do something specific when an input source jostles it into action.
  
-===== Switch-controlled LED =====+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.
  
-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. +===== Polling exampleswitch-controlled LED =====
- +
-<WRAP center round important 60%> +
-The following examples will need switches with external pullups or internal pullups turned on via:  +
- +
-<code c> +
-digitalWrite(pushButtonPin, HIGH);  // turn on pullup resistors +
-</code> +
- +
-I suspect internal pullups is the better way to go but possibly harder to explain. +
- +
-Alternately, use external pulldowns and keep the logic uninverted. +
-</WRAP>+
  
 +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.
  
 +==== With external pulldown resistors ====
 +The following example requires **pulldown resistors** on the input switch.
  
 <file c LightSwitch.ino> <file c LightSwitch.ino>
Line 31: Line 22:
  LightSwitch  LightSwitch
  Turn an LED on and off.  Turn an LED on and off.
 + Requires pulldown resistor on input.
  */  */
  
 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 42: 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 == LOW       // 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
   else                           // otherwise   else                           // otherwise
     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 62: Line 55:
  LightSwitch2  LightSwitch2
  Turn an LED on and off.  Turn an LED on and off.
 + Requires pulldown resistor on input.
  */  */
  
 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 73: 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 85: Line 80:
  LightSwitch3  LightSwitch3
  Turn an LED on and off.  Turn an LED on and off.
 + Requires pulldown resistor on input.
  */  */
  
 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 96: Line 93:
  
 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> 
 + 
 +==== 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: 
 + 
 +<code c> 
 +digitalWrite(pushButtonPin, HIGH); 
 +</code> 
 + 
 +Here are the first two versions above but modified to use **internal pullup resistors**. 
 + 
 + 
 +<file c LightSwitchPullup.ino> 
 +/* 
 + LightSwitchPullup 
 + Turn an LED on and off. 
 + Internal pullup version. 
 + */ 
 + 
 +int pushButtonPin = 2;  // connect the push button to digital pin 2 
 +int ledPin = 13;        // connect the LED to pin 13 
 +int buttonState;        // stores current button state  
 + 
 +void setup() { 
 +  pinMode(pushButtonPin, INPUT);      // make the pushbutton's pin an input 
 +  digitalWrite(pushButtonPin, HIGH);  // turn on internal pullup resistors 
 +  pinMode(ledPin, OUTPUT);            // make LED's pin an output 
 +
 + 
 +void loop() { 
 +  buttonState = digitalRead(pushButtonPin);  // read the input pin 
 + 
 +  // set LED state accordingly 
 +  // 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 
 +    digitalWrite(ledPin, HIGH);  // turn the LED on 
 +  else                           // otherwise 
 +    digitalWrite(ledPin, LOW);   // turn the LED off 
 +
 +</file> 
 + 
 +<file c LightSwitchPullup2.ino> 
 +/* 
 + LightSwitchPullup2 
 + Turn an LED on and off. 
 + Internal pullup version. 
 + */ 
 + 
 +int pushButtonPin = 2;  // connect the push button to digital pin 2 
 +int ledPin = 13;        // connect the LED to pin 13 
 +int buttonState;        // stores current button state  
 + 
 +void setup() { 
 +  pinMode(pushButtonPin, INPUT);  // make the pushbutton's pin an input 
 +  digitalWrite(pushButtonPin, HIGH);  // turn on internal pullup resistors 
 +  pinMode(ledPin, OUTPUT);        // make LED's pin an output 
 +
 + 
 +void loop() { 
 +  buttonState = digitalRead(pushButtonPin);  // read the input pin 
 +  
 +  // 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.1347585470.txt.gz · Last modified: 2012/09/14 01:17 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki