Help with coding, Single POT controlling different patterns and colors

Hi all,

I’ve hit a wall writing my code and I’ve tried some trouble shooting techniques but its been while since i have done some coding.

Basically I’m trying to vary what color output i get depending on the position of an analog signal (using a potentiometer)

Currently I’m just testing to make and only checking for 3 colors (GRB), but i will be adding 2 more colors and adding specific patterns to each color. That part is not in the code yet :confused:

My issue right now is, my last else statement seems to over ride anything written previous. So blue being my last color range check always comes on regardless of pot position. (green will come on then blue and back again). i tried using a prev color logic but that didn’t seem to help. Where it would check to if a color is set then don’t set another color unless there is change.

I appreciate everyones advice, thanks in advance.

Here is my code:

#include <Adafruit_NeoPixel.h>

#define Total_LEDS 31
#define POTpin A0            //define pin names
#define PWMpin 3

Adafruit_NeoPixel strip = Adafruit_NeoPixel(Total_LEDS, 6, NEO_GRB + NEO_KHZ800);

int RedVal = 0;       // red LED value
int GreenVal = 0;    // green LED value
int BlueVal = 0;       // blue LED value
int PreviousColour = 0;


int POTVal = 0;
int x;
int MicroOUT = 0;



void setup() {
 strip.begin();
 strip.show();
 pinMode(POTpin, INPUT);
 pinMode(PWMpin, OUTPUT);
 Serial.begin(9600);
 Serial.println("My Sketch has started");
}



void loop() {
 while(1)
 {
 delay(1000);         //PAUSE 1 SECOND
 
 POTVal = analogRead(POTpin);
 Serial.println("POTVal = ");
 Serial.println(POTVal);

 MicroOUT = map(POTVal, 0, 1023, 0, 255);
 Serial.println("MicroOUT = ");
 Serial.println(MicroOUT);


//This is important
 if  (MicroOUT >= 0 & MicroOUT <= 100){   // range read from POT to show (GREEN) 
   if (PreviousColour != 1){
       GreenVal = 200;                                // red LED value
       RedVal = 0;                            // green LED value
       BlueVal = 0;                              // blue LED value
         Serial.println("RedVal = ");
         Serial.println(RedVal);
         Serial.println("GreenVal = ");
         Serial.println(GreenVal);
         Serial.println("BlueVal = ");
         Serial.println(BlueVal);

       
       for (x = 0; x < Total_LEDS; x++)  {
         Serial.println("Green for Loop Reached = ");
         strip.setPixelColor(x, RedVal, GreenVal, BlueVal);   
         strip.show(); 
       }
   }
       PreviousColour = 1;
       delay(2000); 
 }
     
     else if (MicroOUT >= 101 & MicroOUT <= 150){      // range read from POT to show (AMBER)
       if (PreviousColour != 2){
       GreenVal = 0;                                // red LED value
       RedVal = 200;                            // green LED value
       BlueVal = 0;                              // blue LED value         
         Serial.println("RedVal = ");
         Serial.println(RedVal);
         Serial.println("GreenVal = ");
         Serial.println(GreenVal);
         Serial.println("BlueVal = ");
         Serial.println(BlueVal);

       for (x = 0; x < Total_LEDS; x++)  {
         Serial.println("Red for Loop Reached = ");
         strip.setPixelColor(x, RedVal, GreenVal, BlueVal);   
         strip.show(); 
       }
       }
       PreviousColour = 2;
       delay(2000); 
     }

     else (MicroOUT >= 151 & MicroOUT <= 200);{    // range read from POT to show (BLUE)
       if (PreviousColour != 3){
       GreenVal = 0;                                // red LED value
       RedVal = 0;                            // green LED value
       BlueVal = 200;                              // blue LED value
         Serial.println("RedVal = ");
         Serial.println(RedVal);
         Serial.println("GreenVal = ");
         Serial.println(GreenVal);
         Serial.println("BlueVal = ");
         Serial.println(BlueVal);

       for (x = 0; x < Total_LEDS; x++)  {
         Serial.println("Blue for Loop Reached = ");
         strip.setPixelColor(x,  GreenVal, RedVal, BlueVal);   
         strip.show(); 
       }
       }
       PreviousColour = 3;
       delay(2000); 
     }
    

 }
 }

I forgot to mention hardware I’m using.

3-wire LED strip from Neopixel
Arduino Uno board
5VDC PS
10kOhm Pot

I haven’t completely studied your code, but a couple of things look strange…

  if (PreviousColour != 1){

That logic “feels” screwy to me… If the value is not equal to one it could be zero or anything greater than one and the if-condition will be true.

if  (MicroOUT >= 0 & MicroOUT <= 100){   // range read from POT to show (GREEN) 
    if (PreviousColour != 1){

Both of those conditions have to be true, or nothing will happen. Is that what you want?

Not important, but since you are directly writing the color values, it doesn’t seem necessary to map the pot values.

P.S.
Click the </> icon to create a code box for your code.

DVDdoug:
I haven’t completely studied your code, but a couple of things look strange…

  if (PreviousColour != 1){

That logic “feels” screwy to me… If the value is not equal to one it could be zero or anything greater than one and the if-condition will be true.

So this part is something new i was trying to prevent the over writing of the blue color which seems to be always occurring. By setting PreviousColour to 1 at the end of the “Green” loop then I was thinking it will not meet the if requirement for PreviousColour of the next color loop. If that makes sense.

DVDdoug:

if  (MicroOUT >= 0 & MicroOUT <= 100){   // range read from POT to show (GREEN) 

if (PreviousColour != 1){



Both of those conditions have to be true, or nothing will happen. Is that what you want?


Not important, but since you are directly writing the color values, it doesn't seem necessary to map the pot values.

Yeah, that is what I was trying to achieve. So depending on the POTs’ position, set the color accordingly and only change the color if the POT reaches a different range which the color should change. And the code is partly doing this, when i adjust the POT the colors change according to my ranges but the color its set to gets alternated with BLUE as well.

I’ll try to upload a video , might be easier.

Here is the video:

Update:

So i actually got it working now, i changed the else if statements all to if statments and removed the ; from the last loop. I believe the semicolon was causing the code to skip over the last if requirements and just simply running the last part of the code.