Advertisement
talofer99

Total Time Used in Millis

Feb 22nd, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.14 KB | None | 0 0
  1. /*
  2.   Debounce
  3.  
  4.   Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
  5.   press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's a
  6.   minimum delay between toggles to debounce the circuit (i.e. to ignore noise).
  7.  
  8.   The circuit:
  9.   - LED attached from pin 13 to ground
  10.   - pushbutton attached from pin 2 to +5V
  11.   - 10 kilohm resistor attached from pin 2 to ground
  12.  
  13.   - Note: On most Arduino boards, there is already an LED on the board connected
  14.     to pin 13, so you don't need any extra components for this example.
  15.  
  16.   created 21 Nov 2006
  17.   by David A. Mellis
  18.   modified 30 Aug 2011
  19.   by Limor Fried
  20.   modified 28 Dec 2012
  21.   by Mike Walters
  22.   modified 30 Aug 2016
  23.   by Arturo Guadalupi
  24.  
  25.   This example code is in the public domain.
  26.  
  27.   http://www.arduino.cc/en/Tutorial/Debounce
  28. */
  29.  
  30. // constants won't change. They're used here to set pin numbers:
  31. const int buttonPin = 2;    // the number of the pushbutton pin
  32. const int ledPin = 13;      // the number of the LED pin
  33.  
  34. // Variables will change:
  35. int ledState = HIGH;         // the current state of the output pin
  36. int buttonState;             // the current reading from the input pin
  37. int lastButtonState = LOW;   // the previous reading from the input pin
  38.  
  39. // the following variables are unsigned longs because the time, measured in
  40. // milliseconds, will quickly become a bigger number than can be stored in an int.
  41. unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
  42. unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
  43.  
  44.  
  45. // I ADDED THIS
  46. unsigned long useStartMillis;
  47. unsigned long totoalUseMillis;
  48.  
  49.  
  50. void setup() {
  51.   pinMode(buttonPin, INPUT_PULLUP);
  52.   pinMode(ledPin, OUTPUT);
  53.  
  54.   // set initial LED state
  55.   digitalWrite(ledPin, ledState);
  56. }
  57.  
  58. void loop() {
  59.   // read the state of the switch into a local variable:
  60.   int reading = digitalRead(buttonPin);
  61.  
  62.   // check to see if you just pressed the button
  63.   // (i.e. the input went from LOW to HIGH), and you've waited long enough
  64.   // since the last press to ignore any noise:
  65.  
  66.   // If the switch changed, due to noise or pressing:
  67.   if (reading != lastButtonState) {
  68.     // reset the debouncing timer
  69.     lastDebounceTime = millis();
  70.   }
  71.  
  72.   if ((millis() - lastDebounceTime) > debounceDelay) {
  73.     // whatever the reading is at, it's been there for longer than the debounce
  74.     // delay, so take it as the actual current state:
  75.  
  76.     // if the button state has changed:
  77.     if (reading != buttonState) {
  78.       // START ....
  79.       if (!reading) {
  80.         useStartMillis = millis()
  81.       } else {
  82.         //END
  83.         totoalUseMillis += millis() - useStartMillis;
  84.         // NOW IF YOU WANTED TO SHOW USE IN SECONDES YOU DO :
  85.         // totoalUseMillis/1000
  86.       }
  87.       buttonState = reading;
  88.  
  89.       // only toggle the LED if the new button state is HIGH
  90.       if (buttonState == HIGH) {
  91.         ledState = !ledState;
  92.       }
  93.     }
  94.   }
  95.  
  96.   // set the LED:
  97.   digitalWrite(ledPin, ledState);
  98.  
  99.   // save the reading. Next time through the loop, it'll be the lastButtonState:
  100.   lastButtonState = reading;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement