Hi - I wonder if the forum could help me learn a better, more efficient method to accomplish this task?
I want to use a neoPixel strip as part of a gauge. I'm using a neoPixel ring (24 LEDs) but I want to use different ring segments for different measurements. I have incorporated an offset so I can call the function and start at LED x and then sweep for y.
this use case is: shows either an increasing trend in temperature or a decreasing one.
I need to sweep/chase the LED from left to right (increase) and right to left (decrease)
I would like to improve the look of the chase effect by dimming the trailing LEDs where leading LED is 100%, -1 = 75%, -2 = 50% , -3 = 25% and -4 = 0% (being able to adjust these for best effect. [Yep i was going for 80's throwback 'KITT car' style ]
This code functions as expected, but I think there are smarter ways to achieve it.
Could anyone offer advice on how I should improve this, please?
Future thought. This also locks up the code while it sweeps, so making it non-blocking but maintaining timing through each sweep would be a nice addition
NB code adapted from Adafruit NeoPixel Library example.
NBB I'm testing with a 10 pixel strip (not 24) so avoiding the offset and sweep range for now.
#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
#define LED_COUNT 10 // Number of pixels on the strip
#define LED_PIN 13
// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
void sweepfwd(uint32_t r, uint32_t g, uint32_t b, int wait, int offset, int PIX_COUNT)
{
float dim1 = 0.65;
float dim2 = 0.4;
float dim3 = 0.15;
for (int j = offset; j < PIX_COUNT + offset; j++)
{
strip.clear();
strip.setPixelColor(j + offset, strip.Color(r, g, b)); // Set pixel 'c' to value 'color'
for (int i = 0; i < 4; i++)
{
if (j > 0)
{
strip.setPixelColor(j + offset - 1, strip.Color(r * dim1, g * dim1, b * dim1)); // Set pixel 1 behind to value 'color' at dim1 brightness
}
if (j > 1)
{
strip.setPixelColor(j + offset - 2, strip.Color(r * dim2, g * dim2, b * dim2)); // Set pixel 2 behind to value 'color' at dim2 brightness
}
if (j > 2)
{
strip.setPixelColor(j + offset - 3, strip.Color(r * dim3, g * dim3, b * dim3)); // Set pixel 3 behind to value 'color' at dim3 brightness
}
if (j >= 3)
{
strip.setPixelColor(j + offset - 4, strip.Color(0, 0, 0)); // Set pixel 'c' to value 'color'
}
}
strip.show(); // Update strip with new contents
delay(wait); // Pause for a moment
}
}
void sweepbwd(uint32_t r, uint32_t g, uint32_t b, int wait, int offset, int PIX_COUNT)
{
float dim1 = 0.65;
float dim2 = 0.4;
float dim3 = 0.1;
int colour = 0;
int PIX_START = PIX_COUNT + offset;
for (int j = offset; j < PIX_START; j++)
{
strip.clear();
colour = strip.Color(r, g, b);
strip.setPixelColor(PIX_START - j, colour); // Set pixel 'c' to value 'color'
for (int i = 0; i < 4; i++)
{ // conditional test of LED to make sure we don't turn on any pixel before
if (j > 0)
{
colour = strip.Color(r * dim1, g * dim1, b * dim1);
strip.setPixelColor(PIX_START - j + 1, colour); // Set pixel 1 behind to value 'color' at dim1 brightness
}
if (j > 1)
{
colour = strip.Color(r * dim2, g * dim2, b * dim2);
strip.setPixelColor(PIX_START - j + 2 , colour); // Set pixel 2 behind to value 'color' at dim2 brightness
}
if (j > 2)
{
colour = strip.Color(r * dim3, g * dim3, b * dim3);
strip.setPixelColor(PIX_START- j +3 , colour); // Set pixel 3 behind to value 'color' at dim3 brightness
}
if (j >= 3)
{
colour = 0;
strip.setPixelColor(PIX_START - j + 4, colour); // Set pixel 4 behind to off
}
}
strip.show(); // Update strip with new contents
delay(wait); // Pause for a moment
}
}
void setup()
{
Serial.begin(115200);
strip.begin();
strip.show();
strip.setBrightness(50);
}
void loop()
{
sweepfwd(255, 255, 255, 75, 0, LED_COUNT);
delay(1);
sweepbwd(255, 255, 255, 75, 0, LED_COUNT);
delay(1);
}