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?
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;
}
}
}