View previous topic :: View next topic |
Author |
Message |
Markdem
Joined: 24 Jun 2005 Posts: 206
|
FOR loop not looping |
Posted: Mon Sep 18, 2006 5:37 am |
|
|
Hello all, i am tring to build a light timer that can control 4 sets of lights. The following code is what i use to work out if the light should be on or off
Code: |
void lightwork()
{
ds1307_get_time(hrs,min,sec);
for (i=0; i<4; i++)
{
if ((lightbank_on_h[i] == lightbank_off_h[i]) && (lightbank_on_m[i] == lightbank_off_m[i]))
{
outputstates[i] = 1;
break;
}
else if ((lightbank_on_h[i] == lightbank_off_h[i]) && (lightbank_on_m[i] <lightbank_off_m>= lightbank_on_m[i]) && (min <lightbank_off_m> lightbank_off_m[i]))
{
if ((hrs != lightbank_on_h[i]) || ((hrs == lightbank_on_h[i]) && ((min >= lightbank_on_m[i]) || (min < lightbank_off_m[i]))))
{
outputstates[i] = 1;
break;
}
else
{
outputstates[i] = 0;
break;
}
}
else if (lightbank_on_h[i] < lightbank_off_h[i])
{
if (((hrs == lightbank_on_h[i]) && (hrs <lightbank_off_h>= lightbank_on_m[i])) || ((hrs > lightbank_on_h[i]) && (hrs <lightbank_off_h> lightbank_on_h[i]) && (hrs == lightbank_off_h[i]) && (min <lightbank_off_m> lightbank_off_h[i])
{
if (((hrs == lightbank_on_h[i]) && (hrs > lightbank_off_h[i]) && (min >= lightbank_on_m[i])) || ((hrs > lightbank_on_h[i]) || (hrs < lightbank_off_h[i])) || ((hrs < lightbank_on_h[i]) && (hrs == lightbank_off_h[i]) && (min < lightbank_off_m[i])))
{
outputstates[i] = 1;
break;
}
else
{
outputstates[i] = 0;
break;
}
}
else
{
outputstates[i] = 0;
break;
}
if(outputstates[i] == 0) //check to see if above code turned the lights on or off
{
switch (i)
{
case 0:
device_off(light1);
break;
case 1:
device_off(light2);
break;
case 2:
device_off(light3);
break;
case 3:
device_off(light4);
break;
}
}
else
{
switch (i)
{
case 0:
device_on(light1);
break;
case 1:
device_on(light2);
break;
case 2:
device_on(light3);
break;
case 3:
device_on(light4);
break;
}
}
} //close for loop
} //close function
|
Problem is that it will only work when i=0. eg the loop will now work once, and then it will exit. Can anybode see why??
Thanks, Mark |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Mon Sep 18, 2006 6:29 am |
|
|
Only use 'break' inside the switch statement, in all other situations it will cause your program to break out of the current loop, i.e. out of your while loop. |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Mon Sep 18, 2006 6:29 am |
|
|
1) How is "i" defined?
2) Where is "i" defined?
3) Carefully re-read what a "break" statement does. _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
|