 # Start time only when Sensor Val >0

Hello, I'm developing a Power Meter with the help of a clamp.. The problem is:

Energy (Wh) is calculated by the formula: (E= Power*Time/3600). I want this time1 starting only when it detects a power greater than 0. If the power goes to 0, the counter stops...and re-activates again when the sensor returns a value.

How can I do this?

``````if (power > 0)
{
startTime = millis();
}
else if (power == 0)
{
stopTime = millis();
}
``````

But isn´t millis() always running? This startTime=millis() is the time since the arduino board began running the program, meaning the value of time is not going to start at 0s when it detects power>0! And the same to stopTime.

Yes, millis() is the time in milliseconds since the Arduino was powered up or reset. So what ? You know when power was detected and when it stopped being detected so you can calculate how long it was on or off by subtracting one time from the other.

Thanks! I think I got it!!

Sorry to bother again... but when there are 5 readings (for example) in a row where power >0 how can time be added?

amlo13: Sorry to bother again... but when there are 5 readings (for example) in a row where power >0 how can time be added?

You'll need to keep track of whether or not it was already running then.

``````boolean running = false;   // this should be at global scope

if (power > 0 && running == false)
{
startTime = millis();
running = true;
}
else if (power == 0)
{
stopTime = millis();
running = false;
}
``````