that's my new code
int sensor = 1;
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(9600);
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; //IMPORTANT to save the start time of the current LED state.
freq = 0;
}
}
but in the serial monitor, i always get 25?