Logic control of programme sequence

I have been an Inst tech for a long time and know the hardware side, however , I am very weak on the programming side.
My project. To create an auto curtain opening and closing .
Functionality/ program sequence :-

  1. Must be able to manually open/close the curtains at ANY time.
  2. To auto run the curtains ONLY between certain times ie 1200-1900 ( I know this requires an RTC)
  3. Auto run action to be triggered by the sun shining on an LDR
  4. If manually opened DURING the period where the auto function is enabled - Must be able to override the LDR. ie No good opening the curtain and then the LDR automatically closes it again.
  5. Additional feature : At midnight IF closed to automatically open again.

I have already written the manual open/close sketch with error checking for human error .
I have already sorted out how to use the RTC
I have already written a sketch to get the LDR to work the servo drive.

The hardware side is already done and working.

I just cant put all of the above into a sequence.
Do I use if then else if or some other logical action ie case switch. The more I think about it the less I can come up with a way to do it.

Any help appreciated.
Doc666

doc666:
I have been an Inst tech for a long time and know the hardware side, however , I am very weak on the programming side.
My project. To create an auto curtain opening and closing .
Functionality/ program sequence :-

  1. Must be able to manually open/close the curtains at ANY time.
  2. To auto run the curtains ONLY between certain times ie 1200-1900 ( I know this requires an RTC)
  3. Auto run action to be triggered by the sun shining on an LDR
  4. If manually opened DURING the period where the auto function is enabled - Must be able to override the LDR. ie No good opening the curtain and then the LDR automatically closes it again.
  5. Additional feature : At midnight IF closed to automatically open again.

I have already written the manual open/close sketch with error checking for human error .
I have already sorted out how to use the RTC
I have already written a sketch to get the LDR to work the servo drive.

The hardware side is already done and working.

I just cant put all of the above into a sequence.
Do I use if then else if or some other logical action ie case switch. The more I think about it the less I can come up with a way to do it.

Any help appreciated.
Doc666

You have so much done already. Just expand on the specifications you have.

Did you write the actual open and the close as separate functions, so they can be called by any part of your program. If not, do it now. Does you current program remember the current position of the curtains? If not, make it so. You will need these.

Paul

Thanks Paul for your reply.
Open and close NOT written as separate functions - will correct and also make the actaul servo drive a separate function as well.
The program does remember curtain position so thats fine.

In addition to organising the code into functions (see Planning and Implementing a Program) you should have one or more variables to keep track of where the system is in the sequence.

The push-button, the clock and the LDR should update that variable.

The code to move the curtains should do so based on the value of the variable.

This keeps the inputs and the actions separate and makes the program easier to develop and debug. The only thing common between the inputs and the actions is that variable.

You might call the variable curtainState and maybe it could have these values
'M' for manual open
'm' for manual close
'L' for LDR open
'l' for LDR close (lower case L)
'C' for clock open
'c' for clock close (may not be needed)

Then the control logic might be like these pseudo code snippets (just to illustrate the idea)

if (LDR says close) 
   if (curtainState == 'M') 
      do nothing // don't override the manual setting
   else
     curtainState = 'L'

if (manual button says open) {
    curtainState = 'M' // manu control overrides everything

And the motor control code could be something like this

if (curtainState == 'M' or curtainState == 'L') 
   close curtain
else
  open curtain

...R