Working on a small project and am having a brain fart. I have 6 LED strands each on its own PWM channel. Everything in the attached code works as expected, however I want to make a change so that when it is "fade" mode, each strand will have a different brightness, and there will never be a point where more than 1 strand at a time is at 0 - basically a random wave pattern that never sees 0 on more than one leg. The two ways I see doing this are either adding timer or brightness variables to each strand, and either way i feel like I am taking the long way around the barn...
//Button Definitiions
#define Button1 2 //star button
byte ButtonPresses1 = 0; //variable - how many times the button has been pressed
byte Cycle1 = 0; //variable - determines fade, off or on constant
//Timer Definitions
unsigned long Interval1 = 300; //interval for change
unsigned long PrevMil = 0; //Variable - time
//Star Definitions
int FadeAmount = 5; //variable - fade step
int Brightness = 0; //variable - brightness step
int MaxBrightness = 0; //variable - maximum brightness level
#define s1 3 //star strand 1
#define s2 5 //star strand 2
#define s3 6 //star strand 3
#define s4 9 //star strand 4
#define s5 10 //star strand 5
#define s6 11 //star strand 6
void setup() {
//Debugging
Serial.begin(9600);
//button setup
pinMode(Button1, INPUT); //sets button pin as input
digitalWrite(Button1, HIGH); //sets pull-up resistor
//star setup
pinMode(s1, OUTPUT); //sets star pin as output
pinMode(s2, OUTPUT); //sets star pin as output
pinMode(s3, OUTPUT); //sets star pin as output
pinMode(s4, OUTPUT); //sets star pin as output
pinMode(s5, OUTPUT); //sets star pin as output
pinMode(s6, OUTPUT); //sets star pin as output
}
void loop() {
//Timer setup
unsigned long CurMil = millis(); //grabs the current time
//button control
if (digitalRead(Button1) == LOW) //Check if button is pressed
{
ButtonPresses1++; //adds to the button count
delay(250); //debounce control
}
if (ButtonPresses1 == 0 || ButtonPresses1 == 6) {
ButtonPresses1 = 0; //resets button count and cycle on 5th press
Cycle1 = 0;
}
if (ButtonPresses1 == 1) {
MaxBrightness = 65; //sets max brightness to 25% and cycle on press 1
Cycle1 = 1;
}
if (ButtonPresses1 == 2) {
MaxBrightness = 135; //sets max brightness to 50% and cycle on press 2
Cycle1 = 1;
}
if (ButtonPresses1 == 3) {
MaxBrightness = 195; //sets max brightness to 75% and cycle on press 3
Cycle1 = 1;
}
if (ButtonPresses1 == 4) {
MaxBrightness = 255; //sets max brightness to 100% and cycle on press 4
Cycle1 = 1;
}
if (ButtonPresses1 == 5) {
Cycle1 = 2; //sets cycle for full on on press 5
}
//star control
//cycle control - 0 is all off, 1 is fading, 2 is all on
switch (Cycle1) {
case 0: //All stars off
analogWrite(s1, 0);
analogWrite(s2, 0);
analogWrite(s3, 0);
analogWrite(s4, 0);
analogWrite(s5, 0);
analogWrite(s6, 0);
break;
case 1: //stars fading
if ((unsigned long)(CurMil - PrevMil) >= Interval1) {
PrevMil = millis();
if (Brightness > MaxBrightness) {
Brightness = MaxBrightness;
}
analogWrite(s1, Brightness);
analogWrite(s2, Brightness);
analogWrite(s3, Brightness);
analogWrite(s4, Brightness);
analogWrite(s5, Brightness);
analogWrite(s6, Brightness);
//brightness stepping
Brightness = Brightness + FadeAmount;
//brightness reversal
if (Brightness <= 0 || Brightness >= MaxBrightness) {
FadeAmount = -FadeAmount;
} }
break;
case 2: //All stars on
analogWrite(s1, 255);
analogWrite(s2, 255);
analogWrite(s3, 255);
analogWrite(s4, 255);
analogWrite(s5, 255);
analogWrite(s6, 255);
break;
}
//debugging
Serial.print ("Brightness =");
Serial.println(Brightness, DEC);
Serial.print ("Fade Amount =");
Serial.println(FadeAmount, DEC);
Serial.print ("Button Press =");
Serial.println(ButtonPresses1, DEC);
Serial.print("Max Brightness =");
Serial.println(MaxBrightness, DEC);
Serial.print("Cycle =");
Serial.println(Cycle1, DEC);
}
Thoughts?
t