this is my try, when i use it shows nothing just white screen no data
int ldr1=A0;
int ldr2=A1;
int extend=12;
int retract=13;
int ldr1Value=0;
int ldr2Value=0;
void setup() {
// put your setup code here, to run once:
pinMode(extend, OUTPUT);
pinMode(retract, OUTPUT);
pinMode(ldr1, INPUT);
pinMode(ldr2, INPUT);
Serial. begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if (analogRead(ldr1)==HIGH) {
ldr1Value = analogRead(ldr1);
Serial.print("LDR1 Value is: ")
Serial.println(ldr1Value);
digitalWrite(extend, HIGH);
digitalWrite(retract, LOW); }
else if (analogRead(ldr2)==LOW){
ldr2Value = analogRead(ldr2);
Serial.print("LDR2 Value is: ");
Serial.println(ldr2Value);
digitalWrite(extend, LOW);
digitalWrite(retract, HIGH); }
delay(1000);
}
That's true, but who's to say that the threshold between high and low on an analog input with a range of 0-1023 is right down at one end between 0 and 1?
Sure, you need to differentiate between some value light and darkness, but as @Idahowalker suggested that is likely to be somewhere in the middle not right at one end of the range.
i used the code in your link and didnt work
i used this code the serial moniter give 51 at dark and 1023 at maximum light
int LDR = A1;
int input_val = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
input_val = analogRead(LDR);
Serial.print("LDR Value is: ");
Serial.println(input_val);
delay(1000);
}
int ldr1=A0;
int ldr2=A1;
int extend=12;
int retract=13;
int ldr1Value=0;
int ldr2Value=0;
void setup() {
// put your setup code here, to run once:
pinMode(extend, OUTPUT);
pinMode(retract, OUTPUT);
pinMode(ldr1, INPUT);
pinMode(ldr2, INPUT);
Serial. begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if (analogRead(ldr1)>= 1023) {
ldr1Value = analogRead(ldr1);
Serial.print("LDR1 Value is: ");
Serial.println(ldr1Value);
digitalWrite(extend, HIGH);
digitalWrite(retract, LOW); }
else if (analogRead(ldr2)< 50){
ldr2Value = analogRead(ldr2);
Serial.print("LDR2 Value is: ");
Serial.println(ldr2Value);
digitalWrite(extend, LOW);
digitalWrite(retract, HIGH); }
delay(1000);
}
this code doesnt work (theres no values in the serial moniter, just a white screen)
YES! but i only see 1023, this is what the serial moniter shows
LDR1 Value is: 1023
1023
LDR1 Value is: 1023
1023
LDR1 Value is: 1023
1023
LDR1 Value is: 1023
1023
int ldr1=A0;
int ldr2=A1;
int extend=12;
int retract=13;
int ldr1Value=0;
int ldr2Value=0;
void setup() {
// put your setup code here, to run once:
pinMode(extend, OUTPUT);
pinMode(retract, OUTPUT);
pinMode(ldr1, INPUT);
pinMode(ldr2, INPUT);
Serial. begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if (analogRead(ldr1)>= 1023) {
ldr1Value = analogRead(ldr1);
Serial.print("LDR1 Value is: ");
Serial.println(ldr1Value);
Serial.println( analogRead(ldr1) );
digitalWrite(extend, HIGH);
digitalWrite(retract, LOW); }
else if (analogRead(ldr2)< 50){
ldr2Value = analogRead(ldr2);
Serial.print("LDR2 Value is: ");
Serial.println(ldr2Value);
digitalWrite(extend, LOW);
digitalWrite(retract, HIGH); }
delay(1000);
}
Why would you use analogRead if you're only interested in HIGH and LOW.
On most Arduinos, analogue input is an alternate function of a normal pin so you can use digitalRead. Exception that I'm aware of is A6 and A7 on the classic Nano.