I am building a lighthouse that has 6 leds. I want one led to fade to the next.
I found code to fade 3 leds from one to the next which works perfect for 3 leds. I added 3 more leds and they do not fade in sequence. They go 1,2,3 5,4,6 or such and do not fade in and out as the original three did.
The original code was to fade between three colors on a single led I think.
Each section I added to the original code is noted with a //=== explanation on what I did.
Can anyone help me out with this? I am new to "C" coding. The code is below:
Dave
const int FirstPin = 3;
const int SecondPin = 5;
const int ThirdPin = 6;
const int FourthPin = 9; // ==========Added outputs 9 to 11
const int FifthPin = 10;
const int SixthPin = 11;
// Brightness values
int first = 0;
int second = 0;
int third = 0;
int fourth = 0;//====Added three more here
int fifth = 0;
int sixth = 0;
int zone = 0; // Used to work out which LEDs should be on, off, or ramping up or down
int phase = 0; // Used to ramp brightnesses up or down (ranges between 0 - 255)
int fadeSpeed = 1; // amount phase is incremented by each update
void UpdateLedColours()
{
phase += fadeSpeed;
if(phase > 255)
{
phase = 0;
zone += 1;
if(zone > 11)// <==========Changed this from 5 to 11
{
zone = 0;
}
}
switch(zone)
{
case 0:
first = 255;
second = phase;
third = 0;
break;
case 1:
first = 255 - phase;
second = 255;
third = 0;
break;
case 2:
first = 0;
second = 255;
third = phase;
break;
case 3:
first = 0;
second = 255 - phase;
third = 255;
break;
case 4:
first = phase;
second = 0;
third = 255;
break;
case 5:
first = 255;
second = 0;
third = 255 - phase;
break;
//====================================== Added cases 6 to 11
case 6:
fourth = 255;
fifth = phase;
sixth = 0;
break;
case 7:
fourth = 255 - phase;
fifth = 255;
sixth = 0;
break;
case 8:
fourth = 0;
fifth = 255;
sixth = phase;
break;
case 9:
fourth = 0;
fifth = 255 - phase;
sixth = 255;
break;
case 10:
fourth = phase;
fifth = 0;
sixth = 255;
break;
case 11:
fourth = 255;
fifth = 0;
sixth = 255 - phase;
break;
}
}
void setup()
{
pinMode(FirstPin, OUTPUT);
pinMode(SecondPin, OUTPUT);
pinMode(ThirdPin, OUTPUT);
pinMode(FourthPin, OUTPUT);// added these three outputs
pinMode(FifthPin, OUTPUT);
pinMode(SixthPin, OUTPUT);
}
void loop()
{
UpdateLedColours();
analogWrite(FirstPin, first);
analogWrite(SecondPin, second);
analogWrite(ThirdPin, third);
analogWrite(FourthPin, fourth);//added these three pins
analogWrite(FifthPin, fifth);
analogWrite(SixthPin, sixth);
delay(4);
}