Using multiple LDRs to Control 1 LED

Hi, im currently working on a user interface system where I want to be able to use a laser beam and 2 LDRs to control the brightness of a single LED

Shining the laser at the first LDR will make the LED light up dimly and shinning it at the second LDR will make it light up fully.

This is just a small part of a complex project as eventually I want to be able to do this with lots of LDRs controlling different parameters.

Here is my basic code so far, but its not working correctly, the second LDR (LDR1) does does not respond, thanks in advance!

const int LDR0 = A0;
const int LDR1 = A1;
const int LDR2 = A2;
const int LED = 9;
const int threshold = 900;

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

void loop() {
int LDR0 = analogRead(LDR0);
delay (50);
int LDR1 = analogRead(LDR1);
delay(50);

if (LDR0 < threshold) {
analogWrite(LED, 150);
}
else {
digitalWrite(LED,LOW);
}
if (LDR1 < threshold) {
analogWrite(LED, 5);
}
else {
digitalWrite(LED,LOW);
}

Serial.println(LDR0);
}

There won't be much difference to be seen between analogWrite(LED,5) and digitalWrite(LED, LOW)

Did you notice that whatever output is produced by the test of LDR0 is overwritten by the test of LDR1?

Better use tests like this:
if (LDR0...) ... //fully on
else if (LDR1...) ... //dimmed
else ... //off

Yes that's exactly what seems to be happening, il try that, thanks

DrDiettrich:
Did you notice that whatever output is produced by the test of LDR0 is overwritten by the test of LDR1?

Better use tests like this:
if (LDR0...) ... //fully on
else if (LDR1...) ... //dimmed
else ... //off

Thanks for the help, I have changed the code but LDR1 still does not respond. The code is now:

const int LDR0 = A0;
const int LDR1 = A1;
const int LDR2 = A2;
const int LED = 9;
const int threshold = 960;

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

void loop() {
int LDR0 = analogRead(LDR0);
delay (50);
int LDR1 = analogRead(LDR1);
delay(50);

if (LDR0 > threshold) {
analogWrite(LED, 150);
}
else if (LDR1 > threshold) {
analogWrite(LED, 10);
}
else {
digitalWrite(LED,LOW);

Serial.println(LDR0);
}}

I assume that your conditions still are wrong. Do the values of LDR0 and LDR1 increase or decrease with illumination?

You output LDR0 already, are the values above or below threshold? Ah, another error: Serial.println(LDR0) is called only if LDR0 and LDR1 are above threshold. Remove one brace after prinln and insert it before, to terminate the "else" block. Add another delay(500) after println, for less frequent output.

And use code tags for showing scrollable source listings!

Hi thanks for the replay, I want to so LDR0 = dimmest LDR1 = dim, LDR2 = bright... and so on

Ive made improvements but LDR1 still doesn't function, the values are below the thresholds so when I shine a laser on to the LDR the LED illuminates

const int LDR0 = A0;
const int LDR1 = A1;
const int LDR2 = A2;
const int LED = 9;
const int threshold = 960;

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

void loop() {
  int LDR0 = analogRead(LDR0);
  delay (50);
  int LDR1 = analogRead(LDR1);
  delay(50);
  
  if (LDR0 > threshold) {
 analogWrite(LED, 10);
  }
  else if (LDR1 > threshold) { 
    analogWrite(LED, 150);
  }
  else {
    digitalWrite(LED,LOW);
  }  
  Serial.println(LDR0);
  delay(500);
}

void loop() {
int LDR0 = analogRead(LDR0);
delay (50);
int LDR1 = analogRead(LDR1);
delay(50);

if (LDR0 > threshold) {
analogWrite(LED, 10);
}
else if (LDR1 > threshold) {
analogWrite(LED, 150);
}
else {
digitalWrite(LED,LOW);
}
Serial.println(LDR0);
delay(500);

hi thanks for the replay, I removed the 2 "int"s but it gave me these error messages:

Arduino: 1.6.1 (Windows 8.1), Board: "Arduino Uno"

lldrdimmer.ino: In function 'void loop()':

lldrdimmer.ino:13:9: error: assignment of read-only variable 'LDR0'

lldrdimmer.ino:15:9: error: assignment of read-only variable 'LDR1'

Error compiling.

You're confusing the pin LDR0 with the voltage on the pin. You set LDR0 as a constant, which makes it read-only, then try and write the read value to the pin number.

You need some more variables, like LDR0_val say.

"You need some more variables, like LDR0_val say."
Yes.
And I did NOT catch that, just saw that he was (attempting to) declare an int variable, LDR0 as being the analogRead of something. [Not a good idea.]

int LDR0 = analogRead(LDR0);

I wouldn't try to use the same name for both the variable to hold the pin number and the variable to hold the value. I would give them different names. Else they shadow one another and you'll end up with some weird results.

Yess, thanks everyone that has solved my problem, Im very new to arduino so I appreciate all the help !

For anyone interested the code nor reads:

const int LDR0 = A0;
const int LDR1 = A1;
const int LDR2 = A2;
const int LED = 9;
const int threshold = 960;

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

void loop() {
int LDR0_val = analogRead(LDR0);
 delay (50);
int LDR1_val = analogRead(LDR1);
 delay(50);
 
 if (LDR0_val > threshold) {
analogWrite(LED, 10);
 }
 else if (LDR1_val > threshold) { 
   analogWrite(LED, 150);
 }
 else {
   digitalWrite(LED,LOW);
 }  
 Serial.println(LDR0);
 delay(10);
}

Well, almost, but...

const int LDR0 = A0;
const int LDR1 = A1;
const int LDR2 = A2;
const int LED = 9;
const int threshold = 960;


int LDR0_val;  // change
int LDR1_val;  // change

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

void loop() {
LDR0_val = analogRead(LDR0);  // change
 delay (50);
LDR1_val = analogRead(LDR1);  // change
 delay(50);
 
 if (LDR0_val > threshold) {
analogWrite(LED, 10);
 }
 else if (LDR1_val > threshold) { 
   analogWrite(LED, 150);
 }
 else {
   digitalWrite(LED,LOW);
 }  
 Serial.println(LDR0);
 delay(10);
}