code help regarding reading multiple analog inputs

Hello everyone, I am trying to write a sketch that will read in two analog input signals (through pins A0 and A2) which will then be used to turn on two different sets of LEDs based on their respective input voltage levels. For A0, I have specified output pins 2-6 and for A2, 7-11. (I have used the example LED bar graph code to set up my code). My code compiles without any errors, however, when I went to test it with the Arduino, both sets of LEDs would light up the same even when there was only one input, regardless of whether it was connected to A0 or A2. Basically, both groups of LEDs are not being controlled independently by the two different inputs as I would like them to. I have a feeling there is something wrong with how I set up my for() loops in the main body of the code but can't quite put my finger on it. Any help would be greatly appreciated!

Thanks!

const int analogPin = A0;   // the pin that the input signal is attached to
const int ledCount = 5;  // the number of LEDs 

const int analogPin2 = A2;   
const int ledCount2 = 5; 

int ledPins[] = {2, 3, 4, 5, 6}; 
int ledPins2[] = {7, 8, 9, 10, 11};// an array of pin numbers to which LEDs are attached


void setup() {
  for (int thisLed = 0; thisLed < ledCount; thisLed++) // set digital pins to OUTPUT
  { pinMode(ledPins[thisLed], OUTPUT);}
  
  for (int thisLed2 = 0; thisLed2 < ledCount2; thisLed2++) 
  { pinMode(ledPins2[thisLed2], OUTPUT);}
}


void loop() {
  
  int sensorReading = analogRead(analogPin);
  int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
  
  int sensorReading2 = analogRead(analogPin2);
  int ledLevel2 = map(sensorReading2, 0, 1023, 0, ledCount2);

  
  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    
    if (thisLed < ledLevel) {
      digitalWrite(ledPins[thisLed], HIGH);
    } 
   
    else {
      digitalWrite(ledPins[thisLed], LOW); 
    }
  }
  
  
  for (int thisLed2 = 0; thisLed2 < ledCount2; thisLed2++) {
    
    if (thisLed2 < ledLevel2) {
      digitalWrite(ledPins2[thisLed2], HIGH);
    } 
    
    else {
      digitalWrite(ledPins2[thisLed2], LOW); 
    }
  }
  
}
const int analogPin = A0;

You only use A0 and A1 when you want to use the analogue pins as digital ones.
To use them as analogue ones you must use:-

const int analogPin = 0;

I haven't checked the rest but correct that first.

const int analogPin = A0;   // the pin that the input signal is attached to
const int ledCount = 5;  // the number of LEDs 

const int analogPin2 = A2;   
const int ledCount2 = 5;

Do you count things by going "yeah, I got a thing, 2, 3, 4, ...". Of course not, you go "1, 2, 3,...". So, if you are going to number variable names, number ALL of them.

There are no Serial.print() statements in your code. Time to add some.

My code compiles without any errors, however, when I went to test it with the Arduino, both sets of LEDs would light up the same even when there was only one input, regardless of whether it was connected to A0 or A2.

The "even when there is only one input" bothers me. You should not be trying to read an analog pin with nothing connected to it.

  for (int thisLed = 0; thisLed < ledCount; thisLed++) {

This statement is too hard to read. The index variable should be a one letter name.

for(int i=0; i<ledCount; i++)
{
}

You also haven't set the analog pins to INPUT. I don't know if the Arduino will default to INPUT, hopefully someone else can answer that.

You only use A0 and A1 when you want to use the analogue pins as digital ones.

Sorry Mike, that's not so.

You also haven't set the analog pins to INPUT.

analogRead implies that the pin is an input; there's no need to set pinMode

mdg48,
did you ever figure this out? i am running into a very similar issue without a good solution and wanted to see what you may have ended up doing.
thanks
j

Arduino has only one ADC, and one sampling and hold (S/H) capacitor is used for different channels. So , observed behavior is normal, to prevent inter-channel interference BOTH inputs have to be connected to low impedance (relatively to sampling rate) source. Ground other input to track software issues.