Arduino Nano Nightlight

Hi legends,

Coding newbie here. I’m developing a Nano based 3D printed nightlight for my students.
Each nightlight has a nano, breadboard and a 10 WS2812B light strip.
I want them to be able to program it to fade from one colour to the next, with the students being able to chose the colours.

Is anyone able to help with the code? I have played with the basics of Adafruit and FastLED if that helps.

Thanks!

Hi @Paul246821 - welcome to the forums :slight_smile:

If you’re happy to use FastLED i would use this example, which steps the Hue in an infinite loop. (link)

#include "FastLED.h"
const int numLeds = 10;
CRGB leds[numLeds];

const int ledPin = 6;

void setup() {
  FastLED.addLeds<NEOPIXEL, ledPin>(leds, numLeds); 
}
void loop() { 
  static uint8_t hue = 0;
  FastLED.showColor(CHSV(hue++, 255, 255)); 
  delay(10);
}

here the colour is represented by a single parameter (hue) which you can loop from one value to another to move between two colours. or let it increment forever to continue scrolling through colours.

The brightness is controlled by the V (“value”) parameter, which is the third argument in the CHSV() method eg. to set the brightness to half you would call:

FastLED.showColor(CHSV(hue++, 255, 127));


To adapt this started example to move between two set points you would iterate hue from a start point to an end point. eg.


int hueMin= 0;
int hueMax = 127;
int hue = hueMin;

void loop() {
  while (hue < hueMax ) {
    FastLED.showColor(CHSV(hue++, 255, 255));  // update LEDs and increment hue
    delay(50);
  }
  
  while (hue > hueMin) {
    FastLED.showColor(CHSV(hue--, 255, 255)); // update LEDs and decrement hue
    delay(50);
  }
}

Adafruit have a similar function wheel() in their NeoPixel library if you prefer using that

2 Likes

Michael,

Thanks mate. When you have the ++ next to hue, what’s that doing?

@Paul246821 it’s shorthand for incrementing after the operation.

hue++ is equivalent to

hue = hue + 1;

Now for 3 guess what hue-- does!

1 Like

Here is a code

#include <Adafruit_NeoPixel.h>

#define LED_PIN    6
#define LED_COUNT  10

Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

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

void loop() {
  // Define an array of colors that you want to fade between
  uint32_t colors[] = {strip.Color(255, 0, 0),   // Red
                       strip.Color(0, 255, 0),   // Green
                       strip.Color(0, 0, 255)};  // Blue

  // Calculate the number of colors in the array
  int numColors = sizeof(colors) / sizeof(colors[0]);

  // Calculate the duration to fade between colors (in milliseconds)
  int fadeDuration = 1000;

  // Loop through each color
  for (int i = 0; i < numColors; i++) {
    // Get the current and next color
    uint32_t currentColor = colors[i];
    uint32_t nextColor = colors[(i + 1) % numColors];

    // Fade from the current color to the next color
    fadeBetweenColors(currentColor, nextColor, fadeDuration);
  }
}

void fadeBetweenColors(uint32_t startColor, uint32_t endColor, int duration) {
  // Calculate the number of steps for the fade
  int numSteps = 100;
  int fadeDelay = duration / numSteps;

  // Calculate the amount of change in each color component per step
  int deltaRed = ((endColor >> 16) & 0xFF) - ((startColor >> 16) & 0xFF);
  int deltaGreen = ((endColor >> 8) & 0xFF) - ((startColor >> 8) & 0xFF);
  int deltaBlue = (endColor & 0xFF) - (startColor & 0xFF);

  // Perform the fade
  for (int step = 0; step <= numSteps; step++) {
    // Calculate the new color for the current step
    uint8_t red = ((startColor >> 16) & 0xFF) + (deltaRed * step / numSteps);
    uint8_t green = ((startColor >> 8) & 0xFF) + (deltaGreen * step / numSteps);
    uint8_t blue = (startColor & 0xFF) + (deltaBlue * step / numSteps);
    uint32_t color = strip.Color(red, green, blue);

    // Set the color of all pixels in the strip
    for (int i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, color);
    }
    strip.show();

    // Delay for the fade effect
    delay(fadeDelay);
  }
}

1 Like