User:Natasa Siencnik/prototyping/counting

From XPUB & Lens-Based wiki

Counting time of button press

together with Megan

SKETCH Counter.jpg

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);
  
}