I am trying to control how many LEDS on a NeoPixel ring are lit according to the value of a potentiometer. I have the pot values mapped from 1-12 and would like it to light up the first LED plus one more depending on what mapped value the pot is turned to (i.e. step 1 is one LED lit, step 2 is two LEDS lit, etc.) Currently my code works up to 5 LEDs lighting once it gets to step 5 but when I turn the pot back downwards to steps 4-1 the LEDs are not turning off and are staying lit according to the last step I reached. So far the closest I have come to reaching success is using cases to trigger each step but im sure there may be a better solution I am overlooking. I am using an Arduino UNO with a simple rotary pot and a 12 LED neopixel ring. Do I have to use another function to make it reread the pot value and reset its status?
#include <Adafruit_NeoPixel.h>
#define Pixel 6
int potPin = A0;
int I = 0;
Adafruit_NeoPixel strip(12, Pixel, NEO_RGB + NEO_KHZ800);
uint32_t TEST = strip.Color(150, 150, 150);
void setup() {
strip.begin();
strip.setBrightness(5);
strip.show();
Serial.begin(9600);
}
void loop() {
int I = analogRead(A0);
I = map(I, 0, 1023, 0, 12);
Serial.println(I);
delay(1);
switch (I) {
case 1:
strip.begin();
strip.fill(TEST, 0, 1);
strip.show();
break;
case 2:
strip.begin();
strip.fill(TEST, 0, 2);
strip.show();
break;
case 3:
strip.begin();
strip.fill(TEST, 0, 3);
strip.show();
break;
case 4:
strip.begin();
strip.fill(TEST, 0, 4);
strip.show();
break;
case 5:
strip.begin();
strip.fill(TEST, 0, 5);
strip.show();
break;
}
}
I only have 6 temporarily until I figure out why it is not reversing direction with the LEDS to keep it simple until I solve the issue. Thank you for pointing that old, leftover code from experimenting. Did you have any suggestions? Thank you.
when you fill your strip, you don't turn off the others, so they stay to whatever state they were. you might want to clear the strip first
You indeed don't need the switch/case, just a
strip.clear();
strip.fill(TEST, 0, I); // TEST is a stupid name for a color
Note that with the map function the only way to get the upper bound (12) is if your input is 1023 and that from 0 to 12 you have 13 elements (whilst you have only 12 pixels)
That's what I was going for is for each LED to be lit and stay lit as the pot reaches a higher number, like a volume bar or a progress bar. If I tell it to clear strip would that not cause it to just light then immediately turn off?
Scottdobbs:
That's what I was going for is for each LED to be lit and stay lit as the pot reaches a higher number, like a volume bar or a progress bar. If I tell it to clear strip would that not cause it to just light then immediately turn off?
No the strip state is just a memory representation of a desired state but the actual display happens when you call strip.show();so you can mess around as much as you want with the memory representation, you won't see the strip blink or whatever until you call the function.
ideally you would just test if the pot mapped value has changed, if so, clear all the leds and turn on only the ones you want to see on, in the right color. then call show(). (if you don't want to use clear and fill, you can use a for loop going through each pixel and call setPixelColor() and decide which pixels are lit and which ones are off)