Can I have an LED sequence running alongside a FADE ?

Hi..

I hope someone can help. Also, I hope that I can explain it correctly..

Thanks for having a look!

I have a number of LEDs flashing - they use the following code so that they can independently flash in their own specific sequence...

const byte LEDAPin = 12;
const byte LEDBPin = 10;

void setup()
{
pinMode(LEDAPin, OUTPUT);
pinMode(LEDBPin, OUTPUT);
}

void loop()
{
SequenceRunA(LEDAPin);
SequenceRunB(LEDBPin);
}

void SequenceRunA(const byte pin)  
{
unsigned long currentMillis = millis();
const int sequence[][2] =
{
{HIGH, 200},
{LOW, 200}
};

const byte sequenceLength = sizeof sequence / sizeof sequence[0];
static unsigned long intervalStart = 0;  
static unsigned long interval;
static byte index = 0;  

if (intervalStart == 0)
{
digitalWrite(pin, sequence[index][0]);
interval = sequence[index][1];  
intervalStart = currentMillis;  
}

if (currentMillis - intervalStart >= interval)
{
index = (index + 1) % sequenceLength;  
digitalWrite(pin, sequence[index][0]);  
interval = sequence[index][1];  
intervalStart = currentMillis;   
}
}

//END OF SEQUENCE A

void SequenceRunB (const byte pin)  
{
unsigned long currentMillis = millis();
const int sequence[][2] =
{
{LOW, 400},
{HIGH, 200}
};

const byte sequenceLength = sizeof sequence / sizeof sequence[0];
static unsigned long intervalStart = 0;  
static unsigned long interval;
static byte index = 0;  

if (intervalStart == 0)
{
digitalWrite(pin, sequence[index][0]);
interval = sequence[index][1];  
intervalStart = currentMillis;  
}

if (currentMillis - intervalStart >= interval)
{
index = (index + 1) % sequenceLength;  
digitalWrite(pin, sequence[index][0]);  
interval = sequence[index][1];  
intervalStart = currentMillis;   
}
}

I also want to have another LED follow it's own sequence... it would be a simple FADE sequence, as shown below... but my question is... Can I have BOTH of these codes running alongside each other. ie. Can I have a fading LED sequence running alongside the SequenceRun?

THANKS!

int ledPin = 11;    // LED connected to digital pin 11

void setup() 
{}

void loop() {
  for (int fadeValue = 0 ; fadeValue <= 100; fadeValue += 1) 
  {
    analogWrite(ledPin, fadeValue);
    delay(1);
  }
  for (int fadeValue = 100 ; fadeValue >= 0; fadeValue -= 1
  ) {
    analogWrite(ledPin, fadeValue);
    delay(5
   );
  }
  delay (500);
    for (int fadeValue = 0 ; fadeValue <= 100; fadeValue += 1) 
  {
    analogWrite(ledPin, fadeValue);
    delay(1);
  }
  for (int fadeValue = 100 ; fadeValue >= 0; fadeValue -= 1
  ) {
    analogWrite(ledPin, fadeValue);
    delay(5
   );
  }
  delay (1000);
}

Can I have a fading LED sequence running alongside the SequenceRun?

Yes, but you need to eliminate the delay()s and use millis() for timing throughout

UKHeliBob:
Yes, but you need to eliminate the delay()s and use millis() for timing throughout

Thanks for your reply.....
Yeah... Thats where my problem lies... I don't understand how to replace the (delay) with (millis)...
Should I try to make the FADE code a SequenceRun in itself?? That is way above my ability...uh oh...

Yes, make another copy of your SequenceRun function and call it SequenceFade and then you have to step through each of your for loops and delays like this (untested)

const byte LEDAPin = 12;
const byte LEDBPin = 10;
const byte ledPin = 11;    // LED connected to digital pin 11


void setup()
{
  pinMode(LEDAPin, OUTPUT);
  pinMode(LEDBPin, OUTPUT);
}

void loop()
{
  SequenceRunA(LEDAPin);
  SequenceRunB(LEDBPin);
  SequenceFade(ledPin);
}

void SequenceRunA(const byte pin)
{
  unsigned long currentMillis = millis();
  const int sequence[][2] =
  {
    {HIGH, 200},
    {LOW, 200}
  };

  const byte sequenceLength = sizeof sequence / sizeof sequence[0];
  static unsigned long intervalStart = 0;
  static unsigned long interval;
  static byte index = 0;

  if (intervalStart == 0)
  {
    digitalWrite(pin, sequence[index][0]);
    interval = sequence[index][1];
    intervalStart = currentMillis;
  }

  if (currentMillis - intervalStart >= interval)
  {
    index = (index + 1) % sequenceLength;
    digitalWrite(pin, sequence[index][0]);
    interval = sequence[index][1];
    intervalStart = currentMillis;
  }
}

//END OF SEQUENCE A

void SequenceRunB (const byte pin)
{
  unsigned long currentMillis = millis();
  const int sequence[][2] =
  {
    {LOW, 400},
    {HIGH, 200}
  };

  const byte sequenceLength = sizeof sequence / sizeof sequence[0];
  static unsigned long intervalStart = 0;
  static unsigned long interval;
  static byte index = 0;

  if (intervalStart == 0)
  {
    digitalWrite(pin, sequence[index][0]);
    interval = sequence[index][1];
    intervalStart = currentMillis;
  }

  if (currentMillis - intervalStart >= interval)
  {
    index = (index + 1) % sequenceLength;
    digitalWrite(pin, sequence[index][0]);
    interval = sequence[index][1];
    intervalStart = currentMillis;
  }
}

void SequenceFade(const byte pin)
{
  unsigned long currentMillis = millis();

  static unsigned long intervalStart = 0;
  static unsigned long interval;
  static byte index = 0;
  static int fadeValue = 0;

  if (intervalStart == 0)
  {
    analogWrite(pin, fadeValue);
    interval = 1;
    intervalStart = currentMillis;
  }

  if (currentMillis - intervalStart >= interval)
  {
    intervalStart = currentMillis;
    switch (index)
    {
      case 0:   // counting up
        analogWrite(pin, fadeValue);
        interval = 1;
        fadeValue++;
        if (fadeValue > 100)
        {
          index++;
          fadeValue = 100;
        }
        break;

      case 1:   // counting down
        analogWrite(pin, fadeValue);
        interval = 5;
        fadeValue--;
        if (fadeValue < 0)
        {
          index++;
          fadeValue = 0;
        }
        break;

      case 2:   // pause
        interval = 500;
        index++;
        break;

      case 3:   // counting up
        analogWrite(pin, fadeValue);
        interval = 1;
        fadeValue++;
        if (fadeValue > 100)
        {
          index++;
          fadeValue = 100;
        }
        break;

      case 4:   // counting down
        analogWrite(pin, fadeValue);
        interval = 5;
        fadeValue--;
        if (fadeValue < 0)
        {
          index++;
          fadeValue = 0;
        }
        break;

      case 5:   // pause
        interval = 1000;
        index = 0;  // start over
        break;
    }
  }
}

You, are an absolute GENIUS!!

Thank you so much for your help...

What a super way to do it... It is EXACTLY as I needed it. I would never have figured it out myself... my head starts to turn to mush :sob:

Sincere Thanks

Dara