Guest User

Untitled

a guest
Jun 18th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. #define sensor 2
  2. unsigned long startMillis; //some global variables available anywhere in the program
  3. unsigned long currentMillis;
  4. const unsigned long period = 1000; //the value is a number of milliseconds
  5. long freq = 0;
  6. int state = 0;
  7. int lstate = 0;
  8. void setup() {
  9. Serial.begin(115200);
  10. pinMode(sensor, INPUT);
  11. startMillis = millis(); //initial start time
  12. }
  13.  
  14. void loop() {
  15. currentMillis = millis();
  16. state = digitalRead(sensor);
  17.  
  18. if (state != lstate) {
  19. freq = freq +1;
  20. lstate = state;
  21. }
  22. if (currentMillis - startMillis >= period) //test whether the period has elapsed
  23. {
  24. Serial.println(freq);
  25. startMillis = currentMillis;
  26. freq = 0;
  27. }
  28.  
  29. }
Add Comment
Please, Sign In to add comment