Hey folks,
So, after my initial project which involved a single MOSFET powering an RGB strip (all three colors tied together), I started into the next step of wanting to be able to select the particular color instead. I'm thinking of 6 to 8 default colors that can be chosen (Red, green, blue, Yellow, orange, purple, cyan, etc.)
I'm looking for some suggestions. I took my existing code and circuit, added two more MOSFETs to break out each color. This is now my new baseline.
So, thought one was to use a switch/case solution, where depending on the current button count, that will refer to the color I wish to affect in the code (currently fades up, waits 10 min, fades down unless switch is closed sooner than that).
I started to look into functions, but I am thinking the way the code is currently written, I would require a major re-write.
Then I wondered if I would need to instead do an array to carry forward the values of said color.
When all said and done, it seemed to me that I would need to manually write out the code to do what I need for every color instance. I have not figured out the best way to go about this.
So, just asking for suggestions on how to attack this.
Thanks...
FWIW, here is the existing code I am starting from.
#define FADERBLU 3 // MOSFET GATE pin Blue
#define FADERGRN 6 // MOSFET GATE pin Green
#define FADERRED 5 // MOSFET GATE pin Red
#define SWITCH 7 // The contact switch
#define FULLBRIGHTNESSLED 8 // Indictor LED when full brightness reached- Optional
#define LEDONESEC 9 // LED for timer cycle, one 1 sec, off 1 sec durring timer
#define FADESPEED1 20 // Initial Fade up first half. Larger numbers, slower fade up time
#define FADESPEED2 10 // Second half of Fad up. Larger numbers, slower fade up time
int buttonState = 0; // Used to check switch status if door has been closed and reopened.
int lastButtonState = 0; // Used to check switch status if door has been closed and reopened.
int currentState = 0; // Used so that on powerup the dimming feature is not activated once.
void setup()
{
pinMode(FADERBLU, OUTPUT);
pinMode(FADERGRN, OUTPUT);
pinMode(FADERRED, OUTPUT);
pinMode(FULLBRIGHTNESSLED, OUTPUT);
pinMode(LEDONESEC, OUTPUT);
pinMode(SWITCH, INPUT_PULLUP);
digitalWrite(FADERBLU, LOW);
digitalWrite(FADERGRN, LOW);
digitalWrite(FADERRED, LOW);
}
void loop()
{
// Since the switch is set to be tied to ground, LOW state would equal ON when switch is pressed
// In the mean time, the switch is set with the internal pullup resistor, which could be external
buttonState = digitalRead(SWITCH); // Capturing SWITCH state LOW = ON (button pressed)
if (buttonState != lastButtonState && buttonState == LOW) {
// Fade up to 70 (about 27%) at a faster speed
int i;
int switchstatus;
for (i = 0; i < 70; i++) {
analogWrite(FADERBLU, i);
analogWrite(FADERGRN, i);
analogWrite(FADERRED, i);
delay(FADESPEED1);
}
// Fade up to 255 (100%) at a slower speed
for (i = 70; i < 256; i++) {
analogWrite(FADERBLU, i);
analogWrite(FADERGRN, i);
analogWrite(FADERRED, i);
delay(FADESPEED2);
}
digitalWrite(FADERBLU, HIGH);
digitalWrite(FADERGRN, HIGH);
digitalWrite(FADERRED, HIGH);
digitalWrite(FULLBRIGHTNESSLED, HIGH); // Used for testing - Optional
// The Timer function in a FOR loop.
for (int t = 0; t < 300; t++) {
// A separate LED is blinked for 1 second on, 1 second off. This status tells us that the
// loop is running. In this case, the lights will remain on for 10 min. 300 x 2 sec each
// pass = 600 seconds, or 10 minutes.
// During each pass, the switchstatus is checked. If the switch went HIGH (button released
// or door closed in this case), then we break out of loop.
delay(1000);
digitalWrite(LEDONESEC, HIGH);
delay(1000);
digitalWrite(LEDONESEC, LOW);
switchstatus = digitalRead(SWITCH);
if (switchstatus == HIGH) {
// "currentstate" is needed only when first powering up, as the sketch see's the switch
// HIGH (not pressed) and wants to run the dim-down part below.
currentState = 1;
break;
}
}
}
// This is the Fade to-off function. It also checks if this is the first time (thus the IF statement
// , and if so then skip it (only matches this condion on powerup)
if (currentState == 1 && buttonState == HIGH) {
int i;
for (i = 255; i > 0; i--) {
analogWrite(FADERBLU, i);
analogWrite(FADERGRN, i);
analogWrite(FADERRED, i);
delay(FADESPEED2);
currentState = 0;
}
}
analogWrite(FADERBLU, 0);
analogWrite(FADERGRN, 0);
analogWrite(FADERRED, 0);
lastButtonState = buttonState;
digitalWrite(FULLBRIGHTNESSLED, LOW);
}