User:Megan Hoogenboom/assignment5

From XPUB & Lens-Based wiki
< User:Megan Hoogenboom
Revision as of 18:11, 9 November 2010 by Megan Hoogenboom (talk | contribs) (Created page with "== Counting time of button press, with Natasa == <source lang="C"> const int buttonPin = 2; // the number of the pushbutton pin const int speakerPin = 13; // the numb...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Counting time of button press, with Natasa

const int buttonPin = 2;     // the number of the pushbutton pin
const int speakerPin =  13;      // the number of the SPEAKER pin

// variables will change:
int buttonInput = 0;         // variable for reading the pushbutton status
int buttonState = 0;
int s1;
int s2;

void setup() {
  // initialize the speaker pin as an output:
  pinMode(speakerPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);  
  tone(13, 400, 10);
}

void loop(){
  // read the state of the pushbutton value:
  while (digitalRead(buttonPin) == LOW) {
  }
  
  s1 = millis();
  
  while (digitalRead(buttonPin) == HIGH) {
      s2 = millis();
    }
  Serial.println(s2 - s1, DEC);
  delay(1000);
  
}