jappy

Simple Arduino Button Logger

Oct 23rd, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.91 KB | None | 0 0
  1. // digital pin 2 has a pushbutton attached to it. Give it a name:
  2. int pushButton = 2;
  3.  
  4. // the setup routine runs once when you press reset:
  5. void setup() {
  6.   // initialize serial communication at 9600 bits per second:
  7.   Serial.begin(9600);
  8.   // make the pushbutton's pin an input:
  9.   pinMode(pushButton, INPUT);
  10. }
  11.  
  12. int lastButtonState = 0;
  13. unsigned long lastTime = millis();
  14. unsigned long startTime = lastTime;
  15.  
  16. // the loop routine runs over and over again forever:
  17. void loop() {
  18.   // read the input pin:
  19.   int buttonState = digitalRead(pushButton);
  20.   unsigned long time = millis();
  21.   unsigned long diff = time - lastTime;
  22.   if (buttonState != lastButtonState && diff > 200) {
  23.     // print out the state of the button:
  24.     if (buttonState == 1) {
  25.       Serial.println((time - startTime) / 1000.0);
  26.     }
  27.     lastButtonState = buttonState;
  28.     delay(50);        // delay in between reads for stability
  29.   }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment