Sorry to be pop up again, I wanted to add another LED onto another PMW pin, such as IO3.
The below code works, but is this method the most efficient to achieve the same PWM pattern on 2 IO pins ?
Many thanks for your help,
#define FADE_IN 0
#define FULL_BRIGHT 1
#define FADE_OUT 2
#define FADE_DELAY 45 //ms
#define FULL_DELAY 70 //ms
#define LED_PIN1 5
#define LED_PIN2 3
#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 fadeValue1 = FADE_MIN;
static uint8_t fadeValue2 = FADE_MIN;
if (millis() <= nextWakeup)
return; // do nothing as time has not expired
switch (currentState)
{
case FADE_IN:
analogWrite(LED_PIN1, fadeValue1++);
analogWrite(LED_PIN2, fadeValue2++);
if (fadeValue1 >= FADE_MAX)
{
currentState = FULL_BRIGHT;
fadeValue1 = FADE_MAX;
}
if (fadeValue2 >= FADE_MAX)
{
currentState = FULL_BRIGHT;
fadeValue2 = FADE_MAX;
}
nextWakeup = millis() + FADE_DELAY;
break;
case FULL_BRIGHT:
digitalWrite(LED_PIN1, HIGH);
digitalWrite(LED_PIN2, HIGH);
currentState = FADE_OUT;
nextWakeup = millis() + FULL_DELAY;
break;
case FADE_OUT:
analogWrite(LED_PIN1, fadeValue1--);
analogWrite(LED_PIN2, fadeValue2--);
if (fadeValue1 <= FADE_MIN)
{
currentState = FADE_IN;
fadeValue1 = FADE_MIN;
}
if (fadeValue2 <= FADE_MIN)
{
currentState = FADE_IN;
fadeValue2 = FADE_MIN;
}
nextWakeup = millis() + FADE_DELAY;
break;
default: // error catch all - restart the sequence
currentState = FADE_IN;
fadeValue1 = FADE_MIN;
fadeValue2 = FADE_MIN;
break;
}
return;
}