So basically, I am coding an arduino to code LEDs and I got an error and I am unable to figure out what I did wrong. I do need to add a few things but first I have to fix this code.
This is the error:
In function 'void setup()':
coolLED:42:56: error: invalid use of void expression
leds[i] = CRGB(srand(255), srand(255), srand(255)); // sets LEDs to a random color
^
exit status 1
invalid use of void expression
And then this is the code.
#include <stdio.h>
#include <stdlib.h>
#include <FastLED.h>
#define DATA_PIN 6
#define NUM_LEDS 150
#define DELAY 1
int color = 1;
int i = 0;
int o = 0;
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
void setup() {
pinMode(6, OUTPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
pinMode(10, INPUT);
Serial.begin(9600);
FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
int r = 0;
int g = 0;
int b = 0;
FastLED.setBrightness(100);
for (o = 0; o < 100; o ) {
for (i = 0; i < NUM_LEDS; i ) {
leds[i] = CRGB(srand(255), srand(255), srand(255)); // sets LEDs to a random color
}
}
if (color = 1) {
for (o = 0; o < 100; o ) {
for (i = 0; i < NUM_LEDS; i ) {
leds[i] = CRGB(0, 0, 255); // sets LEDs to blue
}
FastLED.setBrightness(200 - i);
}
} else {
for (o = 0; o < 100; o ) {
for (i = 0; i < NUM_LEDS; i ) {
leds[i] = CRGB(255, 0, 0); // sets LEDs to red
}
FastLED.setBrightness(200 - i);
}
}
}
I really don't know where to start. I haven't had enough experience to fix this.
CodePudding user response:
Sorry, this is just a trivial oversight: srand()
("seed rand") is a void function setting the seed of the pseudo random number generator (that is, the position in the deterministic sequence of numbers successive calls to rand()
will produce), hence the error. You probably wanted to use rand()
.
On the upside, it is easily corrected. Happy hacking! :-)
[Oh, I see only now that afarrag had made a comment to that effect already. Anyway, the answer will be more visible.]
CodePudding user response:
Moving my comments to an answer:
Function srand
returns void
and is used to set the seed. You probably want to use rand
instead. Have a look at their manual page.
This may resolve your issue:
leds[i] = CRGB(rand()%6, rand()%6, rand()%6);
CodePudding user response:
All I had to do was put random() instead of the other random function. I misunderstood the use of functions.