Arrays - Randomly choosing an element
In my program so far, I've established some variables (different colours which hold an RGB value).
I'm trying to get my program to randomly choose one of these colours but when I look at the serial port only 1's and 0's are being printed.
I'm new to C and am so lost, if it helps I can write what I want in python.
#include <FastLED.h>
#define LED_PIN 8
#define NUM_LEDS 88
CRGB led[NUM_LEDS];
CRGB green =(0,255,0);
CRGB red =(255,0,0);
CRGB blue =(0,0,255);
CRGB orange =(255,50,0);
CRGB pink =(255,50,80);
CRGB yellow =(255,255,0);
CRGB white =(10,10,10);
CRGB off =(0,0,0);
int codeGen = 0;
//-------------------------------------------------------------------------------------------------------
void setup() {
FastLED.addLeds<WS2812, LED_PIN, GRB>(led, NUM_LEDS);
pinMode(0,INPUT_PULLUP);
pinMode(1,INPUT_PULLUP);
pinMode(2,INPUT_PULLUP);
pinMode(3,INPUT_PULLUP);
pinMode(4,INPUT_PULLUP);
pinMode(5,INPUT_PULLUP);
pinMode(6,INPUT_PULLUP);
pinMode(7,INPUT_PULLUP);
pinMode(9,OUTPUT);
Serial.begin(9600);
}
//-------------------------------------------------------------------------------------------------------
void loop() {
CRGB codeColors[] = {green, red, blue, orange, pink, yellow};
CRGB code[] = {};
for(int i=0; i < 4; i++){
codeGen = random(0,6);
Serial.println(codeColors[codeGen]);
delay(200);
}
}

3 Replies
CRGB
is a struct
ure so you can't just use println()
to display its contents... instead, show the component parts like so:
Literally love you, thank you so much
happy to help... if you're good, do mark this thread as solved 😉