Tutorial.Switch History

Hide minor edits - Show changes to markup

June 07, 2007, at 06:58 PM by David A. Mellis - removing unused debug lines
Deleted lines 40-42:
  if (DEBUG)
    Serial.begin(19200);
January 28, 2007, at 12:01 PM by David A. Mellis -
Changed lines 3-4 from:

switch

to:

Switch

January 28, 2007, at 11:39 AM by David A. Mellis -
Changed lines 1-2 from:

Using a pushbutton as a switch

to:

Examples > Digital I/O

switch

January 28, 2007, at 11:38 AM by David A. Mellis -
Changed lines 13-14 from:

[@/* switch

to:

[@ /* switch

Changed lines 19-20 from:
 * noise).  If DEBUG is true, each time the output pin is toggled, the time
 * (in milliseconds) and the pin's new state is sent to the computer.
to:
 * noise).  
Deleted lines 24-25:

int DEBUG = 0; // set to 1 to enable debugging output to the computer

Changed lines 59-66 from:
    time = millis();

    if (DEBUG) {
      Serial.print(time);
      Serial.print(" ");
      Serial.print(state);
      Serial.println();
    }
to:
    time = millis();    
Changed line 66 from:
 @]
to:

@]

January 28, 2007, at 11:37 AM by David A. Mellis -
Added lines 9-10:
December 26, 2006, at 07:03 AM by David A. Mellis -
Changed line 11 from:

[=/* switch

to:

[@/* switch

Changed line 73 from:
 =]
to:
 @]
December 26, 2006, at 07:03 AM by David A. Mellis -
Added lines 5-8:

Circuit

A push-button on pin 2 and an LED on pin 13.

Changed lines 11-12 from:
 [=

/* switch

to:

[=/* switch

December 26, 2006, at 07:01 AM by David A. Mellis -
Added lines 1-70:

Using a pushbutton as a switch

This example demonstrates the use of a pushbutton as a switch: each time you press the button, the LED (or whatever) is turned on (if it's off) or off (if on). It also debounces the input, without which pressing the button once would appear to the code as multiple presses.

Code

 
/* switch
 * 
 * Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
 * press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
 * a minimum delay between toggles to debounce the circuit (i.e. to ignore
 * noise).  If DEBUG is true, each time the output pin is toggled, the time
 * (in milliseconds) and the pin's new state is sent to the computer.
 *
 * David A. Mellis
 * 21 November 2006
 */

int DEBUG = 0;         // set to 1 to enable debugging output to the computer

int inPin = 2;         // the number of the input pin
int outPin = 13;       // the number of the output pin

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  if (DEBUG)
    Serial.begin(19200);

  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
}

void loop()
{
  reading = digitalRead(inPin);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();

    if (DEBUG) {
      Serial.print(time);
      Serial.print(" ");
      Serial.print(state);
      Serial.println();
    }
  }

  digitalWrite(outPin, state);

  previous = reading;
}
 

Share