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/13 05:18] 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 inputsOne is by **polling**, the other is by **interrupts**. +
- +
-With a polling setup, the mircocontoller constantly and repeatedly looks explicitly at all its input sources to see what state they are in and then it responds accordingly. In an interrupt scheme, the microcontroller does essentially nothing most of the time but comes to life and does 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 off. +
- +
-<file c LightSwitch.ino> +
-/* +
- LightSwitch +
- Turn an LED on and off. +
- */ +
- +
-// connect the push button to digital pin 2 +
-int pushButton = 2; +
-// connect the LED to pin 13 +
-int ledPin = 13; +
- +
-void setup() { +
-  pinMode(pushButton, INPUT);   // make the pushbutton's pin an input +
-  pinMode(ledPin, OUTPUT);      // make LED's pin an output +
-+
- +
-void loop() { +
-  // read the input pin: +
-  int buttonState = digitalRead(pushButton); +
-  // set LED state accordingly +
-  if (buttonState == 1)           // '0' means "not pressed", 1 means "pressed" +
-    digitalWrite(ledPin, HIGH);   // turn the LED on +
-  else +
-    digitalWrite(ledPin, LOW);    // turn the LED off +
-     +
-  //delay(1);        // delay in between reads for stability (?) +
-+
-</file>+
arduino/basic_interaction.1347513497.txt.gz · Last modified: 2012/09/13 05:18 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki