Hello Everyone
I'm sorry if this question has been asked before, I have searched the forum and read through others posts but are still confused. I'm new to Arduino programming.
I am playing around with an RGB led strip with a UNO and I'm trying to get a random led position from an array, its not working and I cant quite figure out what I'm doing wrong.
[code]
#include <FastLED.h>
#define NUM_LEDS 60
#define DATA_PIN 5
CRGB leds[NUM_LEDS];
long randled;
long randledcolor;
int r = 0;
int g = 0;
int b = 0;
//int ledposArray[5] = {0, 15, 30, 45, 59};
void setup() {
// put your setup code here, to run once:
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
}// close void setup()
void loop() {
int ledposArray[5] = {0, 15, 30, 45, 59};
randled = random(60);
//randled = ledArray[5];
ledposArray[5] = random();
r = random(0,255);
g = random(0,255);
b = random(0,255);
// put your main code here, to run repeatedly:
//leds[randled].setRGB( r, g, b);
leds[ledposArray[5]].setRGB( r, g, b);
FastLED.show();
leds[randled] = CRGB::Black;
delay(1000);
}
[/code]
int ledposArray[5] = {0, 15, 30, 45, 59};
ledposArray[5] = random(); // this write outside the bounds of the array
Your array, ledposArray, has 5 elements numbered 0 - 4.
What do you expect random() to return?
What is that code supposed to do?
Do you want to choose one of the ledposArray array elements at random? See comments about random seed.
void setup()
{
Serial.begin(9600);
// need randomSeed to start the random sequence at different number each time the Arduino starts
// see the reference
//https://www.arduino.cc/reference/en/language/functions/random-numbers/randomseed/
randomSeed(analogRead(A0));
}
void loop()
{
int ledposArray[5] = {0, 15, 30, 45, 59};
int randomElement = ledposArray[random(0,5)];
Serial.print(" random element of ledposArray = ");
Serial.println(randomElement);
delay(1000);
}
/code]
groundFungus:
int ledposArray[5] = {0, 15, 30, 45, 59};
ledposArray[5] = random(); // this write outside the bounds of the array
Your array, ledposArray, has 5 elements numbered 0 - 4.
What do you expect random() to return?
What is that code supposed to do?
Do you want to choose one of the ledposArray array elements at random?
Thanks for the reply
The ledposarray is 5 of the positions of a 60 led rgb strip, when I put a 5 in the random you mentioned the code only lights up the first 5 leds (position 0,1,2,3,4) of the 60 led strip and not any from the ledposArray
What I'm trying to achieve is an understanding of how to use Arrays in Arduino programming and in this project I want to randomly light up leds from the array that represents positions 0, 15, 30, 45, 59 of the 60 leds on the strip
Did the code that I posted help?
groundFungus:
Did the code that I posted help?
Yes it did. Thankyou and I incorporated that into the code below
so for my understanding we have used randomElement to define ledposArray[random(0,5) which randomly chooses one of the numbers from ledposArray, So any other Arrays introduced into this code would need to be defined in the same way but with a different name?
[code]
#include <FastLED.h>
#define NUM_LEDS 60
#define DATA_PIN 5
CRGB leds[NUM_LEDS];
long randled;
void setup() {
// put your setup code here, to run once:
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
}
void loop() {
// put your main code here, to run repeatedly:
int ledposArray[5] = {0, 15, 30, 45, 59};
int randomElement = ledposArray[random(0,5)];
randled = random(60);
//leds[randled] = CRGB::Red;
leds[randomElement] = CRGB::Red;
FastLED.show();
leds[randomElement] = CRGB::Black;
delay(1000);
}
[/code]