~ubuntu-branches/ubuntu/precise/arduino/precise

« back to all changes in this revision

Viewing changes to examples/Digital/StateChangeDetection/StateChangeDetection.pde

  • Committer: Bazaar Package Importer
  • Author(s): Scott Howard
  • Date: 2010-04-13 22:32:24 UTC
  • Revision ID: james.westby@ubuntu.com-20100413223224-jduxnd0xxnkkda02
Tags: upstream-0018+dfsg
ImportĀ upstreamĀ versionĀ 0018+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  State change detection (edge detection)
 
3
        
 
4
 Often, you don't need to know the state of a digital input all the time,
 
5
 but you just need to know when the input changes from one state to another.
 
6
 For example, you want to know when a button goes from OFF to ON.  This is called
 
7
 state change detection, or edge detection.
 
8
 
 
9
 This example shows how to detect when a button or button changes from off to on
 
10
 and on to off.
 
11
        
 
12
 The circuit:
 
13
 * pushbutton attached to pin 2 from +5V
 
14
 * 10K resistor attached to pin 2 from ground
 
15
 * LED attached from pin 13 to ground (or use the built-in LED on
 
16
   most Arduino boards)
 
17
 
 
18
 created  27 Sep 2005
 
19
 modified 30 Dec 2009
 
20
 by Tom Igoe
 
21
        
 
22
 http://arduino.cc/en/Tutorial/ButtonStateChange
 
23
 
 
24
 */
 
25
 
 
26
// this constant won't change:
 
27
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
 
28
const int ledPin = 13;       // the pin that the LED is attached to
 
29
 
 
30
// Variables will change:
 
31
int buttonPushCounter = 0;   // counter for the number of button presses
 
32
int buttonState = 0;         // current state of the button
 
33
int lastButtonState = 0;     // previous state of the button
 
34
 
 
35
void setup() {
 
36
  // initialize the button pin as a input:
 
37
  pinMode(buttonPin, INPUT);
 
38
  // initialize the LED as an output:
 
39
  pinMode(ledPin, OUTPUT);
 
40
  // initialize serial communication:
 
41
  Serial.begin(9600);
 
42
}
 
43
 
 
44
 
 
45
void loop() {
 
46
  // read the pushbutton input pin:
 
47
  buttonState = digitalRead(buttonPin);
 
48
 
 
49
  // compare the buttonState to its previous state
 
50
  if (buttonState != lastButtonState) {
 
51
    // if the state has changed, increment the counter
 
52
    if (buttonState == HIGH) {
 
53
      // if the current state is HIGH then the button
 
54
      // wend from off to on:
 
55
      buttonPushCounter++;
 
56
      Serial.println("on");
 
57
      Serial.print("number of button pushes:  ");
 
58
      Serial.println(buttonPushCounter, DEC);
 
59
    } 
 
60
    else {
 
61
      // if the current state is LOW then the button
 
62
      // wend from on to off:
 
63
      Serial.println("off"); 
 
64
    }
 
65
 
 
66
    // save the current state as the last state, 
 
67
    //for next time through the loop
 
68
    lastButtonState = buttonState;
 
69
  }
 
70
  
 
71
  // turns on the LED every four button pushes by 
 
72
  // checking the modulo of the button push counter.
 
73
  // the modulo function gives you the remainder of 
 
74
  // the division of two numbers:
 
75
  if (buttonPushCounter % 4 == 0) {
 
76
    digitalWrite(ledPin, HIGH);
 
77
  } else {
 
78
   digitalWrite(ledPin, LOW);
 
79
  }
 
80
  
 
81
}
 
82
 
 
83
 
 
84
 
 
85
 
 
86
 
 
87
 
 
88
 
 
89
 
 
90