CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

FOR loop not looping

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Markdem



Joined: 24 Jun 2005
Posts: 206

View user's profile Send private message Send e-mail

FOR loop not looping
PostPosted: Mon Sep 18, 2006 5:37 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Sep 18, 2006 6:29 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Mon Sep 18, 2006 6:29 am     Reply with quote

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!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group