This should do the same thing without blocking. I have not compiled or tested this code, so there could be some errors.
This function could be added anywhere to give the fading as it is self contained.
#define FADE_IN 0
#define FULL_BRIGHT 1
#define FADE_OUT 2
#define FADE_DELAY 45 //ms
#define FULL_DELAY 70 //ms
#define LED_PIN 5
#define FADE_MAX 30 // max PWM value
#define FADE_MIN 1
void Beacon()
{
static uint32_t nextWakeup = 0;
static uint8_t currentState = FADE_IN;
static uint8_t fadeValue = FADE_MIN;
if (millis() <= nextWakeup)
return; // do nothing as time has not expired
switch (currentState)
{
case FADE_IN:
analogWrite(LED_PIN, fadeValue++);
if (fadeValue >= FADE_MAX)
{
currentState = FULL_BRIGHT;
fadeValue = FADE_MAX;
}
nextWakeup = millis() + FADE_DELAY;
break;
case FULL_BRIGHT:
digitalWrite(LED_PIN, HIGH);
currentState = FADE_OUT;
nextWakeup = millis() + FULL_DELAY;
break;
case FADE_OUT:
analogWrite(LED_PIN, fadeValue--);
if (fadeValue <= FADE_MIN)
{
currentState = FADE_IN;
fadeValue = FADE_MIN;
}
nextWakeup = millis() + FADE_DELAY;
break;
default: // error catch all - restart the sequence
currentState = FADE_IN;
fadeValue = FADE_MIN;
break;
}
return;
}