loops and timers - really stuck.

Ok, I shall try to get the code up here, but that is where I am stuck:
The flow of the code.

What I want to do:
{ This is FUNCTION 1
Press a button -
Toggle an output
Set a static variable.
EXIT.
If called return value to reflect static variable.
Allow software toggle of state.
}

{ This is a LOOP
Call {function 1} and get return value.
Depending on variable, either display or clear chr() on the screen
Call {functionn 2} annd get return value
Depending on variable, either display or clearn chr() on the screen
}

{ This is function 2
Receive 2 variables. One is a unique number for which other function is calling it, the other is how long the count down is in minutes.
Get minute value from RTC
When called start count down timer
Count down until ZERO.
When called with 0,0 return the number of minutes remaining.
When running, display chr() on screen. When complete, or cancelled, clear chr() from screen
When ZERO, signal to "calling" FUNCTION that it's time is up and TOGGLE the status of it's outputs.
}
Where I am faling over is the inter-function signaling. I can't quite get my head around how to do it.

What I understand is there are 3 calling conditions:
0,0 would return the time left on the countdown.
unique number, minutes would start the countdown running with that many minutes to go. Marking which routine initiated the countdown.
The other possibility is open, but I would guess it would be unique number, 0.

If you think this is confusing, understand that is how I see C at this point in time.

unsigned int function_1 () {
    static unsigned int variable_static = 0; 
//toggle output
    variable_static = 10; //I like 10... Messi uses 10... :)
    return variable_static; //you can use it or not if you like.  
}

unsigned char function_2 ( unsigned int ID, unsigned int minutes) {
  //get minutes from RTC
  while (minutesFromRTC() < minutes); 
  
//too confusing for what you want. 

}

void loop() {

function_1();

function_2(); 

}

I think you need to split this problem in a few more functions, or split it differently. Can you tell us instead what is the goal here?

This is part of my "Alarm clock" project.

http://arduino.cc/forum/index.php/topic,109090.new.html

The original code is badly written and badly structured - so I have been told.

I wrote functions to do things and those functions "wrote" to the screen.

The problem is that if I exited the CLOCK DISPLAY, when I went back, the symbols/icons were gone.

I had to modify the code so in the main "loop" there would be a poll of the functions asking them for their status.

They would return a value: 0 or 1 signifying their status. Ofcourse it isn't that simple, as I also have to be able
to call the fucntion to "activate" it. I say activate as a concept and not specifically to turn things ON or OFF.

So, that's all well and good, but "needs as they be" require more functionality.

This is an "alarm clock" yet there is no "SLEEP" function. Now, most of the functions are translated from an old
digital clock which I hacked 25+ years ago.

My options are:
"Activate" SLEEP mode and have an output active for SLEEP time/minutes, or in SLEEP minutes time the output activates.

So what I did:
I wrote a SLEEP routine. Alas I can't set the ammount of time, but that is for the future.

A few possibilities to do:
Now I want to activate a function then press the SLEEP button.
Doing this, the active function will stay active for SLEEP number of minutes.

If I toggle a function, then press the SLEEP: In SLEEP number of minutes, the function will activate.

To abort either of these two above, I press the SLEEP button again.

When the SLEEP timer is active, an icon is on the screen.
When an output is active, another icon is on the screen.

These icons are restored if I exit the CLOCK DISPLAY and then return.

So there needs to be a bit of checking and sending "signals" from one function to another.

I am trying to get my head around it all, but alas I am thinking in the wrong way - discreet electronics, rather than
a program.

Does that help?

I will try to put up the reevant parst of the code later on. It is a bit more complex than what you posted.

Here are some parts of the code:

byte light_toggle(byte foo)
{
    static byte Light_status;
    byte fctn; 
    fctn = foo;
    if (fctn == 0)
    {
      
      return Light_status;
    }

    if (fctn == 1)
    {
       Light_status = Light_status +1;
       Light_status = Light_status%2;

       if (Light_status==0)    
       {
            digitalWrite(light,LOW);
       }

       if (Light_status==1)    
       {
            digitalWrite(light,HIGH);
            sleep_time(10,1);
       }
    }
    if (fctn == 2)    // used to force off.
    {
        Light_status = 0;
        digitalWrite(light,LOW);
    }
   return 0;
}




byte sleep_time(byte foo, byte boo)      // foo = time, boo = calling routine
{
    int rtc[7];
    int threashold = 5;      // threashold for time of minutes.  Must be greater than 2 for now.
    RTC.get(rtc,true);
    static byte count_down_time_remaining;
    static byte memory;      // This is a copy of the original call value so if it is seen again, it cancels the timer.
    static byte min_flag;    // This ia the memory of the last minute from the RTC.
    byte fctn;
    static byte caller;
    fctn = foo;
    caller = boo;
    byte old_caller;
/*
    if (fctn == 0)        //  used for debugging.
    {
      lcd.setCursor(5,0);
      lcd.print(fctn);
      lcd.print(" ");
      lcd.print(memory);
      lcd.print(" ");
      lcd.print(count_down_time_remaining);
      lcd.print(" ");
      lcd.print(min_flag);
    }
*/

    // this is another effort to do the function.
    if (old_caller == caller)
    {
        if (fctn == 0)
        {
            count_down_time_remaining = 0;
            return 0;
        }
        if (fctn > 0)
        {
            count_down_time_remaining = fctn;
            return count_down_time_remaining;
        }
    }
    if (caller == 0)
    {
        if (fctn == 0)
        {
            // decrement counter
            return count_down_time_remaining;
        }
    }
}




/*
    // Another effort.
    if (fctn < threashold)
    {
        switch (fctn)
        {
            case 0:
            if (count_down_time_remaining >0)
            {
               if (min_flag != rtc[1])
               {
                   min_flag = rtc[1];
                   count_down_time_remaining = count_down_time_remaining - 1;
               }
               return 1;
               break;   
            }
            if (count_down_time_remaining == 0)
            {
                if (caller == 1)
                {
                    light_toggle(2);
                }
                //  Other routines for other "Callers" to handle turning them off.
                
                return 0;
                break;
            }
            break;
            
            case 1:

            break;
            
            case 2:
            
            break;
            return 0;
        }
    }
    if (memory == fctn)
    {
        count_down_time_remaining = 0;
        memory = 0;  // any number lower than the threashold, but 0 is a good place to set it.
        return 255;
    }
    count_down_time_remaining = fctn;
    memory = fctn;
    min_flag = rtc[1];
    return 1;
}
*/





(the loop)

    clock1.run();
    if (!clock1.alarm_is_on)
    {
      temp1=wait_on_escape(1000);
      switch (temp1)
      {
        case 0:
        break;
        
        //  Un-remark these as needed for extra functions at the display level.
        
        // alarm defere routine, so if you wake up earlier than the alarm, you can cancel it for that time.
        case 1:                // UP key
        alarm_defere(1);
        break;
        
        // ALL Alarms ON/OFF quick toggle.
        case 2:                // DOWN key
        alarm_defere(2);
        break;
        
        //  Electric blanket ON/OFF function.   Needs work for time specific on period.
        case 3:                // LEFT key
        leky_blanky(1);
        break;
        
        // call my routine (in alarm_clock) to toggle light.
        case 4:                // RIGHT key
        light_toggle(1);
        break;
        
        //  call my routine for sleep time (in alarm_clock).
        case 5:                // ENTER key
        //sleep_time(30,0);
        //digitalWrite(LED,HIGH);
        break;
        
        //  This is the ESCAPE key and exits back to the menu
        case 6:                // ESCAPE key
        return;
 
        
        default:
        //return;
        break;
      }
    }

Here is an ENGLISH version of my latest effort.

I am trying to break things down into little pieces and attack it from that stand, but I am still not making a lot of headway.

I know I have left things out - I can feel it in my bones.
And sorry for the "long post" and maybe not including it in the "code" things, but it isn't really code.

It is how I am thinking to structure how things happen in the scheme of things.

Other function/s:

Call SLEEP(ID,TIME)

SLEEP(ID,TIME)

Check ID
Switch on ID
=0
Count down
Compete?
(yes)
Clear flag
Call (ID,toggle)
EXIT
(no)
Return counter
EXIT
=1 // This is from another function.
Compare to old ID
Different? (yes)
Store new ID and TIME
Start counting
EXIT
(no)
Get new TIME
Compare TIME to old TIME
Same? (yes)
Stop counting
Clear flag
EXIT
(no)
Set old TIME = TIME
Start counting --- Needed?
EXIT
EXIT
=2 // This is from another function.
• (repeat for other functions.)
=’X’ // This is for when the button is pressed.
Compare ID to old ID
Different? (yes)
Store new ID and TIME
Start count down
EXIT
(no)
Cancel countdown
EXIT