Controlling individual LEDs on Neopixel Ring with Potentiometer

Hi there!

I'm using a potentiometer to turn on LEDs in sequence on a Neopixel Ring.

Everything is going well except when I try to assign individual colour values to the LEDs.
When I do this, they do change to the correct colour, but when the potentiometer is changed and the LEDs switch on or off one by one, all the 'off' LEDs flicker on for a millisecond.

Here's the set-up:

#include <Adafruit_NeoPixel.h>
#define PIN 3
Adafruit_NeoPixel strip = Adafruit_NeoPixel(12, PIN, NEO_GRB + NEO_KHZ800);

int potPin = 2;
int val = 0;
int colorVal = 0;
int reading = 0;
int x;
int prevVal = 0;
int switchPin = 6;
boolean lastBtn = LOW;
boolean NeopixelColor = false;
boolean lastButton = LOW;


void setup() {
  // put your setup code here, to run once:
  strip.begin();
  strip.show();
  pinMode(switchPin, INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
  reading = analogRead(potPin);
  val = (reading/1024.0) * 13;
  colorVal = (reading/1024.0) * 255;
  
  if (digitalRead(switchPin) == HIGH && lastButton == LOW)
  {
    delay(250); // Account for contact debounce
    NeopixelColor = !NeopixelColor;
    
  }
  
  if (NeopixelColor == true)
  {
    // Neopixel LED number code
    strip.setBrightness(10);
    if (val != prevVal)
    {
      for ( x = 0; x < val; x++) 
      {
      strip.setPixelColor(x,255,0,255);
      strip.setPixelColor(1,colorVal,0,255,255);
      strip.setPixelColor(2,colorVal,0,0,255);
      strip.setPixelColor(3,colorVal,255,0,255);
      strip.setPixelColor(4,colorVal,0,255,255);
      strip.setPixelColor(5,colorVal,0,255,255);
      strip.setPixelColor(6,colorVal,0,255,255);
      strip.setPixelColor(7,colorVal,0,0,255);
      strip.setPixelColor(8,colorVal,0,255,255);
      strip.setPixelColor(9,colorVal,0,255,255);
      strip.setPixelColor(10,colorVal,0,0,0);
      strip.setPixelColor(11,colorVal,0,0,0);
      strip.setPixelColor(12,colorVal,0,0,0);
      }
      for (x=val; x<13; x++) 
      { 
        strip.setPixelColor(x,0,0,0);
        strip.show();
      }
      prevVal = val;
    }
    else
    {
      strip.show();
    }
    
  }
  else
  {
    // Neopixel Color code
    for (x=255; x < prevVal; x++)
    {
      strip.setPixelColor(x,colorVal,0,0-colorVal);
      strip.show();
    }
  }
}

I really hope someone can help- it's driving me mad!
(Ignore the switch nonsense, it's leftover from a previous project.)

Wait. Is this still a compilable, working sketch?

If it contains a lot of irrelevant details, please remove them yourself before posting because it will take us much longer to figure out what to "ignore".

1 Like

It compiles and works with or without the switch included in the circuit.

This bit is the problem:

      for ( x = 0; x < val; x++) 
      {
      strip.setPixelColor(x,255,0,255);
      strip.setPixelColor(1,colorVal,0,255,255);
      strip.setPixelColor(2,colorVal,0,0,255);
      strip.setPixelColor(3,colorVal,255,0,255);
      strip.setPixelColor(4,colorVal,0,255,255);
      strip.setPixelColor(5,colorVal,0,255,255);
      strip.setPixelColor(6,colorVal,0,255,255);
      strip.setPixelColor(7,colorVal,0,0,255);
      strip.setPixelColor(8,colorVal,0,255,255);
      strip.setPixelColor(9,colorVal,0,255,255);
      strip.setPixelColor(10,colorVal,0,0,0);
      strip.setPixelColor(11,colorVal,0,0,0);
      strip.setPixelColor(12,colorVal,0,0,0);
      }
      for (x=val; x<13; x++)

Originally I only had 1 colour value. Introducing 12 more has caused the flickering in the video below when the LEDs turn on or off.

Diagram for reference.

This is what it looks like when the code is simply:

      for ( x = 0; x < val; x++) 
      {
      strip.setPixelColor(x,255,0,255);
      }
      for (x=val; x<13; x++) 

Here is a video of that working:

can you explain the meaning of this line?

method setPixelColor in general has two parameters - the first is pixel number and the second is color. Color may be represent as 3 rgb values - red green blue. So the setPixelColor can accept 4 parameters, but you putted five in it.

What are you trying to achieve?

1 Like

In order to sequentially change colors using a potentiometer, it is best to transform color values from RGB to HSV scale and gradually increment HUE component.
But what are you doing in the code - is absolutely senseless.

1 Like

I'm trying to get each of the LEDs to have its own individual, constant colour when it turns on. A rainbow, if you please.
At the moment, when the LEDs turn on incrementally, all the other LEDs blink at the same time.

As for that code:

strip.setPixelColor(7,colorVal,0,0,255);

I 'borrowed' that from somebody else. I should add that this is my second project, I am a newcomer to arduino.

I may have to leave it as is because I don't understand changing from RGB to HSV.

we all do not understand something at first, but then we can learn.

Whatever else is causing trouble, the above might also. There is no need to strip.show() within the loop.

Set all you pixel values, then do once strip.show().

    for (x=val; x<13; x++) 
    { 
         strip.setPixelColor(x,0,0,0);
    }

    strip.show();

HTH

a7

1 Like

A thousand thanks!

That worked perfectly, problem solved! :smiley:

Encountered my next problem:

I am running 2 neopixel rings but I need the lights to turn on in opposite directions.

At the moment, when the potentiometer is turned, both rings light up anti-clockwise.

I would like one turning clockwise and the other anti-clockwise.

#include <Adafruit_NeoPixel.h>
#define PIN 3
Adafruit_NeoPixel stripA = Adafruit_NeoPixel(12, PIN, NEO_GRB + NEO_KHZ800);
#define PIN 4
Adafruit_NeoPixel stripB = Adafruit_NeoPixel(12, PIN, NEO_GRB + NEO_KHZ800);

int potPinA = 2;
int val = 0;
int colorVal = 0;
int reading = 0;
int x;
int y;
int prevVal = 0;
int switchPin = 6;
boolean lastBtn = LOW;
boolean NeopixelColor = false;
boolean lastButton = LOW;


void setup() {
  // put your setup code here, to run once:
  stripA.begin();
  stripB.begin();
  stripA.show();
  stripB.show();
  pinMode(switchPin, INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
  reading = analogRead(potPinA);
  val = (reading/1075.0) * 13;
  colorVal = (reading/1024.0) * 255;
  
  if (digitalRead(switchPin) == HIGH && lastButton == LOW)
  {
    delay(250); // Account for contact debounce
    NeopixelColor = !NeopixelColor;
    
  }
  
  if (NeopixelColor == true)
  {
    // Neopixel LED number code
    stripA.setBrightness(20);
    stripB.setBrightness(20);
    if (val != prevVal)
    {
      for (x = 0; x < stripA.numPixels(); x++)
       for (x = 0; x < stripB.numPixels(); x++)
   
        
      {
      stripA.setPixelColor(x,33,10,119);
      stripA.setPixelColor(1,colorVal,43,165,97);
      stripA.setPixelColor(2,colorVal,55,179,193);
      stripA.setPixelColor(3,colorVal,110,212,225);
      stripA.setPixelColor(4,colorVal,101,211,246);
      stripA.setPixelColor(5,colorVal,68,150,254);
      stripA.setPixelColor(6,colorVal,86,44,249);
      stripA.setPixelColor(7,colorVal,208,61,252);
      stripA.setPixelColor(8,colorVal,212,3,253);
      stripA.setPixelColor(9,colorVal,190,4,234);
      stripA.setPixelColor(10,colorVal,190,4,234);
      stripA.setPixelColor(11,colorVal,0,0,255);
      stripA.setPixelColor(12,colorVal,0,0,0);
      stripB.setPixelColor(0,33,10,119);
      stripB.setPixelColor(11,colorVal,0,255,0);
      stripB.setPixelColor(10,colorVal,55,179,193);
      stripB.setPixelColor(9,colorVal,110,212,225);
      stripB.setPixelColor(8,colorVal,101,211,246);
      stripB.setPixelColor(7,colorVal,68,150,254);
      stripB.setPixelColor(6,colorVal,86,44,249);
      stripB.setPixelColor(5,colorVal,208,61,252);
      stripB.setPixelColor(4,colorVal,212,3,253);
      stripB.setPixelColor(3,colorVal,190,4,234);
      stripB.setPixelColor(2,colorVal,190,4,234);
      stripB.setPixelColor(1,colorVal,0,0,255);
      stripB.setPixelColor(1,colorVal,0,0,0);
      stripB.setPixelColor(0,colorVal,0,0,0);
      
     
      }
      for ( x=val; x<13; x++) 
      { 
        stripA.setPixelColor(x,0,0,0);
        stripB.setPixelColor(x,0,0,0);
      }
      stripA.show();
      stripB.show();
      
      prevVal = val;
    }
    else
    {
      stripA.show();
      stripB.show();
    }
    
  }
  else
  {
    // Neopixel Color code
    for (x=255; x < prevVal; x++)
    {
      stripA.setPixelColor(x,colorVal,0,0-colorVal);
      stripB.setPixelColor(x,colorVal,0,0-colorVal);
      stripA.show();
      stripB.show();
    }
  }
}

I know it is something to do with this, but changing the values to negative has not effect.

 for (x = 0; x < stripA.numPixels(); x++)
       for (x = 0; x < stripB.numPixels(); x++)

I have tried the solution below with no success.

for (x = 0; x < stripA.numPixels(); x++)
       for (x >= 0; x > stripB.numPixels(); x--)
      for ( x=val; x<13; x++) 
      { 
        stripA.setPixelColor(x,0,0,0);
        stripB.setPixelColor(12 - x,0,0,0);
      }

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