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);
}
}
}