Strange Timer behaviour

I’ve been trying to get a timer in my program, and after much tempering with the code, i finaly got it to work, but i can’t understand why.

I hope someone can shed some light on this:

I have a loop of so far just three lines:

void loop(){

        CheckTouchscreen(); //checks IF theres any touch action then continues
      
        buttonClear();      // resets the buttons to unpressed state
     
        timerCheck();       // check if the timers have run out of time
      
}// end of loop

then I call a button through the touchscreen which calls the curtains function:

void curtains(){
         
          if ( curtainSet < 55){ curtainSet = 1 ;}
          if (curtainSet > 260){ curtainSet = 309;}
          if (curtainMove == 1)
              {
               Serial.println ( "BA------------------------------curtain stopped" );
              curtainMove = !curtainMove ;
              }
          if (oldCurtainSet > curtainSet)
              {
                Serial.println ( "BA-------------------------curtains are opening");
                openCurtain = 1;
                curtainMove = 1;
              }
          if (oldCurtainSet < curtainSet)
              {
                  Serial.println ( "BA--------------------------curtains are closing");
                closeCurtain = 1;
                curtainMove = 1;
                
              }

          oldCurtainSet = curtainSet;
          writeButtonCurtains(); 
          loopTime = millis();
          timerCheck();
}

And then the timer function should check if the time is up:

void timerCheck(){
         const long curtainFullTime = 3000;
         unsigned long curtainTime;
         currentTime = millis();
         int y =0;
         if (curtainSet>olderCurtainSet)
            {
            y = curtainSet - olderCurtainSet;
            }
         if ( curtainSet<olderCurtainSet)
            {
               y = olderCurtainSet - curtainSet ;
            }
         
           curtainTime = (curtainFullTime/310*y);

          if (curtainMove == 1) 
          {      
            delay (4  );
          }

         if (currentTime - loopTime >= curtainTime &&  curtainMove == 1)
              {
            curtainMove = 0;
            Serial.print ( " -------running time was:   ");
            Serial.println ( currentTime - loopTime );
            Serial.println ( "-----------------curtain stopped by timer");
            olderCurtainSet = curtainSet;
              }    

      }

this works. The strange thing is this in the last function :

          if (curtainMove == 1) 
          {      
            delay (4  );
          }

If I leave this out it doesn’t work anymore.

could anyone explain this?

I don't know. Your logic is so convoluted that I can't follow it.

The idea was to put al the code in separate functions, because the total program will probably get quite large. I’m building a touchscreen controlled lighting system, and for now I’m just working on the touchscreen part.

I’ve put the code in the attachment so you can have a look.

testdisplay8.ino (18 KB)

leolectric:
The idea was to put al the code in separate functions, because the total program will probably get quite large.

Well, good. I have no quarrel with that. In general, it's an excellent programming practice.

What does timerCheck() do? Check the timer? For what condition? Then what?

timerCheck just has to check if the timer, set inside "Void curtains" has run the portion of the time given through "curtainSet" , in the last section of CheckTouchscreen.

This one checks the place where the slider is touched, and gives this a number. in timerCheck the number is recalculated in the time needed to get to that point, and when the tie is up "curtainMove" is set to 0.

I had some trouble getting this to work, so I put in some print lines to debug, and the If statement, so it wouldn't always have to print, and it suddenly worked. I then got rid of the print lines, and turned the delay back, and discovered that under a delay of 4 , 'the " if (currentTime - loopTime >= curtainTime && curtainMove == 1)" doesn't fire anymore. It turned out I needed the delay. but I don't understand why, and how this can be avoided, because I prefer code without delays.

I also don't understand why the timer seems to hold up the program since I purposely used If statement in the timer, But for some reason the timerCheck function keeps the touchscreen from feeling new touches.

Any thoughts on that?