Go Down

Topic: Disable variable change within if-loop? (Read 555 times) previous topic - next topic


I'm working on Daniel Andrade's binary clock (http://www.danielandrade.net/2008/07/15/binary-clock-with-arduino/). (Is there a way to make a regular <a href=></a> link on this forum?  :P)

I'm adding all sorts of funny things to it, mostly to do with cutting power consumption for battery operation, but the question I have is about the time-set mechanism. (There are two pushbuttons connected to GND; the inputs have internal pull-ups enabled.) The way the code is set up, the hour jumps when the minute hits sixty:
Code: [Select]

if (minute >=60) {
minute = 0; // reset minutes to zero

if (hour >=24) {


But what about when the clock is being set? (This will be an actual bedside clock, so it'll have to change for DST, etc. And I don't expect it to keep perfect time, so it'll need adjustment.)
Code: [Select]

         valm = digitalRead(A4);    // add one minute when pressed
if(valm == LOW) {
         hPrev = hour;
         hour = hPrev;
fourSec = 0;    //this resets the
         TCNT1 = 0;      //hardware TIMER1
delay(2);       //system is clocked way down; this is 256 millisec real-time
valh = digitalRead(A5);    // add one hour when pressed
if(valh == LOW) {

But what about when the clock is being set? I want to be able to change the hour and minute counters independently--the hour will stay the same, no matter what happens to the minute. (Changing the hour is fine as-is. Makes no difference to the minutes.) I tried to make that happen with the hPrev variable, but it didn't make any difference.

So is there any way I can program "within this if-loop, DO NOT change 'hour' no matter what happens to 'minute'"? It seems that the problem is the ┬ÁC is too fast for me--even within the loop, it recognizes the fact that "minute" has hit sixty. Do I have to use some convoluted variable-relaying or something?

Thanks very much!


Not seeing the rest of the code, you should use a state variable. If you are setting the clock, then you just have another logic for that in an else statement (or skip the hour++ line). Hmm, I guess that could work.
if (!setting)

For me it would be easier to understand the programming logic like this if there was a single variable that held the seconds since midnight. Then you can just convert that into hours, minutes and seconds when need be. When you are setting the hours, you would need to calculate what that is. Maybe that's just me..


Ohhhh, I see what you mean! Within the "minute >= 60" loop, check to see if the minute-set button is actually being pushed. If so, don't change the hour no matter what. Elegant!

Go Up