Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define sensor 2
- unsigned long startMillis; //some global variables available anywhere in the program
- unsigned long currentMillis;
- const unsigned long period = 1000; //the value is a number of milliseconds
- long freq = 0;
- int state = 0;
- int lstate = 0;
- void setup() {
- Serial.begin(115200);
- pinMode(sensor, INPUT);
- startMillis = millis(); //initial start time
- }
- void loop() {
- currentMillis = millis();
- state = digitalRead(sensor);
- if (state != lstate) {
- freq = freq +1;
- lstate = state;
- }
- if (currentMillis - startMillis >= period) //test whether the period has elapsed
- {
- Serial.println(freq);
- startMillis = currentMillis;
- freq = 0;
- }
- }
Add Comment
Please, Sign In to add comment