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); } }
No description
3 Replies
Toph
Toph•3y ago
CRGB is a structure so you can't just use println() to display its contents... instead, show the component parts like so:
Serial.print( codeColors[codeGen].red );
Serial.print( ", " );
Serial.print( codeColors[codeGen].green );
Serial.print( ", " );
Serial.println( codeColors[codeGen].blue );
Serial.print( codeColors[codeGen].red );
Serial.print( ", " );
Serial.print( codeColors[codeGen].green );
Serial.print( ", " );
Serial.println( codeColors[codeGen].blue );
That One Kid
That One KidOP•3y ago
Literally love you, thank you so much
Toph
Toph•3y ago
happy to help... if you're good, do mark this thread as solved 😉

Did you find this page helpful?