Hi all...
I am still trying to get to grips with Arduino coding.. I have bought 4 books, and am doing all I can to get my head around it. But I'm still struggling a bit... If anyone had any ideas about the code below (query is in the //COMMENTS at the top of code), I would really appreciate it.
Many Thanks
Dara
//SO HERE IS MY PROBLEM... I HAVE THREE SEPARATE SEQUENCES GOING....
//THE FIRST TWO ARE STANDARD FLASHING LEDS...
//THE THIRD IS A FADING LED..
//I HAVE TRIED MANY DIFFERENT WAYS TO SOLVE THIS (USING A BOOK "GETTING STARTED WITH ARDUINO)... NO JOY...
//I WOULD LOVE TO BE ABLE TO GET TO A STAGE WHERE THE FOLLOWING HAPPENS..
//PUSH A SWITCH TO TURN ON SEQUENCE 1...
//SECOND PUSH TURNS ON SEQUENCES 2 AND 3..
//PUSH TO SWITCH OFF...
//IS THIS EVEN POSSIBLE? OR AM I TRYING TO DO TOO MUCH??
//ANY AND ALL HELP AND ADVICE IS APPRECIATED...
//I AM *FAIRLY* OKAY WOTH CODING, BUT AM STILL LEARNING, SO I MIGHT NOT UNDERSTAND
//ALL SUGGESTIONS, BUT ALL HELP APPRECIATED...
//THANKS DARA
const byte LEDA8Pin = A8;
const byte LEDA9Pin = A9;
const byte LEDFADE13Pin = 0;
//-------------------------------------------------
void setup()
{
pinMode(LEDA8Pin, OUTPUT);
pinMode(LEDA9Pin, OUTPUT);
pinMode(LEDFADE13Pin, OUTPUT);
}
//-------------------------------------------------
void loop()
{
SequenceRunA8(LEDA8Pin);//ROSSLARE EUROPORT OC WRG (G) 5s OK ok120
SequenceRunA9(LEDA9Pin);//BALLINACOURTY POINT FL(2)WRG (W) 10s OK
SequenceFade13(LEDFADE13Pin);//TUSKAR ROCK FL(2) 7.5s OK
}
//-------------------------------------------------
//START OF LIGHT LED FLASHING, INDEPENDENT OF FADE
void SequenceRunA8(const byte pin)
{
unsigned long currentMillis = millis();
const int sequence[][2] =
{
{HIGH, 3500},
{LOW, 121500}
};
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
//-------------------------------------------------
//START OF SECOND LIGHT LED FLASHING, INDEPENDENT OF FADE
void SequenceRunA9(const byte pin)
{
unsigned long currentMillis = millis();
const int sequence[][2] =
{
{HIGH, 500},
{LOW, 1000},
{HIGH, 500},
{LOW, 8000}
};
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 LIGHT SEQUENCE
//-------------------------------------------------
//START OF FADING LIGHT SEQUENCE
void SequenceFade13(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: // pause
interval = 1000;
index++;
break;
case 1: // counting up
analogWrite(pin, fadeValue);
interval = 1;
fadeValue++;
if (fadeValue > 100)
{
index++;
fadeValue = 100;
}
break;
case 2: // counting down
analogWrite(pin, fadeValue);
interval = 10;
fadeValue--;
if (fadeValue < 0)
{
index++;
fadeValue = 0;
}
break;
case 3: // pause
interval = 500;
index++;
break;
case 4: // counting up
analogWrite(pin, fadeValue);
interval = 1;
fadeValue++;
if (fadeValue > 100)
{
index++;
fadeValue = 100;
}
break;
case 5: // counting down
analogWrite(pin, fadeValue);
interval = 10;
fadeValue--;
if (fadeValue < 0)
{
index++;
fadeValue = 0;
}
break;
case 6: // pause
interval = 4800;
index = 1; // start over
break;
}
}
}