Pages: [1]   Go Down
Author Topic: Disable variable change within if-loop?  (Read 233 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?  smiley-razz)

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:
if (minute >=60) {
hour++;
minute = 0; // reset minutes to zero
}

if (hour >=24) {
hour=0;
}

Goodgood.

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:
         valm = digitalRead(A4);    // add one minute when pressed
if(valm == LOW) {
         hPrev = hour;
minute++;
         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) {
hour++;
delay(2);
}

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!
Logged

Finland
Offline Offline
Jr. Member
**
Karma: 1
Posts: 84
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
  hour++;

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..
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Pages: [1]   Go Up
Jump to: