bxparks / AceRoutine

A low-memory, fast-switching, cooperative multitasking library using stackless coroutines on Arduino platforms.
MIT License
173 stars 18 forks source link

COROUTINE_DELAY_SECONDS & COROUTINE_DELAY_MICROS not working #26

Closed WhiteLionATX closed 3 years ago

WhiteLionATX commented 3 years ago

I use it like this:

COROUTINE(ledLoop) 
{
  COROUTINE_LOOP() 
  {  
      static uint16_t i3b;
      for(i3b=STARS_START_LED; i3b<NUM_LEDS_TOTAL; i3b++) 
      {
          if (ledEffect_stars != 1) break;
          leds[i3b] = CRGBW(redStars, greenStars, blueStars, whiteStars);
          // not working: 
          COROUTINE_DELAY_SECONDS(1);
      }   
  }
}

All code is working fine but there is no delay happening. (using different ESPs with PlatformIO)

bxparks commented 3 years ago

Can you try COROUTINE_DELAY(1000)? The COROUTINE_DELAY_SECONDS(1) loses accuracy for small values because it uses an approximation for / 1000 to save CPU time on some processors.

WhiteLionATX commented 3 years ago

Can you try COROUTINE_DELAY(1000)? The COROUTINE_DELAY_SECONDS(1) loses accuracy for small values because it uses an approximation for / 1000 to save CPU time on some processors.

I tried COROUTINE_DELAY(1000), COROUTINE_DELAY_SECONDS(1), COROUTINE_DELAY_MICROS(1000000) with different values ...aso. It seems that there is delay happening at all by all this. I cant debug to investigate on the microcontroller (ESP8266) I use.

bxparks commented 3 years ago

What is the value of ledEffect_stars? If that is not exactly 1, this becomes an infinite loop that cannot be broken. You need to add COROUTINE_YIELD() after you break out of the for-loop. I don't know what the rest of your program does.

Can you put some print statements, and make sure the the loop is working?

COROUTINE(ledLoop) 
{
  static uint16_t i3b;
  COROUTINE_LOOP() 
  {  
      for(i3b=STARS_START_LED; i3b<NUM_LEDS_TOTAL; i3b++) 
      {
          if (ledEffect_stars != 1) break;
          Serial.print(i3b);
          Serial.println(": looping...");
          COROUTINE_DELAY(1000);
      }
      COROUTINE_YIELD();
  }
}
WhiteLionATX commented 3 years ago

I noticed it was my fault. I am very sorry. It was a implicit type conversion problem of a part of my code above. I think you can move this to closed. Thank you very much for your fast & friendly replies!!

bxparks commented 3 years ago

You still need that COROUTINE_YIELD() to avoid an infinite loop when you break out of the for-loop. Good luck with your project.