User Tools

Site Tools


arduino:arduino_crash_course:digital_input

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:digital_input [2012/11/05 23:33] mithatarduino:arduino_crash_course:digital_input [2012/11/12 18:29] – [Software debouncing] mithat
Line 101: Line 101:
 boolean lastButtonState = HIGH; // value of buttonState from previous loop iteration boolean lastButtonState = HIGH; // value of buttonState from previous loop iteration
 boolean buttonState = HIGH; boolean buttonState = HIGH;
- 
-/* boolean debounce(int pinNum, boolean lastState) { 
-  boolean nowState = digitalRead(pinNum); 
-  if (lastState != nowState) { 
-    delay(debounceTime); 
-    nowState = digitalRead(pinNum); 
-  } 
-  return nowState; 
-} */ 
  
 boolean debounce(int pinNum) { boolean debounce(int pinNum) {
Line 124: Line 115:
  
 void loop() { void loop() {
-//  buttonState = debounce(pushButtonPin, lastButtonState);  // read the input pin 
   buttonState = debounce(pushButtonPin);  // read the input pin   buttonState = debounce(pushButtonPin);  // read the input pin
  
Line 288: Line 278:
      
   lastButtonState = buttonState;   lastButtonState = buttonState;
 +}
 +</file>
 +
 +===== Switch-case selection structure =====
 +
 +Both examples above use the //nested if-else// selection structure. Arduino's programming language also includes another selection structure that is useful in these situations: the //switch-case// structure. Below we have rewritten MultipleLEDsPushButton to use a //swich-case// structure.
 +
 +<file c MultipleLEDsTimer2.ino>
 +/*
 + MultipleLEDsTimer2
 + Cycle through three LEDs with a timer.
 + This version uses a switch-case selection structure.
 + */
 +
 +const int delayTime = 500;  // time to wait between LED changes
 +const int led0 = 13;        // connect an LED to pin 13
 +const int led1 = 12;        // connect an LED to pin 12
 +const int led2 = 11;        // connect an LED to pin 11
 +
 +int ledState;               // used to decide which LED is on (0 to 2)
 +
 +void setup() {
 +  // make LED pins outputs
 +  pinMode(led0, OUTPUT);          
 +  pinMode(led1, OUTPUT);
 +  pinMode(led2, OUTPUT);
 +
 +  // initialize state
 +  ledState = 0;
 +
 +  // turn the correct LED on and the others off
 +  digitalWrite(led0, HIGH);
 +  digitalWrite(led1, LOW);
 +  digitalWrite(led2, LOW);
 +}
 +
 +void loop() {
 +  delay(delayTime);
 +  ledState = (ledState + 1) % 3;    // cycle through 0,1,2
 +
 +  // turn the correct LED on and the others off  
 +  switch (ledState) {
 +    case 0:
 +      digitalWrite(led0, HIGH);  
 +      digitalWrite(led1, LOW);  
 +      digitalWrite(led2, LOW);        
 +      break;
 +    case 1:
 +      digitalWrite(led0, LOW);  
 +      digitalWrite(led1, HIGH);  
 +      digitalWrite(led2, LOW);      
 +      break;
 +    case 2:
 +      digitalWrite(led0, LOW);  
 +      digitalWrite(led1, LOW);  
 +      digitalWrite(led2, HIGH);       
 +      break;
 +    default: 
 +      digitalWrite(led0, HIGH);  
 +      digitalWrite(led1, HIGH);  
 +      digitalWrite(led2, HIGH); 
 +      break;
 +  }
 } }
 </file> </file>
arduino/arduino_crash_course/digital_input.txt · Last modified: 2017/12/06 01:13 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki