# ws2812 LED programming problem

**URL:** <https://forum.arduino.cc/t/ws2812-led-programming-problem/548719>\
**Category:** Programming\
**Created:** [September 30, 2018, 11:36pm UTC](https://forum.arduino.cc/t/ws2812-led-programming-problem/548719 "2018-09-30T23:36:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kalessa](https://avatars.discourse-cdn.com/v4/letter/k/e8c25b/32.png) [@Kalessa](https://forum.arduino.cc/u/Kalessa)\
**Post date:** [September 30, 2018, 11:36pm UTC](https://forum.arduino.cc/t/ws2812-led-programming-problem/548719/1 "2018-09-30T23:36:00Z")

</div>

I want to place LED lighting above my galley or to just simply use it as a nightlight, accent lighting, whatever. I have the color and brightness figured out using potentiometers but I cant figure out how to sequentially light them up with a third Pot and insert that into my loop. Working code is below.

ws2812 addressable LED waterproof lightstrip.  
3 10k SPST potentiometers for color, brightness and number of LED's.  
The SPST is to turn the circuit on, all in one knob, perfect. The brightness pot will be the on/off but you have to buy these pots in packages... The color pot will be for white as there is no white in rainbow spectrum.

So heres what I have planned;

60 LED's spanning 7'.  
Arduino reads on analog pins a voltage ranging from 0-1023. so resistance/6=170.5  
so r\<=170 = 1 led (c), =\>340 = 2 led's (c) and etc.

X=black  
C=pot color

Aft Fwd  
Stove Sink Refrigerator  
1 60  
data flow=\>  
\<=170 xxxxxcxxxxxcxxxxxcxxxxxcxxxxxcxxxxxcxxxxxcxxxxxcxxxxxcxxxxxc  
=\>340 xxxxccxxxxccxxxxccxxxxccxxxxccxxxxxcxxxxccxxxxccxxxxccxxxxcc  
=\>510 xxxcccxxxcccxxxcccxxxcccxxxcccxxxcccxxxcccxxxcccxxxcccxxxccc  
=\>680 xxccccxxccccxxccccxxccccxxccccxxccccxxccccxxccccxxccccxxcccc  
=\>850 xcccccxcccccxcccccxcccccxcccccxcccccxcccccxcccccxcccccxccccc  
=\>980 cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

What am I doing wrong?  
I still have to add the POT line, but the code as it is doesn't compile.

//

#include \<Adafruit\_NeoPixel.h\>  
// can use #include \<FastLed.h\>

const int STRIP\_LENGTH = 60;

const int LED\_STRIP\_PIN = 10;

const int BRIGHTNESS\_POT = A6;  
const int BRIGHTNESS\_SWITCH = 8;

const int COLOR\_POT = A3;  
const int BRIGHTNESS\_SWITCH = 6;

float brightness = 0.0;

Adafruit\_NeoPixel strip = Adafruit\_NeoPixel(STRIP\_LENGTH, LED\_STRIP\_PIN,  
NEO\_GRB + NEO\_KHZ800);

void setup() {

pinMode(COLOR\_POT, INPUT);  
pinMode(COLOR\_SWITCH, INPUT\_PULLUP);  
pinMode(BRIGHTNESS\_POT, INPUT);  
pinMode(BRIGHTNESS\_SWITCH, INPUT\_PULLUP);  
strip.begin();  
strip.show();  
}

void loop() {  
if (digitalRead(BRIGHTNESS\_SWITCH) == HIGH) {  
// Get the percentage of the full brightness.  
brightness = 1.0 - (analogRead(BRIGHTNESS\_POT) / 1023.0);  
Serial.println(brightness);  
if (digitalRead(COLOR\_SWITCH) == HIGH) {  
// Scale down the color range.  
int color = (analogRead(COLOR\_POT) / 1023.0) \* 245.0;  
calculateAndChangeColor(color);  
} else {  
// Color is disabled. Change strip to white.  
changeColor(255, 255, 255);  
}  
} else {  
changeColor(0,0,0);  
}

delay(50);

}

void changeColor(int red, int green, int blue) {  
int i;  
for(i=0; i\< strip.numPixels(); i++) {  
strip.setPixelColor(i, red_brightness, green_brightness, blue\*brightness);  
}  
strip.show();  
}

// This is based on the Adafruit NeoPixel strandtest example.  
void calculateAndChangeColor(int potValue) {  
int red = 0;  
int green = 0;  
int blue = 0;  
if(potValue \< 85) {  
red = potValue \* 3;  
green = 255 - potValue \* 3;  
} else if(potValue \< 170) {  
potValue -= 85;  
red = 255 - potValue \* 3;  
blue = potValue \* 3;  
} else {  
potValue -= 170;  
green = potValue \* 3;  
blue = 255 - potValue \* 3;  
}  
changeColor(red, green, blue);

}

---

<div class="post-metadata">

**Author:** ![DrAzzy](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@DrAzzy](https://forum.arduino.cc/u/DrAzzy)\
**Post date:** [September 30, 2018, 11:44pm UTC](https://forum.arduino.cc/t/ws2812-led-programming-problem/548719/2 "2018-09-30T23:44:00Z")

</div>

If you have code that doesnt compile, post the errors uou get - most of us answer these when were not at our computers with the IDE (if we were, wed be working on our own projects!), and nobody wants to bother comppiling others code while helping other people for free.

Also, use code tags ehen posting code or error output.

---

<div class="post-metadata">

**Author:** ![Kalessa](https://avatars.discourse-cdn.com/v4/letter/k/e8c25b/32.png) [@Kalessa](https://forum.arduino.cc/u/Kalessa)\
**Post date:** [September 30, 2018, 11:50pm UTC](https://forum.arduino.cc/t/ws2812-led-programming-problem/548719/3 "2018-09-30T23:50:21Z")

</div>

line 36, calculateAndChangeColor(color); was not declared in this scope.. i'm missing something stupid.

---

<div class="post-metadata">

**Author:** ![Kalessa](https://avatars.discourse-cdn.com/v4/letter/k/e8c25b/32.png) [@Kalessa](https://forum.arduino.cc/u/Kalessa)\
**Post date:** [September 30, 2018, 11:58pm UTC](https://forum.arduino.cc/t/ws2812-led-programming-problem/548719/4 "2018-09-30T23:58:25Z")

</div>

copy paste hell..

the above code works. This is what doesn't compile,

#include \<Adafruit\_NeoPixel.h\>

const int STRIP\_LENGTH = 1;

const int LED\_STRIP\_PIN = 10;

const int BRIGHTNESS\_POT = A6;  
const int BRIGHTNESS\_SWITCH = 8;

const int COLOR\_POT = A3;  
const int COLOR\_SWITCH = 6;

float brightness = 0.0;

Adafruit\_NeoPixel strip = Adafruit\_NeoPixel(STRIP\_LENGTH, LED\_STRIP\_PIN,  
NEO\_GRB + NEO\_KHZ800);

void setup() {

pinMode(COLOR\_POT, INPUT);  
pinMode(COLOR\_SWITCH, INPUT\_PULLUP);  
pinMode(BRIGHTNESS\_POT, INPUT);  
pinMode(BRIGHTNESS\_SWITCH, INPUT\_PULLUP);  
strip.begin();  
strip.show();  
}

void loop() {  
if (digitalRead(BRIGHTNESS\_SWITCH) == HIGH) {  
// Get the percentage of the full brightness.  
brightness = 1.0 - (analogRead(BRIGHTNESS\_POT) / 1023.0);  
Serial.println(brightness);  
if (digitalRead(COLOR\_SWITCH) == HIGH) {  
// Scale down the color range.  
int color = (analogRead(COLOR\_POT) / 1023.0) \* 245.0;  
calculateAndChangeColor(color);  
} else {  
// Color is disabled. Change strip to white.  
changeColor(255, 255, 255);  
}  
} else {  
changeColor(0,0,0);  
}

delay(50);

void changeColor(int red, int green, int blue) {  
int i;  
for (i = 0, i \< 60, i++) {  
strip.setPixelColor(i, 0, 0, 0);  
if ((i % 6) == 5) { // finds 5, 11, 17 etc  
if (brightness \> 0) strip.setPixelColor(i, red_brightness, green_brightness, blue_brightness); // pixel 5, 11, 17...  
if (brightness \> 170) strip.setPixelColor(i-1,red_brightness, green_brightness, blue_brightness); // pixel 4, 10, 16...  
if (brightness \> 340) strip.setPixelColor(i-2,red_brightness, green_brightness, blue_brightness); // pixel 3, 9, 15...  
if (brightness \> 510) strip.setPixelColor(i-3,red_brightness, green_brightness, blue_brightness); // pixel 2, 8, 14...  
if (brightness \> 680) strip.setPixelColor(i-4,red_brightness, green_brightness, blue_brightness); // pixel 1, 7, 13...  
if (brightness \> 850) strip.setPixelColor(i-5,red_brightness, green_brightness, blue_brightness); // pixel 0, 6, 12...  
}  
}  
}

// This is based on the Adafruit NeoPixel strandtest example.  
void calculateAndChangeColor(int potValue) {  
int red = 0;  
int green = 0;  
int blue = 0;  
if(potValue \< 85) {  
red = potValue \* 3;  
green = 255 - potValue \* 3;  
} else if(potValue \< 170) {  
potValue -= 85;  
red = 255 - potValue \* 3;  
blue = potValue \* 3;  
} else {  
potValue -= 170;  
green = potValue \* 3;  
blue = 255 - potValue \* 3;  
}  
changeColor(red, green, blue);

}

---

<div class="post-metadata">

**Author:** ![Kalessa](https://avatars.discourse-cdn.com/v4/letter/k/e8c25b/32.png) [@Kalessa](https://forum.arduino.cc/u/Kalessa)\
**Post date:** [October 1, 2018, 1:04am UTC](https://forum.arduino.cc/t/ws2812-led-programming-problem/548719/5 "2018-10-01T01:04:29Z")

</div>

Bracket issue.. sort of resolved.. I'm thinking i've been looking at this to long to notice my mistakes. So now I have another bracket issue and I think I have to change the part where it looks for brightness at line 80..

So line 77, expected ';' before ')'

current code,

#include \<Adafruit\_NeoPixel.h\>

const int STRIP\_LENGTH = 1;  
const int LED\_STRIP\_PIN = 10;  
const int BRIGHTNESS\_POT = A6;  
const int BRIGHTNESS\_SWITCH = 8;  
const int COLOR\_POT = A3;  
const int COLOR\_SWITCH = 6;

float brightness = 0.0;  
Adafruit\_NeoPixel strip = Adafruit\_NeoPixel(STRIP\_LENGTH, LED\_STRIP\_PIN,NEO\_GRB + NEO\_KHZ800);

void setup() {  
pinMode(COLOR\_POT, INPUT);  
pinMode(COLOR\_SWITCH, INPUT\_PULLUP);  
pinMode(BRIGHTNESS\_POT, INPUT);  
pinMode(BRIGHTNESS\_SWITCH, INPUT\_PULLUP);  
strip.begin();  
strip.show();  
}

void loop() {

if (digitalRead(BRIGHTNESS\_SWITCH) == HIGH) {  
// Get the percentage of the full brightness.  
brightness = 1.0 - (analogRead(BRIGHTNESS\_POT) / 1023.0);  
Serial.println(brightness);  
if (digitalRead(COLOR\_SWITCH) == HIGH) {  
// Scale down the color range.  
int color = (analogRead(COLOR\_POT) / 1023.0) \* 245.0;  
calculateAndChangeColor(color);  
} else {  
// Color is disabled. Change strip to white.  
changeColor(255, 255, 255);  
}  
} else {  
changeColor(0,0,0);  
}

delay(50);  
}

// void changeColor(int red, int green, int blue) {  
// int i;  
// for(i=0; i\< strip.numPixels(); i++) {  
// strip.setPixelColor(i, red_brightness, green_brightness, blue\*brightness);  
// }  
// strip.show();  
// }

// This is based on the Adafruit NeoPixel strandtest example.  
void calculateAndChangeColor(int potValue) {  
int red = 0;  
int green = 0;  
int blue = 0;  
if(potValue \< 85) {  
red = potValue \* 3;  
green = 255 - potValue \* 3;  
} else if(potValue \< 170) {  
potValue -= 85;  
red = 255 - potValue \* 3;  
blue = potValue \* 3;  
} else {  
potValue -= 170;  
green = potValue \* 3;  
blue = 255 - potValue \* 3;  
}  
changeColor(red, green, blue);  
}

void changeColor(int red, int green, int blue) {  
int i;  
for (i = 0, i \< 60, i++) {  
strip.setPixelColor(i, 0, 0, 0);  
if ((i % 6) == 5) { // finds 5, 11, 17 etc  
if (brightness \> 0) strip.setPixelColor(i, red_brightness, green_brightness, blue_brightness); // pixel 5, 11, 17...  
if (brightness \> 170) strip.setPixelColor(i-1,red_brightness, green_brightness, blue_brightness); // pixel 4, 10, 16...  
if (brightness \> 340) strip.setPixelColor(i-2,red_brightness, green_brightness, blue_brightness); // pixel 3, 9, 15...  
if (brightness \> 510) strip.setPixelColor(i-3,red_brightness, green_brightness, blue_brightness); // pixel 2, 8, 14...  
if (brightness \> 680) strip.setPixelColor(i-4,red_brightness, green_brightness, blue_brightness); // pixel 1, 7, 13...  
if (brightness \> 850) strip.setPixelColor(i-5,red_brightness, green_brightness, blue_brightness); // pixel 0, 6, 12...  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 11:23pm UTC](https://forum.arduino.cc/t/ws2812-led-programming-problem/548719/6 "2021-05-05T23:23:43Z")

</div>

Closed on May 5, 2021, 11:23pm UTC


