I have problem with my timer ,this homework is connect with arduino. My problem is when val==4,the value of sec won’t go properly, it just decrease maybe ten or more at a time,and I want it to decrease one second at a time. Can someone help me with this problem. Thanks a lot!
you are changing the value of sec in the for loop therefore the value is going to get larger quickly as for every second that your sketch is open you are counting up to the total seconds and for each one of those seconds you are adding the then total seconds time. Meaning if your sketch has ben opened for two seconds then sec will be 3, or 1 + 2, for 4 seconds and sec will be 10 or 4 + 3 + 2 + 1, or currently your doing sec = sec!.
I am wondering how to make sec decrease one second at a time , my idea is when val==4, the timer will start to countdown.But my code will only decrease one second when I press the button, once I release the button it just stop.
Thanks for solving my problem.
It sounds like the value you are testing is reverting back to something else as soon as the if statement runs. Perhaps attach a bool condition to 4 instead.
bool k = false;
If (key == 4) k = true;
if(k) {
sec += 1;
}