Counter Function Help

Hi,

I was wandering if someone could help me with writing some code that I thought would be relatively simple but I am having trouble getting it to do exactly what I want it to do. The idea of the function is to perform the following:

while(a button is pressed/held)
continuously count and save the number of seconds it has been held for.
If (the amount of time the button has been held for is less than a maximum time)
change an output message
change the output of a set of LED's
Else If (the amount of time the button has been held for is greater than a maximum time)
change an output message
change the output of a set f LED's

The bit I am having trouble with is continuously counting and saving the number of seconds the button has been held. I think it needs to be some sort of for loop that constantly looks at the time when the button was pressed using millis() against the current time using millis() but the system also needs to account for multiple button presses. By this I mean the amount of time the button has been held should be stored so if the button is released and then depressed again the counter should begin from this value not from zero.

Can anyone point me in the right direction?

So you need to "accumulate" the total time the button has been pressed.

Remember the time it is pressed, subtract that from the time it's released and add the result to a variable. That variable holds the total time the button has been pressed.


Rob

Thanks for your reply Rob.

Perhaps I did not make this clear in my first post.

The problem I have with performing the function in the way you describe is it relies on the button being released to calculate the amount of time it was pressed for, in this way the conditions for the if statements could be exceeded without the system knowing until the button is released.

By this I mean that when the button is first held, it could be held for longer than "maxtime" changing the system output for longer than the time permitted as time held is not calculated untill the button is released.

Is there another way of calculating the time held without waiting for the switch to be released?

If I get you correctly you wanna have a value of the time the button is pressed even if it's still down. This is even easier: Just write down the time the state of the button goes from up to down. Then in every loop() run you can calculate how long that state is already so by subtracting the current value from the saved one. You doesn't have to wait for the button to go up, the processor will happily calculate that value in every run.

if (pstate == HIGH && swstate == LOW )
	{
		starttime = millis(); // registers the time the switch was pressed
		changeLED(1, ON);
	}

Thanks for your help people, I have been trying methods as you suggested but for some reason my initial if statement does not save the initial time the button is pressed which i call "starttime" I know this because if i serial print the output it always displays 0 when I press the button. Anyone have any ideas as to where I might be going wrong?

Need the rest or your code.

Are you properly assigning pstate to state at the end of each loop?

silly me! I deleted that line when I removed some unnecessary code, thanks for pointing out my mistake!