Dual LEDs

Dear Friends,

I'm using the example from http://arduino.cc/en/Tutorial/AnalogInOutSerial and able to dim LED connected on Analog 9. What i want to do is controlling two LEDs in contrast, while fading out A9 the A8 fading in and vice-versa using the same Pot connected on Analog 8.

Thanks,
Newbie LED

After some tries, here's the result that worked a bit :

const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin1 = 9; // Analog output pin that the BLUE LED is attached to
const int analogOutPin2 = 10; // Analog output pin that the WHITE LED is attached to

int sensorValue = 0; // value read from the pot
int outputValue1 = 0; // value output to the PWM (analog out)
int outputValue2 = 0; // value output to the PWM (analog out)

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}

void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue1 = map(sensorValue, 0, 1023, 0, 255);
outputValue2 = map(sensorValue, 0, 1023, 255, 0);
// change the analog out value:
analogWrite(analogOutPin1, outputValue1);
analogWrite(analogOutPin2, outputValue2);

// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t" "output 1 = ");
Serial.println(outputValue1);
Serial.print("\t" "output 2 = ");
Serial.println(outputValue2);

// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(2);
}

But, one point is still missing, i do not want to turn on any of LEDs in middle (let say both LEDs should be off between Analog Values 383 and 639), sorry do not get the point with If statement. Any helps ?

Pretty neat code there 8)
Try this:

const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin1 = 9; // Analog output pin that the BLUE LED is attached to
const int analogOutPin2 = 10; // Analog output pin that the WHITE LED is attached to

int sensorValue = 0;        // value read from the pot
int outputValue1 = 0;        // value output to the PWM (analog out)
int outputValue2 = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);
  // map it to the range of the analog out:
  outputValue1 = map(sensorValue, 0, 1023, 0, 255);
  outputValue2 = map(sensorValue, 0, 1023, 255, 0);
  if(sensorValue < 640 && sensorValue > 337)
  {
    analogWrite(analogOutPin1, 0);
    analogWrite(analogOutPin2, 0);
    outputValue1 = 0;
    outputValue2 = 0;
  }
  else
{
      // change the analog out value:
    analogWrite(analogOutPin1, outputValue1);
  analogWrite(analogOutPin2, outputValue2);
}
  // print the results to the serial monitor:
  Serial.print("sensor = " );
  Serial.print(sensorValue);
  Serial.print("\t" "output 1 = ");
  Serial.println(outputValue1);
  Serial.print("\t" "output 2 = ");
  Serial.println(outputValue2);


  // wait 2 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(2);
}

Ps. Its appreciated if you post code properly.

Dear Elac,

Thanks for your interest, i also tried this by own again and it worked anyhow :

const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin1 = 9; // Analog output pin that the BLUE LED is attached to
const int analogOutPin2 = 10; // Analog output pin that the WHITE LED is attached to

// int sensorValue = 0;        // value read from the pot
int outputValue1 = 0;        // value output to the PWM (analog out)
int outputValue2 = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop() {
  // read the analog in value:
  int sensorValue = analogRead(analogInPin);            
  // map it to the range of the analog out:
  
  if (sensorValue <= 383)
  {
      outputValue1 = map(sensorValue, 0, 382, 255, 0);
      outputValue2 = map(sensorValue, 0, 382, 0, 0);
  }
  else if (sensorValue >= 639)
  {
      outputValue1 = map(sensorValue, 640, 1023, 0, 0);
      outputValue2 = map(sensorValue, 640, 1023, 0, 255);
  }
 else
  {
  outputValue1 = map(sensorValue, 0, 1023, 0, 0);
  outputValue2 = map(sensorValue, 0, 1023, 0, 0);
  }
  // change the analog out value:
  analogWrite(analogOutPin1, outputValue1);          
  analogWrite(analogOutPin2, outputValue2);
  
  // print the results to the serial monitor:
  Serial.print("output 1 = ");
  Serial.println(outputValue1);
  Serial.print("\n" "output 2 = ");      
  Serial.println(outputValue2);
  

  // wait 2 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(2);                    
}

Dual_LED__Own_.ino (1.55 KB)