Below is the complete code for the driver in question. It receives commands from a separate controller that sends an array of the button states, which are then parsed by the controller to switch program modes, brightness, speeds, etc. The main driver then also has two slaves that it sends the light commands to so they all move in synch.
An image of the schematic is attached below as well.
#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
#include <FastLED.h>
RF24 radio(A1,10); // nRF24L01(+) radio attached using Getting Started board
RF24Network network(radio); // Network uses that radio
const uint16_t this_node = 03; // This node ID
const uint16_t NUM_NODES = 2; // Number of slaves
uint16_t Index[NUM_NODES]{ // Slave IDs
Index[0] = 023,
Index[1] = 033};
int mode = 4, max_mode = 5, G = 3, R = 5, B = 6, div_bright = 1, WAIT = 20;
uint8_t COLOR = 0, HUE = 255, BRIGHTNESS = 255;
uint8_t buttons[5]; //Button-array received from Controller
boolean rcvd = false;
void setup()
{
radio.begin();
network.begin(/*channel*/ 90, /*node address*/ this_node);
SPI.begin();
Serial.begin(9600);
pinMode(R, OUTPUT);
pinMode(G, OUTPUT);
pinMode(B, OUTPUT);
colorBars();
}
void loop()
{
/*
Serial.println();
Serial.print("Current Mode: ");
Serial.println(mode);
Serial.println();
*/
check_Net(); // Update network and take subsequent actions
switch (mode){ // Switch-case contains the lighting operations
case 0:
showAnalogRGB( CRGB(0,0,0) );
break;
case 1:
White();
break;
case 2:
ColorWheel();
break;
case 3:
Rand_Color();
break;
case 4:
Twilight();
break;
case 5:
Waver();
break;
}
}
void White (){
showAnalogRGB( CRGB(255, 90, 40) );
}
void ColorWheel(){
showAnalogRGB( CHSV( COLOR, HUE, BRIGHTNESS) );
unsigned long tic = millis();
unsigned long toc = millis() - tic;
while (toc < WAIT){
toc = millis() - tic;
check_Net();
}
COLOR++;
}
void Rand_Color(){
COLOR = random(255);
showAnalogRGB( CHSV( COLOR, HUE, BRIGHTNESS) );
unsigned long tic = millis();
unsigned long toc = millis() - tic;
while (toc < WAIT){
toc = millis() - tic;
check_Net();
}
}
void Twilight(){
int green2 = beatsin16(6, 10, 255);
int green1 = beatsin16(2.5, 50, 200);
int blue1 = beatsin16(0.5, 10, 250);
int blue2 = beatsin16(4.8, 30, 200);
int avg_green = (green1 + green2) / 2;
int avg_blue = (blue1 + blue2) / 2;
showAnalogRGB( CRGB( 255, avg_green, avg_blue) );
}
void Waver(){
int sin_1 = beatsin16(3.2, 50, 255);
int sin_2 = beatsin16(0.5, 60, 255);
int brightness = (sin_1 + sin_2) / 2;
showAnalogRGB( CHSV(5, 190, brightness));
}
void showAnalogRGB( const CRGB& rgb)
{
analogWrite(R, rgb.r );
analogWrite(G, rgb.g );
analogWrite(B, rgb.b );
update_Nodes(rgb);
}
void colorBars()
{
showAnalogRGB( CRGB::Red ); delay(1000);
showAnalogRGB( CRGB::Green ); delay(1000);
showAnalogRGB( CRGB::Blue ); delay(1000);
showAnalogRGB( CRGB::Black ); delay(1500);
}
void update_Nodes (const CRGB& rgb){ // Update Slave modules
uint8_t payload[3];
payload[0] = rgb.r;
payload[1] = rgb.g;
payload[2] = rgb.b;
for (int i = 0; i < NUM_NODES; i++){
RF24NetworkHeader header(/*to node*/ Index[i], 'L');
bool ok = network.write(header,&payload,sizeof(payload));
if (ok){Serial.print("TX, Node: "); Serial.print (Index[i]); Serial.println(" = TRUE");}
else{Serial.print("TX, Node: "); Serial.print (Index[i]); Serial.println(" = FALSE");}
}
}
void check_Net(){ // update newtork and take subsequent action
network.update();
while ( network.available() ) { // Is there anything ready for us?
RF24NetworkHeader header; // If so, take a look at it
network.peek(header);
switch (header.type){ // Dispatch the message to the correct handler.
case 'B': network.read(header,&buttons,sizeof(buttons)); update_Buttons(); break;
}
}
}
void update_Buttons(){
//Serial.println(buttons[0]); // Mode
//Serial.println(buttons[1]); // Off
//Serial.println(buttons[2]); // Brightness
//Serial.println(buttons[3]); // Left pot
//Serial.println(buttons[4]); // Right pot
if (buttons[0] == 1){
buttons[0] = 0;
mode++;
if (mode > max_mode){mode=0;}
}
if (buttons[1] == 1){
buttons[1] = 0;
mode = 0;
return;
}
if (buttons[2] == 1){
buttons[2] = 0;
BRIGHTNESS -= 51;
}
//Serial.println(buttons[0]); // Mode
//Serial.println(buttons[1]); // Off
//Serial.println(buttons[2]); // Brightness
//Serial.println(buttons[3]); // Left pot
//Serial.println(buttons[4]); // Right pot
//Serial.print("Now in Mode: ");
//Serial.println(mode);
return;
}
Unfortunately, now that this problem has been corrected, there's another that's popping up. This time, I believe its regarding the power supply. This driver is controlling cabinet-lighting in the kitchen; there are lights below the main cabinets above the kitchen counter, and also lights illuminating the floor under the counter. These two lines of light are not physically connected and each have their own leads; there are approximately 8-10 meters of LEDs altogether with 60 LEDs/m (each of the two strips are approximately the same length). Assuming a max draw of 1.2 A/m for bright-white, these should top-out at 12A combined. So I decided to test them using a 15A ATX PSU and just took both of the leads for each color channel and connected them together to the MOSFET terminals. So the red from each got screwed into the same mosfet, the blues in theirs, greens, and finally the POS were connected.
When each strip is tested by itself, they work great. When I connected both strips together, I get this weird warbling effect, that's particularly pronounced during blue and green fade in/out.
I'm assuming this has to do with limitations of the power supply, but I'm otherwise at a loss. It seems like if the supplies were getting maxed-out then the LEDs just wouldn't be as bright, like you see with wall-warts. I also tried plugging in a 12V, 10A switched-mode LED PSU which buzzed horribly and also warbled when both lines were hooked up, but did great and worked silently when only one line was running.
I also tried using a separate board that uses the exact same circuit as in the schematic, but with different MOSFETs to no avail. The MOSFETs that were used in each are ISL9N306AD3 and ISL9N306AS3ST.