Combining led blink and fade (PWM)

Danois90:
If you cannot wrap your head around all the great examples you have gotten, then you could try to use a library which could help you do the work. Please read this on how to import a ZIP library and then get TDuino from here (click "Clone or download" and then Download ZIP). TDuino has a timeline class which allows you to gradually run a task after a certain amount of time, which may be what you need.

The following code is untested so it may not work at all, but try and see what happens - and read the comments for explanation :slight_smile:

#include <TDuino.h>

//Number of LED's to handle
#define NUM_LED 13

//Number of different states
//State 1 = All on
//State 2 = All off
//State 3 = 6+8 on
//State 4 = 6+8 off
//State 5 and up = NUM_LED * 2 states to fade on and off each LED
#define NUM_STATES 4 + (NUM_LED * 2)

//Speed of each transition
#define FADE_SPEED 1000

//The pins for each LED
const byte LED_PINS[NUM_LED] = { 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 45 };

//Forward declaration - may not be required
void tlCallback(byte slot, float progress);

//The timeline to use, constructed with callback and
//an amount of slots matching the number of LED's to handle
TTimeline tl(tlCallback, NUM_LED);

//Other global declarations:
byte state; //Current state
int i; //Used multiple places
bool fadeInverse; //Direction of the fade

void tlCallback(byte slot, float progress)
{
  //Implementation of the timelines callback, here we handle the LED's brightness
  if (fadeInverse) analogWrite(LED_PINS[slot], TL_MapToInt(progress, 255, 0)); //Inverse - fade from 255 to 0
  else analogWrite(LED_PINS[slot], TL_MapToInt(progress, 0, 255)); //Forward - fade from 0 to 255
}

void nextState()
{
 
  //Increment the state
  state++;
 
  //Check for a valid state
  if (state > NUM_STATE) state = 1;
 
  //This is used to define how long time should pass before
  //a slot in the timeline is started
  int timeOffset = 0;
 
  if (state == 1)
  {
     
      //Turn on all LED's one after the other
      fadeInverse = false; //Fade up
      for (i = 0; i < NUM_LED; i++)
      {
        tl.set(i, FADE_SPEED, timeOffset);
        timeOffset += FADE_SPEED;
      }
     
  }
 
  else if (state == 2)
  {
     
      //Turn off all LED's one after the other - reverse order
      fadeInverse = true; //Fade down
      for (i = NUM_LED - 1; i >= 0; i--)
      {
        tl.set(i, FADE_SPEED, timeOffset);
        timeOffset += FADE_SPEED;
      }
     
  }
 
  else if (state == 3)
  {
     
      //Fade on 6 + 8
      fadeInverse = false; //Fade up
      timeOffset = 1000; //Wait 1 second before starting
      tl.set(6, FADE_SPEED, timeOffset);
      tl.set(8, FADE_SPEED, timeOffset);
     
  }
 
  else if (state == 4)
  {
     
      //Fade off 6 + 8
      fadeInverse = true; //Fade up
      timeOffset = 5000; //Wait 5 seconds before starting
      tl.set(6, FADE_SPEED, timeOffset);
      tl.set(8, FADE_SPEED, timeOffset);
     
  }
 
  else
  {
     
      //Fade on and off each LED
      i = state - 4; //This is the current LED multiplied by 2
      fadeInverse = i % 2 != 0; //If the state is not even, fade off
      tl.set(i / 2, FADE_SPEED, timeOffset); //i divided by 2 is the LED currently working
     
  }
       
}

void setup()
{
 
  //Setup the timeline object
  tl.setup();
 
  //Setup pins
  for (i = 0; i < NUM_LED; i++)
  {
      pinMode(LED_PINS[i], OUTPUT);
      analogWrite(LED_PINS[i], 0);
  }
 
  //Other setup code here
 
  //Initialize the state machine
  state = 0;
  nextState();
 
}

void loop()
{
 
  //Loop the timeline
  tl.loop();
 
  //If the timeline has finished (no active slots), go to next state
  if (tl.firstActive() < 0) nextState();
 
}

Thanks, I haven't tried the code yet cause it says 'NUM_STATE' was not declared in this scope,
What should I define NUM_STATE as ?