I'm thinking about working on a new project with multiple RGB LEDs. what I'd like to be able to do is have the LEDs randomize through a set of predefined colors. The problem is I'm not entirely sure how to go about it. What I did was #define the colors (#define RED 255, 0, 0) then I made an array of the colors
int colorArray[]={WHITE, RED, BLUE, GREEN};
then I created a random number between 0 and the total array number. The problem with this is that when it returns the array it only outputs (if that's the correct term) the first number in the #define. so colorArray[1] returns just 255.
I'm sure my problem is that I only have 1 year of learning c++ coding from over 20+ years ago so I'm probably not doing something right. I'm putting my code below that works so there is an idea of what I'm doing.
what I'd like to do is to have the code pick and display a random color on each LED but only from a predefined set of colors
#include <RGBLed.h>
// LEDS
#define LED1 12, 11, 10
#define LED2 9, 8, 7
#define LED3 6, 5, 4
#define LED4 A0, 2, 3
#define LED5 A3, A2, A1
//Buttons
#define button1 A6
#define button2 A7
//Colors
#define WHITE 255, 255, 255
#define RED 255, 0, 0
#define GREEN 0, 255, 0
#define BLUE 0, 0, 255
#define PURPLE 170, 0, 255
#define LIGHT_BLUE 127, 127, 127
// define LEDS
RGBLed led1(LED1, RGBLed::COMMON_CATHODE);
RGBLed led2(LED2, RGBLed::COMMON_CATHODE);
RGBLed led3(LED3, RGBLed::COMMON_CATHODE);
RGBLed led4(LED4, RGBLed::COMMON_CATHODE);
RGBLed led5(LED5, RGBLed::COMMON_CATHODE);
// Arrays
int outputLEDArray[] = {LED1, LED2, LED3, LED4, LED5};
int outputLEDNumber = sizeof(outputLEDArray) / sizeof(outputLEDArray[0]);
//initialize pins
void initPins() {
//set buttons pins to input
pinMode(button1, INPUT);
pinMode(button2, INPUT);
}
void setup() {
// put your setup code here, to run once:
initPins();
}
void loop() {
// put your main code here, to run repeatedly:
led1.setColor(WHITE);
led2.setColor(RED);
led3.setColor(GREEN);
led4.setColor(BLUE);
led5.setColor(PURPLE);
}