I am having some difficulty using the timer (millis) command successfully. I have a power reading that i want to multiply by the elapsed time to get a watt hour reading.I multiply millis by 1000(to get seconds) and I divide the time in seconds by 3600( to convert to hours). My goal is to not get the timer to begin until i meet a condition (ex push button pressed). As of now the timer begins as soon as i turn the arduino on, which i do not want as the voltage and current analog values will not be applied right away. Is there a way to start and reset the timer using push buttons or some sort of condition(ex, voltage>1).
thanks
I am having some difficulty using the timer (millis) command successfully.
Show us your work.
Some reading:
code
unsigned long time;
time = millis();
float time2 = (time/1000);
lcd.print((time2/3600)*power))
its working now i just need a way to start the timer when i want to, rather than the time beginnning to run as soon as i turn on the arduino
I assume you know how to read a push button.
Read the PB, if it reads the opposite state since the last read you know there has been a change in its position.
If the read is HIGH then you do something.
state = digitalRead(button);
if(lastState != state)
{
if(state == HIGH)
{
//run some code
}
lastState = state;
}
You may have to add debounce.
airicklindsay22:
I multiply millis by 1000(to get seconds) and I divide the time in seconds by 3600( to convert to hours).
You need to divide millis by 1000 to get seconds.
airicklindsay22:
codeunsigned long time;
time = millis();
float time2 = (time/1000);
lcd.print((time2/3600)*power))its working now i just need a way to start the timer when i want to, rather than the time beginnning to run as soon as i turn on the arduino
if(some start condition) StartTime = millis();
...
time = millis() - StartTime;
...