Two potentiometer inputs do the same thing when they're not supposed to

Hi, everybody. Doing some playing around getting my project to work properly: Osepp Uno R3 plus, trying to use two pots to run two Adafruit neopixel rings (12 pixels each). I've been trying to get them set up so each pot controls each ring. I managed to get as far as each one sweeping their preferred color ranges, but each pot input pin seems to control both rings at the same time. I even assigned each pot their own pin in the program.

Here's what I used. It's actually modified from another one I found on here:

#include <Adafruit_NeoPixel.h>
#define PIN1 2 //You might need to change this depending on what pins your board has. These means you should connect the ring's 'data in' to digital pin 2 on the arduino.
#define PIN2 4

//Initialize the ring. In the line below I used "12" since you mentioned a ring of 12 LEDs. If not then change it to however many LEDs you have.
Adafruit_NeoPixel ring1 = Adafruit_NeoPixel(12, PIN1, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel ring2 = Adafruit_NeoPixel(12, PIN2, NEO_GRB + NEO_KHZ800);

int potpin = A0; //This is the value you will change based on your potentiometer values once you get that working too.
int potpin2 = A1;

void setup() {
ring1.begin();
ring1.show(); // Initialize all pixels to 'off'
potpin = 0;
pinMode(A0, INPUT);

ring2.begin();
ring2.show(); // Initialize all pixels to 'off'
potpin2 = 1;
pinMode(A1, INPUT);
}

void loop() {

int reading = analogRead(A0); //Adafruit trinket oddity, usually the same as pinMode
potpin = (int)(reading * 12 / 1024); //potentiometer specific values, adjust until entire ring is filled

//Turn on correct number of LEDs based on numOn
for (int i = 0; i < 12; i++) {
if (i < potpin+1) { //the +1 keeps one LED on at all times
ring1.setPixelColor(i, 100, 0, 0); // turn on this LED, also adjusts colors ); //turn on this LED, adjust values to affect colors and brightness of LEDs

}
else {
ring1.setPixelColor(i, 0, 0, 100); //turn off this LED
}

ring1.show(); //Apply changes to Ring

int reading = analogRead(A1); //Adafruit trinket oddity, usually the same as pinMode
potpin2 = (int)(reading * 12 / 1024); //potentiometer specific values, adjust until entire ring is filled

//Turn on correct number of LEDs based on numOn
for (int i = 0; i < 12; i++) {
if (i < potpin2+1) { //the +1 keeps one LED on at all times
ring2.setPixelColor(i, 0, 100, 0); // turn on this LED, also adjusts colors ); //turn on this LED, adjust values to affect colors and brightness of LEDs

}
else {
ring2.setPixelColor(i, 100, 100, 100); //turn off this LED
}

ring2.show(); //Apply changes to Ring

}
}
}

Any idea what I'm doing wrong? I'm sure my eyes are just strained and I just can't see something obvious...

Try adding some Serial.println() statements to appropriate locations in your code and then run it with the Serial Monitor open so you can see what is happening.

Tried that just now. Serial monitor shows a blank field, so maybe I don't have them in the right places. Not sure where to look next. Tried isolating each chunk of input lines with curly brackets, thinking that would maybe isolate them from one another, but nothing.

EDIT I think it's important at this stage to mention that I only currently have one pot hooked up to the breadboard for testing. All I do is swap that pot's signal wire between the two analog inputs I'm using. I did some further searching, and I wondered: Could the unused analog input be floating to where the currently used input is? Do I need both pots hooked up for this to work properly?

Please post your full updated sketch.

If possible, you should always post code directly in the forum thread as text using code tags:

  • Do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) on your code. This will make it easier for you to spot bugs and make it easier for us to read.
  • In the Arduino IDE or Arduino Web Editor, click on the window that contains your sketch code.
  • Press "Ctrl + A". This will select all the text.
  • Press "Ctrl + C". This will copy the selected text to the clipboard.
  • In a forum reply here, click on the reply field.
  • Click the </> button on the forum toolbar. This will add the forum's code tags markup to your reply.
  • Press "Ctrl + V". This will paste the sketch between the code tags.
  • Move the cursor outside of the code tags before you add any additional text to your reply.
  • Repeat the above process if your sketch has multiple tabs.

This will make it easy for anyone to look at it, which will increase the likelihood of you getting help.

Sorry about that. Here it is. Made some minor color and pin changes, don't worry about it...

#include <Adafruit_NeoPixel.h>
#define PIN1 4 
#define PIN2 7


Adafruit_NeoPixel ring = Adafruit_NeoPixel(12, PIN1, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel ring2 = Adafruit_NeoPixel(12, PIN2, NEO_GRB + NEO_KHZ800);


int potpin1 = A0;
int potpin2 = A2;

void setup() {
  ring.begin();
  ring.show(); 
  potpin1 = 0;
  pinMode(A0, INPUT);

  ring2.begin();
  ring2.show(); 
  potpin2 = 2;
  pinMode(A2, INPUT);
}

void loop() {

  int reading = analogRead(A0); 
  potpin1 = (int)(reading * 12 / 1022);

  
  for (int i = 0; i < 12; i++) {
    if (i < potpin1 + 1) {
      ring.setPixelColor(i, 100, 0, 0);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ); 

    }
    else {
      ring.setPixelColor(i, 20, 0, 100); 
    }

    ring.show();

    int reading = analogRead(A2); 
    potpin2 = (int)(reading * 12 / 1022); 

    
    for (int i = 0; i < 12; i++) {
      if (i < potpin2 + 1) { 
        ring2.setPixelColor(i, 20, 0, 50);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ); 


      }
      else {
        ring2.setPixelColor(i, 0, 20, 20); 
      }

      ring2.show(); 

    }
  }
}

I think it's important at this stage to mention that I only currently have one pot hooked up to the breadboard for testing. All I do is swap that pot's signal wire between the two analog inputs I'm using. I did some further searching, and I wondered: Could the unused analog input be floating to where the currently used input is? Do I need both pots hooked up for this to work properly?

Yes quite right, your unconnected analogue input is floating and is giving you the same reading on all analogue inputs. You need to connect up both.

potpin1 = (int)(reading * 12 / 1022);

No need for the int cast.

Barrel_Ball:
Sorry about that. Here it is. Made some minor color and pin changes, don't worry about it...

OK, now go back and modify your original post - using the "More -> Modify" option below the right hand corner of your post - to tidy it up.