Press 1 and then calculate average value after 60s

Hello guys,

I measure the magnetic field. And I wanted to calculate the average value of the magnetic field after 60s if I press the 1. So when I press one in serial monitor after 60s it gives me the average value. How can I do that with the millis function ?

How many measurements reads per 60 seconds?

When your switch is closed, start recording your readings in an array or just increment a variable by the read value. Also record the current value of millis( ) and enable a Flag.

When the Flag is enabled, check if 60000 ms has expired.

When time is up, calculate the average.

Note: Serial Monitor won't send the '1' until you also press the Enter/Return key or click on the Send button.

So I am understanding this as you want to get rid of using the 1 key to get the avg via serial.
So in setup use prev= millis() so prev holds the start time.

Then in your loop check to see if 60000 millisecs have elapsed using

if (millis()-prev > 60000) {
     //then in here do what you need to do
     prev=millis()    //reset prev to start from present time
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.