User Tools

Site Tools


arduino: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:basic_interaction [2012/09/14 01:05] mithatarduino:basic_interaction [2012/09/14 01:19] (current) mithat
Line 1: Line 1:
-====== Basic Interaction ====== +Moved [[arduino:arduino_crash_course:basic_interaction|here]].
- +
-===== 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**. +
- +
-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. +
- +
-Of the two, polling is probably easier to get started with. Following is a simple example of using polling. +
- +
-===== 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 h/w pullups or internal pullups turned on with  +
- +
-<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> +
- +
- +
- +
-<file c LightSwitch.ino> +
-/* +
- LightSwitch +
- 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, INPUT);  // make the pushbutton's pin an input +
-  pinMode(ledPin, OUTPUT);        // make LED's pin an output +
-+
- +
-void loop() { +
-  int buttonState = digitalRead(pushButtonPin);  // read the input pin +
- +
-  // set LED state accordingly +
-  if (buttonState == LOW)        // if the button is pushed +
-    digitalWrite(ledPin, HIGH);  // turn the LED on +
-  else                           // otherwise +
-    digitalWrite(ledPin, LOW);   // turn the LED off +
-     +
-  //delay(1);        // delay in between reads for stability (?) +
-+
-</file> +
- +
-Notice the use of an ''if-else'' statement. The ''if-else'' statement is an example of **flow control**. +
- +
-A more compact version of the above that eliminates the if-else statement: +
- +
-<file c LightSwitch2.ino> +
-/* +
- LightSwitch2 +
- 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, INPUT);  // make the pushbutton's pin an input +
-  pinMode(ledPin, OUTPUT);        // make LED's pin an output +
-+
- +
-void loop() { +
-  int buttonState = digitalRead(pushButtonPin);  // read the input pin +
-  digitalWrite(ledPin, !buttonState);          // turn the LED on or off +
-  //delay(1);        // delay in between reads for stability (?) +
-+
-</file> +
- +
-And an even more compact version: +
- +
-<file c LightSwitch3.ino> +
-/* +
- LightSwitch3 +
- 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, INPUT);  // make the pushbutton's pin an input +
-  pinMode(ledPin, OUTPUT);        // make LED's pin an output +
-+
- +
-void loop() { +
-  digitalWrite(ledPin, !digitalRead(pushButtonPin));  // read the input pin and turn the LED on or off +
-  //delay(1);        // delay in between reads for stability (?) +
-+
-</file>+
arduino/basic_interaction.1347584750.txt.gz · Last modified: 2012/09/14 01:05 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki