I need help running two seperate pwm driven LEDs?

Hi, thanks to whoever's reading this! :slight_smile:

I'm relatively new to arduino and I've recently been trying to make a little circuit with two potentiometers and two LEDs, where pot. A controls LED A and pot. B controls LED B.

Now I have tried to put together a sketch to accomplish this, but for some unknown reason, only one potentiometers' output is being read, and is being sent to control both LEDs. Any ideas to what I'm doing wrong? Thanks wonders :smiley:

int varA;
int varB;

void setup() {
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
}

void loop() {
varA = analogRead(1);
analogWrite(9, varA/4);
varB = analogRead(2);
analogWrite(10, varB/4);
}

(Potentiometer output voltages are being fed into analog inputs 1 and 2. The values are divided by 4
and sent to write a variable pwm signal on pins 9 and 10 individually. The outputs of pins 9 and 10 are
connected to separate LEDs with current limiting resistors to ground. The potentiometers are being fed 5v.)

You did connect the pots to A1 and A2, not to A0 and A1...

Add some print statements, to see what's going on.
Leo..

int varA;
int varB;

void setup() {
  Serial.begin(9600);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
}

void loop() {
  varA = analogRead(A1); // pot connected to A1
  Serial.print("varA: ");
  Serial.print(varA);
  analogWrite(9, varA/4);
  varB = analogRead(A2); // pot connected to A2
  Serial.print("  varB: ");
  Serial.println(varB);
  analogWrite(10, varB/4);
  delay(500); // some delay to see the values 
}

Yeah I just prefer not to use pin 0, thanks for the advice :slight_smile:

M301:
Yeah I just prefer not to use pin 0

Why?

To do an analog read, use the constants A0, A1, A2, A3 and so on rather than a bare 0,1,2,3. The values that you actually need to use to read the analog inputs may vary from board to board, which is why you are provided with predefined constants.

For giggles:

void setup() {
  Serial.begin(9600);
  Serial.print("analog input A0 is actually pin ");
  Serial.println(A0);
  Serial.print("analog input A1 is actually pin ");
  Serial.println(A1);
  Serial.print("analog input A2 is actually pin ");
  Serial.println(A2);
  Serial.print("analog input A3 is actually pin ");
  Serial.println(A3);
  Serial.print("analog input A4 is actually pin ");
  Serial.println(A4);
  
}