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
Next revisionBoth sides next revision
arduino:arduino_crash_course:basic_interaction [2012/11/03 21:12] – [Polling versus interrupts] mithatarduino:arduino_crash_course:basic_interaction [2012/11/05 22:16] – [Using internal pullups] mithat
Line 29: Line 29:
 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 36: Line 37:
  
 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
Line 61: Line 62:
 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 68: Line 70:
  
 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 in between reads for stability (?)
Line 85: Line 87:
 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 117: Line 120:
 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 125: Line 129:
  
 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 145: Line 150:
 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 153: Line 159:
  
 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+  
 +  // Bbcasue 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.txt · Last modified: 2017/12/06 01:05 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki