Timer.stop() not working!

Hello!

I have a problem with the timers… I have started the timer with an function with is called by an UDP Request. Another UDP Request will stop the timer, but after a short time it restarts automaticaly…

Can everone help me? Here is the code witch is called by the timer:

void fadingRGB() {
    fade2Color(1,255,255,1); // red
    delay(fadingTime);
    fade2Color(255,1,255,1); // green
    delay(fadingTime);
    fade2Color(255,255,1,1); // blue
    delay(fadingTime);
}

int getMax(int a, int b, int c){
 if(a>=b) {
   if (a>=c){
     return a;
   } else {
     return c;
   }
 } else { 
   if (b>=c){
     return b;
   } else {
     return c;
   }
 }
}

// Returns difference of 2 int
int getDiff(int a, int b){
 if(a>=b) {
   return a-b;
 } else { 
   return b-a;
 }
}

void fade2Color(int redTo, int greenTo, int blueTo, int speedVal){
     int redDiff = getDiff(redIs, redTo);
     int greenDiff = getDiff(greenIs, greenTo); 
     int blueDiff = getDiff(blueIs, blueTo); 
     int maxRange = getMax(redDiff,greenDiff,blueDiff);
     /* sorry futur-me for this confusing code */
     
     for (int i=1; i<maxRange; i++){
       if (redIs < redTo){
         redIs    += 1;
       } else if (redIs > redTo){
           redIs    -= 1;
         } else {
           redIs = redTo;
         }
       if (greenIs < greenTo){
         greenIs    += 1;
       } else if (greenIs > greenTo){
           greenIs    -= 1;
         } else {
           greenIs = blueTo;
         }   
       if (blueIs < blueTo){
         blueIs    += 1;
       } else if (blueIs > blueTo){
           blueIs    -= 1;
         } else {
           blueIs = blueTo;
         }
       analogWrite(redPWM,   redIs);  
       analogWrite(greenPWM, greenIs); 
       analogWrite(bluePWM_LEDDimmer,  blueIs);  
       delay(speedVal);
    }
}

EDIT: Sometimes it is working… but mostly don’t.

best greetings!

The callback isn’t that interesting for your issue.
The point is where and how the timer is constructed, started and stopped. Also delay() is a bad thing to be using in a timer callback - especially in that abundance.

What you see might actually be queued timer triggers since each trigger will take rather long you may have multiple pending calls to be worked off before your stop() call will take effect.

@stefanmahr, putting delay(speedVal); ordelay(fadingTime);` in a timer callback() is not ideal nor recommended. Software timers are daisy chained and will block other timers as long as the callback() function doesn’t return. This is why it is suggested that you treat timer callbacks like interrupt service routines - short and quick to return. I would suggest rethinking how you do fadingRGB() and fade2color().

One way is to do something similar in loop() using a non-blocking millis() delay. You would set a flag in the timer callback() that loop() reads and executes the fadingRGB() and fade2Color() functions without blocking. I suggest you consider using an FSM (finite state machine) to step through the fading states to avoid any blocking delays().

1 Like

Thanks for your answers. I also tought that this is not the perfect solution…

@peekay123 I have no idea how I should do this… can you give me an example?

@stefanmahr, I don’t have time right now but i’ll try and put something together tonight for you.

@peekay123 Thank you very much! That’s really great :smile: