Need Help, trying to make a neopixel flash (predetermined) random colors

I'm really close but I must be missing something, I predetermine my colors with lines like this:

#define pink 255,80,80
#define blue 50,80,255
#define yellow 255,120,25

and I have a function to pick which color at random like this:

void colorPicker(){
int Choice=random(0,5);
Serial.print("Choice ");
Serial.print(Choice);
Serial.print(" ,");
switch(Choice) {
case 0:
chosenColor = pink;
break;
case 1:
chosenColor = blue;
break;
case 2:
chosenColor = yellow;
break;
case 3:
chosenColor = purple;
break;
case 4:
chosenColor = green;
break;
case 5:
chosenColor = orange;
break;
}
Serial.print("chosenColor ");
Serial.println(chosenColor);
}

The serial monitor returns data that looks like this:

Choice 3 chosenColor 120
Choice 3 chosenColor 120
Choice 2 chosenColor 255

But I expect a line that says something like this instead:

Choice 0 chosenColor pink

Anyone have a clue what I could be doing wrong here? I've been trying to crack this all day and I just feel defeated.

edit: I forgot, the colorPicker function needs to be used by this function here:

void randomColors(){
  int IncrementPixels = PIXEL_COUNT /2;
  for(int i=0; i<IncrementPixels; i++) {
    int j = map (i,0,PIXEL_COUNT,PIXEL_COUNT,0);
    colorPicker();
    strip.setPixelColor(i, chosenColor);
    colorPicker();
    strip.setPixelColor(j-1, chosenColor);
    strip.show();                          //  Update strip to match
    delay(FadeTime);  
  }
  colorWipe(strip.Color(  0,   0,   0), FadeTime);
}

It is much better if you post all your code, and give a lot more details about the processor you have and how it is wired up,

You might want to look at this How to get the best out of this forum before you proceed any further.
We only know what you tell us, and without knowing what you have, we don't stand a chance.

That just makes the colour black, hard to see.

You must define all the colours you use, not just some of them.

Sorry, I am using a gemma m0 with the neopixel strip attached to A0 and a button to trigger it attached to A2.
my code is mostly from the neopixel buttoncycler example from the library.
here is my full code:

#include <Adafruit_DotStar.h> // Dot Star Library to control local led
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

#define BUTTON_PIN   A2  // Trigger
#define PIXEL_PIN    A0  // Digital IO pin connected to the Pixels.
#define PIXEL_COUNT 42  // Number of Pixels

// Define colors
#define pink 255,80,80
#define blue 50,80,255
#define yellow 255,120,25
#define purple 120,50,255
#define green 50,255,70
#define orange 255,25,5

char chosenColor[6];

Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800); //Pixel Strip

Adafruit_DotStar DotStar_strip(DOTSTAR_NUM, PIN_DOTSTAR_DATA, PIN_DOTSTAR_CLK, DOTSTAR_BRG);//Dot star (local LED)

boolean oldState = HIGH;
int     mode     = 0;    // Currently-active animation mode, 0-9
int FadeTime = 5;
void setup() {
  Serial.begin(9600);
  Serial.println("Starting");
  //(disable local LED)
  delay (1000);
  randomSeed(analogRead(0));
  DotStar_strip.begin(); // Initialize pin for output
  DotStar_strip.setBrightness(80);
  DotStar_strip.show();  // Turn all LEDs off ASAP

  //normal startup code
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  strip.begin(); // Initialize NeoPixel strip object (REQUIRED)
  strip.show();  // Initialize all pixels to 'off'
}


void loop() {
  boolean newState = digitalRead(BUTTON_PIN); // Get current button state.
  if((newState == LOW) && (oldState == HIGH)) { // Check if state changed from high to low (button press).
    delay(5); // Short delay to debounce button.
    newState = digitalRead(BUTTON_PIN); // Check if button is still low after debounce.
    if(newState == LOW) {  
     randomColors();
    }
  }

  // Set the last-read button state to the old state.
  oldState = newState;
}

// Fill strip pixels one after another with a color. Strip is NOT cleared
// first; anything there will be covered pixel by pixel. Pass in color
// (as a single 'packed' 32-bit value, which you can get by calling
// strip.Color(red, green, blue) as shown in the loop() function above),
// and a delay time (in milliseconds) between pixels.
void colorWipe(uint32_t color, int wait) {
  int IncrementPixels = PIXEL_COUNT /2;
  for(int i=0; i<IncrementPixels; i++) { // For each pixel in strip...   //strip.numPixels()
    int j = map (i,0,PIXEL_COUNT,PIXEL_COUNT,0);
    strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
    strip.setPixelColor(j-1, color);         //  Set pixel's color (in RAM)
    strip.show();                          //  Update strip to match
    delay(wait);                           //  Pause for a moment
  }
}
void randomColors(){
  int IncrementPixels = PIXEL_COUNT /2;
  for(int i=0; i<IncrementPixels; i++) {
    int j = map (i,0,PIXEL_COUNT,PIXEL_COUNT,0);
    colorPicker();
    strip.setPixelColor(i, chosenColor);
    colorPicker();
    strip.setPixelColor(j-1, chosenColor);
    strip.show();                          //  Update strip to match
    delay(FadeTime);  
  }
  colorWipe(strip.Color(  0,   0,   0), FadeTime);
}
void colorPicker(){
  int Choice=random(0,5);
  Serial.print("Choice ");
  Serial.print(Choice);
  switch(Choice) {  
        case 0:
        chosenColor = pink;
        break;
        case 1:
        chosenColor = blue;
        break;
        case 2:
        chosenColor = yellow;
        break;
        case 3:
        chosenColor = purple;
        break;
        case 4:
        chosenColor = green;
        break;
        case 5:
        chosenColor = orange;
        break;
  }
  Serial.print("chosenColor ");
  Serial.println(chosenColor);
}

thanks for trying to help, I appreciate it.

The colorwipe line is because it's supposed to flash the colors than black out after.

The #define just does a text substitution. For example

chosenColor = yellow;

will be

chosenColor = 255,120,25;

The compiler will take the first value and ignore the rest.

You probably want to define some color objects and use them.

RgbColor red(255, 0, 0);
RgbColor green(0, 255, 0);

and assign them to variables

RgbColor leftColor = red;
RgbColor rightColor = green;

ok this helps, but I'm not understanding, where or how do you then declare leftColor and rightColor?

Those are the definitions and initialization to default values. Later you can modify them or set the color on the strip.

To set them from a web browser

  if (server.hasArg("lred")) {
    // we have input data - assume they are all set 
    leftColor = RgbColor(server.arg("lred").toInt(),server.arg("lgreen").toInt(),server.arg("lblue").toInt());
    rightColor = RgbColor(server.arg("rred").toInt(),server.arg("rgreen").toInt(),server.arg("rblue").toInt());
    rate = server.arg("rate").toInt();
    firstTime = true;
  }

To set the colors in the strip

        strip.SetPixelColor(i+j, leftColor );
        strip.SetPixelColor(pixelCount-(i+j+1), rightColor);

The code fragments are from my color collider. Look here

I got it working, I just moved the setPixelcolor line from the random colors function into each of the case switches in the colorpicker function then created a second random number generator and case switch for the other half of the strip.

like this:

void randomColors(){
  int IncrementPixels = PIXEL_COUNT /2;
  for(int i=0; i<IncrementPixels; i++) {
    int j = map (i,0,PIXEL_COUNT,PIXEL_COUNT,0);
    increment=i;
    jincrement=j;
    colorPicker();
    //strip.setPixelColor(i, rgbColor);
    //colorPicker();
    //strip.setPixelColor(j-1, rgbColor);
    strip.show();                          //  Update strip to match
    delay(FadeTime);  
  }
  colorWipe(strip.Color(  0,   0,   0), FadeTime);
}
void colorPicker(){
  int Choice1=random(0,5);
  int Choice2=random(0,5);
  //Serial.print("Choice ");
  //Serial.print(Choice);
  switch(Choice1) {  
        case 0:
        strip.setPixelColor(increment, pink);
        break;
        case 1:
        strip.setPixelColor(increment, blue);
        break;
        case 2:
        strip.setPixelColor(increment, yellow);
        break;
        case 3:
        strip.setPixelColor(increment, purple);
        break;
        case 4:
        strip.setPixelColor(increment, green);
        break;
        case 5:
        strip.setPixelColor(increment, orange);
        break;
  }
  switch(Choice2) {  
        case 0:
        strip.setPixelColor(jincrement, pink);
        break;
        case 1:
        strip.setPixelColor(jincrement, blue);
        break;
        case 2:
        strip.setPixelColor(jincrement, yellow);
        break;
        case 3:
        strip.setPixelColor(jincrement, purple);
        break;
        case 4:
        strip.setPixelColor(jincrement, green);
        break;
        case 5:
        strip.setPixelColor(jincrement, orange);
        break;
  }
  //Serial.print("chosenColor ");
  //Serial.println(chosenColor);
}

and definitions are like this:

#define pink 255,80,80
#define blue 50,80,255
#define yellow 255,120,25
#define purple 120,50,255
#define green 50,255,70
#define orange 255,25,5
1 Like

If you are looking to dial-in a color and know it's value, here's a simulation for that...

Thank you very much, this is a really cool tool.

This is one of many that turn up googling

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.