Strange Random Number

Strange Random Number
First post - so please be gentel with me!

Ive got a small bit of code, that sets a random number for Red, Green, Blue to output to a WS2812 LED, the code to output the RGB to the LED works as ive got random colours showing. However when I outout the values of Red, Green, Blue to the serial output, the Blue is a strange number:-
MyRed = 12
MyGreen = 41
MyBlue = 3119

MyRed = 85
MyGreen = 75
MyBlue = 21771

MyRed = 55
MyGreen = 80
MyBlue = 14094

MyRed = 68
MyGreen = 82
MyBlue = 17488

MyRed = 46
MyGreen = 34
MyBlue = 11809

MyRed = 71
MyGreen = 5
MyBlue = 18192

MyRed = 88
MyGreen = 21
MyBlue = 22550

MyRed = 48
MyGreen = 62
MyBlue = 12313

etc

The code is:-

#include "FastLED.h"
#define NUM_LEDS 100
#define DATA_PIN 5
#include <Arduino.h>
// Define the array of leds
CRGB leds[NUM_LEDS];

int MyRed; int MyGreen; int MyBlue; int MyStar;

void setup()
{
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  MyRed = 0;   MyGreen = 0;   MyBlue = 0;

  Serial.begin (9600);
  randomSeed(analogRead(0));

}

void loop()
{
  for (int i = 0; i <= NUM_LEDS; i++)
  {
    MyRed = random(1, 100);
    MyGreen = random(1, 100);
    MyBlue =  random(1, 100);
    leds[i].setRGB(MyGreen, MyRed , MyBlue);
  }
  
  MyStar = random(NUM_LEDS);
  leds[MyStar].setRGB(255, 255, 255);
  
  FastLED.setBrightness(50);
  FastLED.show();
  
  String stringOne= String(MyRed, DEC);
  Serial.println ("MyRed = " + stringOne );

  stringOne= String(MyGreen, DEC);
  Serial.println ("MyGreen = " + stringOne );

  stringOne= String(MyBlue, DEC);
  Serial.println ("MyBlue = " + stringOne );

  Serial.println (" " );

  delay(500);
}

The idea is that the LED's are a random colour, with one bright white (star).

Oddly, if I set MyBlue=0 insted of a random ( MyBlue = 0; // random(1, 100); )
Its MyGreen that changes to Zero:-

MyRed = 40
MyGreen = 0
MyBlue = 10303

MyRed = 22
MyGreen = 0
MyBlue = 5706

MyRed = 94
MyGreen = 0
MyBlue = 24162

MyRed = 36
MyGreen = 0
MyBlue = 9267

MyRed = 56
MyGreen = 0
MyBlue = 14362

MyRed = 52
MyGreen = 0
MyBlue = 13369

Im confused !!!!!!!!!

Just tried this:-
MyRed = 0;
MyGreen = 0;
MyBlue = random(1, 100);
leds*.setRGB(MyGreen, MyRed , MyBlue);*
and the LED's are different brightness's of blue, but the output is showing:
MyRed = 0
MyGreen = 41
MyBlue = 0
MyRed = 0
MyGreen = 38
MyBlue = 0
MyRed = 0
MyGreen = 93
MyBlue = 0
MyRed = 0
MyGreen = 46
MyBlue = 0
MyRed = 0
MyGreen = 25
MyBlue = 0
etc

MadMike42:

  String stringOne= String(MyRed, DEC);

Serial.println ("MyRed = " + stringOne );

stringOne= String(MyGreen, DEC);
  Serial.println ("MyGreen = " + stringOne );

stringOne= String(MyBlue, DEC);
  Serial.println ("MyBlue = " + stringOne );

All those goofy String shenanigans are completely unnecessary. Try this:

Serial.print("MyRed = ");
Serial.println(MyRed);

Serial.print("MyGreen = ");
Serial.println(MyGreen);

Serial.print("MyBlue = ");
Serial.println(MyBlue);

Compare the program size before and after and you might be surprised. Use the F() macro around those string literals to further improve it:

Serial.print(F("MyRed = "));
Serial.println(MyRed);

Serial.print(F("MyGreen = "));
Serial.println(MyGreen);

Serial.print(F("MyBlue = "));
Serial.println(MyBlue);

I use the neopixel library, so fastled might be different, but...something about this line looks odd;

leds.setRGB(MyGreen, MyRed , MyBlue);

If it’s “setRGB”...why are you setting GRB?

As I said, maybe it’s normal for that library, but thought I’d check.

And Delta is right about NUM_LEDS...first led is index zero, so if you have 100, the last one is number 99. Arduino does odd things if you try to write data to the (n+1)th element of an “n” element array...

School boy error on my part
fixed by using < instead of "<="

Thanks for the info on strings - was easier in VB6

Also using RGB in FastLED library, was tryng different colours in setup which is why it was set as GRB.
Also - moved the output to within the loop and all output looks fine.

This is just a test, hoping to make Christmas jumper with LED's - should be fun!

Thanks - Mike

MadMike42:
This is just a test, hoping to make Christmas jumper with LED's - should be fun!

Thanks - Mike

As said I’m new to Arduino, but mostly all I do is tinker with WS2812Bs. If you want to share ideas on colours and sequences I’m happy to do so...I’ve just written a little “shimmer” effect routine with a nice natural sine wave effect. Variables allow the shimmer to be on all three RGB channels (ie multicolour), or “weighted” to any extent up to and including just dimming a single colour. Can also control the speed of the shimmer and the overall brightness.

I’m just looking at adding some variation to the total brightness as well...sigh. This is me. I get frustrated when stuff doesn’t work, then as soon as it does I start changing it again.

Video of the effect here;

Shimmer (set to red)

Same routine, but running in blu/green on a 12x12 grid. Gives a nice watery effect...

Water effect shimmer