potentiometer does not adjust rgb LPD8806.h

I have setup a potentiometer to change the LED strip. But I have ran into to issue.

  1. When changing the potentiometer the LED stripe updates, but it the rgb does not update in the same response time and therefore some colors get missed out.

  2. Currently I have an if statement which also updated the R G B. But because the potentiometer bounces, the if statement get triggered changing the color

If anyone has ideas that would be greatly appreciated.

#include “LPD8806.h”
#include “SPI.h” // Comment out this line if using Trinket or Gemma
#ifdef AVR_ATtiny85
#include <avr/power.h>
#endif

// Example to control LPD8806-based RGB LED Modules in a strip

/*****************************************************************************/

// Number of RGB LEDs in strand:
int nLEDs = 12;

// Chose 2 pins for output; can be any valid output pins:
int dataPin = 2;
int clockPin = 3;
// mode for chnaging the color
int colorCounter = 1;
//button to change mode
int switchPin = 7;
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button

int const potPin = A0; // select the input pin for the potentiometer
int val; // variable to store the value coming from the sensor
int previousVal = 0;

int r = 255;
int g, b = 0;

// First parameter is the number of LEDs in the strand. The LED strips
// are 32 LEDs per meter but you can extend or cut the strip. Next two
// parameters are SPI data and clock pins:
LPD8806 strip = LPD8806(nLEDs, dataPin, clockPin);
//LPD8806 strip = LPD8806(nLEDs);

void setup() {
#if defined(AVR_ATtiny85) && (F_CPU == 16000000L)
clock_prescale_set(clock_div_1); // Enable 16 MHz on Trinket
#endif
pinMode(switchPin ,INPUT);
Serial.begin(9600);
strip.begin();
strip.show();
}

void loop() {
val = analogRead(potPin);

if (val > previousVal){
Serial.print("increasing in number: ");
Serial.println(val);
changeColor(val);

previousVal = val;
} else if (val < previousVal) {
Serial.println(“decreasing in number”);
changeColor(val);
previousVal = val;

} else {
Serial.print(“not chnaging colors”);
changeColor(val);
Serial.println(val);
}

colorCustom(strip.Color(r, g, b), 50);

}
void changeColor(int val){
if (val <= 384){
g += 1;
} else if (val > 384 && val <= 512 ){
r -= 1;
} else if (val > 512 && val <= 640 ){
b += 1;
} else if (val > 640 && val <= 768 ){
g -= 1;
} else if (val > 768 && val <= 1024 ){
r += 1;
}Serial.print(“r :”);
Serial.println(r);
Serial.print("g : ");
Serial.println(g);
Serial.print(“B :”);
Serial.println(b);
Serial.println(val); // stop the program for some time

}
void goThroughColors(int val){
if (val <= 384){
g += 1;
} else if (val > 384 && val <= 512 ){
r -= 1;
} else if (val > 512 && val <= 640 ){
b += 1;
} else if (val > 640 && val <= 768 ){
g -= 1;
} else if (val > 768 && val <= 1024 ){
r += 1;
}

// colorCustom(strip.Color(r, g, b), 50); // Violet
colorCustom(strip.Color(127, 0, 127), 50);
};

void colorCustom(uint32_t c, uint8_t wait) {
int i;

// Start by turning all pixels off:
for(i=0; i<strip.numPixels(); i++) strip.setPixelColor(i, 0);

// Then display one pixel at a time:
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c); // Set new pixel ‘on’
// Refresh LED states
// Erase pixel, but don’t refresh!
delay(wait);
}

strip.show(); // Refresh to turn off last pixel
}
void rainbow(uint8_t wait) {
int i, j;

for (j=0; j < 384; j++) { // 3 cycles of all 384 colors in the wheel
for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel( (i + j) % 384));
}
strip.show(); // write all the pixels out
delay(wait);
}
}

// Slightly different, this one makes the rainbow wheel equally distributed
// along the chain
void rainbowCycle(uint8_t wait) {
uint16_t i, j;

for (j=0; j < 384 * 5; j++) { // 5 cycles of all 384 colors in the wheel
for (i=0; i < strip.numPixels(); i++) {
// tricky math! we use each pixel as a fraction of the full 384-color wheel
// (thats the i / strip.numPixels() part)
// Then add in j which makes the colors go around per pixel
// the % 384 is to make the wheel cycle around
strip.setPixelColor(i, Wheel( ((i * 384 / strip.numPixels()) + j) % 384) );
}
strip.show(); // write all the pixels out
delay(wait);
}
}

// Fill the dots progressively along the strip.
void colorWipe(uint32_t c, uint8_t wait) {
int i;

for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}

// Chase one dot down the full strip.
void colorChase(uint32_t c, uint8_t wait) {
int i;

// Start by turning all pixels off:
for(i=0; i<strip.numPixels(); i++) strip.setPixelColor(i, 0);

// Then display one pixel at a time:
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c); // Set new pixel ‘on’
strip.show(); // Refresh LED states
strip.setPixelColor(i, 0); // Erase pixel, but don’t refresh!
delay(wait);
}

strip.show(); // Refresh to turn off last pixel
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
for (int j=0; j<10; j++) { //do 10 cycles of chasing
for (int q=0; q < 3; q++) {
for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, c); //turn every third pixel on
}
strip.show();

delay(wait);

for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
for (int j=0; j < 384; j++) { // cycle all 384 colors in the wheel
for (int q=0; q < 3; q++) {
for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, Wheel( (i+j) % 384)); //turn every third pixel on
}
strip.show();

delay(wait);

for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}
/* Helper functions */

//Input a value 0 to 384 to get a color value.
//The colours are a transition r - g -b - back to r

uint32_t Wheel(uint16_t WheelPos)
{
byte r, g, b;
switch(WheelPos / 128)
{
case 0:
r = 127 - WheelPos % 128; //Red down
g = WheelPos % 128; // Green up
b = 0; //blue off
break;
case 1:
g = 127 - WheelPos % 128; //green down
b = WheelPos % 128; //blue up
r = 0; //red off
break;
case 2:
b = 127 - WheelPos % 128; //blue down
r = WheelPos % 128; //red up
g = 0; //green off
break;
}
return(strip.Color(r,g,b));
}

b180907a075034533cc53b73d40c0c6f570ff632.png

How to post an image.

The pot wiper is attached to a digital pin.

because the potentiometer bounces,

A noisy pot can often be quieted by adding a 0.1uf cap from the wiper to ground.

Read the how to use this forum sticky to see how to post code.

Before you post the code, in code tags as instructed in the sticky, indent the code to make it more readable by using the autoformat feature (ctrl-t or Tools, Auto Format) of the IDE.