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
arduino:arduino_crash_course:digital_input [2012/11/05 23:15] mithatarduino:arduino_crash_course:digital_input [2017/12/06 01:13] (current) – [Digital Input] mithat
Line 3: Line 3:
 The Arduino literature refers to "digital" inputs, and while this is strictly accurate, a better name for these kinds of inputs is "Boolean" or "logical" inputs. However, we will use the more common "digital input" to avoid confusion. The Arduino literature refers to "digital" inputs, and while this is strictly accurate, a better name for these kinds of inputs is "Boolean" or "logical" inputs. However, we will use the more common "digital input" to avoid confusion.
  
-An Arduino digital input is one that responds to two different levelsanything above a certain voltage (but lower than the Arduino's supply voltage) is considered HIGH, and anything below a certain voltage (but not below 0 volts) is considered LOW. Anything in between is not defined. The threshold levels will vary with specific Arduino implementation, so to learn what they are you should consult the documentation.+An Arduino digital input is one that responds to two different levelsanything above a certain threshold is considered HIGH, and anything below a certain voltage is considered LOW. Anything in between is not defined. The threshold levels will vary with specific Arduino implementation, so to learn what they are you should consult the documentation.
  
 <WRAP center round important 60%> <WRAP center round important 60%>
Line 9: Line 9:
 </WRAP> </WRAP>
  
-Typically, digital input signals are designed so that when HIGH they have a value equal to the supply voltage and when LOW they are equal to 0 volts.+Digital output signals are typically designed so that when HIGH they have a value equal to the supply voltage and when LOW they are equal to 0 volts.
  
-We've already seen an example of Arduino digital inputs in the [[arduino:arduino_crash_course:basic_interaction|Basic Interaction]] examples. Here is the LightSwitchPullup2.ino example but using some additional variable types that better match the role of the variables.+We've already seen an example of Arduino digital inputs in the [[arduino:arduino_crash_course:basic_interaction|Basic Interaction]] examples. Here is the ''LightSwitchPullup2.ino'' example but using an additional variable type that better matches the role of the ''buttonState'' variable. Variables of type ''boolean'' can store the values ''true'' and ''false''. Since the button has two states, pushed and not pushed, using a variable that can store only two states, ''true'' and ''false'' makes sense. We have chosen to let ''true'' correspond to the button's "pushed" and ''false'' correspond to "not pushed." It is typical for variables used to store digital inputs be of type ''boolean''
 + 
 +We have also added the ''const'' modifier to the ''pushButtonPin'' and  ''ledPin'' variables to say that their values won't change in the program. In fact, when you add the ''const'' modifier to a variable declaration, the value of the variable //can't// change.
  
 <file c LightSwitchPullup2a.ino> <file c LightSwitchPullup2a.ino>
Line 68: Line 70:
   boolean buttonState = digitalRead(pushButtonPin);  // read the input pin   boolean buttonState = digitalRead(pushButtonPin);  // read the input pin
  
-  // if button goes down and before it was high...+  // if button goes down and previously it was high...
   if (buttonState == LOW && lastButtonState == HIGH) {   if (buttonState == LOW && lastButtonState == HIGH) {
     if (ledState == HIGH)            // toggle LED     if (ledState == HIGH)            // toggle LED
Line 101: Line 103:
 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 117:
  
 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
  
-  // if button goes down and before it was high...+  // if button goes down and previously it was high...
   if (buttonState == LOW && lastButtonState == HIGH) {   if (buttonState == LOW && lastButtonState == HIGH) {
     ledState = !ledState;     ledState = !ledState;
Line 151: Line 143:
 ===== Multiple state ===== ===== Multiple state =====
  
-The example above is a program that is essentially in of two states: the LED is on or off. Below are two examples where the program has multiple states. The first is a push-button driven and the second a timer driven three-light blinky-thing.+The example above is a program that is essentially in of two states: the LED is on or off. Below are two examples where the program has multiple states. The first is a timer-driven and the second a push-button driven three-light blinky-thing.
  
 +<file c MultipleLEDsTimer.ino>
 +/*
 + MultipleLEDsTimer
 + Cycle through three LEDs with a timer.
 + */
 +
 +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  
 +  if (ledState == 0)
 +  {
 +    digitalWrite(led0, HIGH);  
 +    digitalWrite(led1, LOW);  
 +    digitalWrite(led2, LOW);  
 +  }
 +  else if (ledState == 1)
 +  {
 +    digitalWrite(led0, LOW);  
 +    digitalWrite(led1, HIGH);  
 +    digitalWrite(led2, LOW);  
 +  }
 +  else if (ledState == 2)
 +  {
 +    digitalWrite(led0, LOW);  
 +    digitalWrite(led1, LOW);  
 +    digitalWrite(led2, HIGH);  
 +  }
 +  else // turn all LEDs on to indicate an error state.
 +  {
 +    digitalWrite(led0, HIGH);  
 +    digitalWrite(led1, HIGH);  
 +    digitalWrite(led2, HIGH);  
 +  }
 +}
 +</file>
 +
 +<file c MultipleLEDsPushButton.ino>
 /* /*
  MultipleLEDsPushButton  MultipleLEDsPushButton
Line 165: Line 218:
 const int debounceTime = 5;     // number of millisecods to delay after button press const int debounceTime = 5;     // number of millisecods to delay after button press
  
-int ledState = 0              // used to decide which LED is on (0 to 2)+int ledState;                   // used to decide which LED is on (0 to 2)
 boolean lastButtonState = HIGH; // value of buttonState from previous loop iteration boolean lastButtonState = HIGH; // value of buttonState from previous loop iteration
 boolean buttonState = HIGH;     // start by assuming button is unpressed boolean buttonState = HIGH;     // start by assuming button is unpressed
Line 183: Line 236:
   pinMode(led2, OUTPUT);   pinMode(led2, OUTPUT);
  
 +  // initialize state
 +  ledState = 0;
 +  
   // turn the correct LED on and the others off   // turn the correct LED on and the others off
   digitalWrite(led0, HIGH);   digitalWrite(led0, HIGH);
Line 192: Line 248:
   buttonState = debounce(pushButtonPin);  // read the input pin   buttonState = debounce(pushButtonPin);  // read the input pin
  
-  // if button goes down and before it was high...+  // if button goes down and previously it was high...
   if (buttonState == LOW && lastButtonState == HIGH) {   if (buttonState == LOW && lastButtonState == HIGH) {
     ledState = (ledState + 1) % 3;    // cycle through 0,1,2     ledState = (ledState + 1) % 3;    // cycle through 0,1,2
Line 225: Line 281:
   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>
arduino/arduino_crash_course/digital_input.1352157329.txt.gz · Last modified: 2012/11/05 23:15 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki