Thank you.
that's my new code
int 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;
float hum = 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;
}
if (currentMillis - startMillis >= period) //test whether the period has elapsed
{
freq = freq/2;
hum = 76 - ((freq - 16000)/25);
Serial.println(hum);
startMillis = currentMillis; //IMPORTANT to save the start time of the current LED state.
freq = 0;
}
lstate = state;
}
i'm getting now 43
i will test with another sensor and see if i get the same results.