1

Hi I was wondering if someone is able to help me! I am currently trying to get a strip of 10 neopixels to change colour. I want to be able to code a random neopixel in the strip to change to either red, blue, green and yellow consecutively after 2 seconds. I am relatively new to Arduino so I'm still learning. I have been using the code below and it does everything that I want it to but I would like to set the colour so they're not as randomised. I found this code online.

#include <Adafruit_NeoPixel.h>

#define PIN 6
#define LED_COUNT 50
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, PIN, NEO_RGB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop()
{
  randomPixelColor(100);

}

void randomPixelColor(int wait)
{
  int i= random(strip.numPixels());

  uint32_t c = random(2147483647); // I have no idea what this crazy number does.
  strip.setPixelColor(i, c);
  strip.show();
  delay(1000);

}    // end void randomPixelColor()

I don't even know if this is the right code to be using! Hopefully someone can help me cause I'm so confused.

  • What does "not as randomized" mean? The magic number is equal to 2^31 - 1, though that does not make much sense, since the color of an RGB strip only uses 24 bits, thus a random number covering all the RGB color space, that is available, would have 2^24 -1 as maximum value – chrisl Apr 24 '20 at 11:16
  • I would like it to change from red, to blue, to green, to yellow every 2 seconds, but I would like it to be a random pixel that changes rather than a set one. – Erin Shankland Apr 24 '20 at 11:21

1 Answers1

1

You already are choosing a random LED. Now you only need to define and use the colors, that you need. Define an array with the colors:

#define COLOR_N 4
uint32_t colors[COLOR_N] = {0x00FF0000, 0x000000FF, 0x0000FF00, 0x00FFFF00};

Also define a static or global variable to hold the current color:

uint8_t current_color = 0;

Then use this array in strip.setPixelColor() with the current_color variable as index:

strip.setPixelColor(i, colors[current_color]);

Now increment current_color for the next color. To go from yellow to red again, you can use the modulo operator %:

current_color = (current_color + 1) % COLOR_N;

You can easily extend to more colors, by writing the new colors into the colors array and setting COLOR_N to the corresponding value.


About the color values: The neopixels use an RGB color model with 8 bit depth. That means, that each component (red, green and blue) occupies exactly 1 byte. Thus there are 256 steps for each component. To describe a full color, you need all three components, thus 3 bytes or 24 bits. The data type, that fits this uint32_t (unsigned integer with 32 bit), though the most significant byte is unused here (if you use an RGBW strip, this byte is the component for the white LEDs).

We can write the number, that is representing a full color, as a hexadecimal value. Each component occupies 2 hexadecimal digits:

0x00RRGGBB

where 0x is the prefix, which marks a hexadecimal number, and RR, GG and BB are the components red, green and blue of the color. So a full red is 0x00FF0000, a full blue is 0x000000FF (the leading zeros are not mandatory, but I think they help readability and comparability).

The color mixing works additive here, just like light does. All components mixed together at the same ratio gives you white, all components to zero means black. Thus you get yellow from mixing red and green the same ratio. So a full yellow is 0x00FFFF00.

chrisl
  • 16,257
  • 2
  • 17
  • 27
  • Good answer. However, you should explain where your color values come from. – Duncan C Apr 24 '20 at 12:08
  • Something like: "RGB can be expressed as 3 2-digit hex (hexadecimal) numbers plus 2 leading zeros. (Hex numbers range from 0 to F, so a 2 digit hex number ranges from 00 to FF, or 0 to 255 decimal.) Take an 8 character hex number. The digits would be 00RdGrBl, where "Rd" are the digits for red, "Gr" are the digits for green, and "Bl" are the digits for green. By convention, you write hex numbers with the prefix "0x", which tells the reader (and the compiler) that the number is in hex. So the color 0x00FF0000 has FF, or 255, for red, and 0 for green and blue. This color is bright red. – Duncan C Apr 24 '20 at 12:09
  • The color 0x00FFFF00 has FF (255) for red and green, and no blue. In additive colors, red + green = yellow. (LEDs and displays use additive colors. Paint and dies use the subtractive colors we learned in school, where blue + yellow = green) – Duncan C Apr 24 '20 at 12:11
  • @DuncanC I added a paragraph about this topic – chrisl Apr 24 '20 at 12:36
  • Outstanding. You might want to add a bit about the difference between additive and subtractive colors, since this confuses the hell out of just about everybody since we learn in art class that blue+yellow=green. – Duncan C Apr 24 '20 at 15:33
  • @DuncanC I wouöd prefer not to boat the answer. Color mixing was not a part of the question and the OP did not ask a follow up on this. Also there are many resources that explain that very good – chrisl Apr 24 '20 at 15:37