How to use serial moniter?

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

What is a HIGH and a LOW with analog read? Would it not make more sense to use a value like

if (analogRead(ldr1) >= 600) {
else if (analogRead(ldr2)  < 600 ){

or some other value of your choosing?

Basic analogRead in Arduino Program.

no bcs i am intersted in only high and low (myproject is light tracker to power the motor just on and off)

Good luck.

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

Cool!

Did you try

if (analogRead(ldr1) >= 1022) {
else if (analogRead(ldr2)  < 50 ){

i specifically choose a resistor for that i think the proplem is somewhere else

YEP and it feels good too

Without using serial prints values will not be sent to the serial monitor.

OP, seems to have deleted the post.

Why use else if (analogRead(ldr2) < 50) when the low value was found to be 51?

sorry i posted an old code, this is it

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)

thats why i was using LOW and HIGH
i choose my resistors to give higher than 3v (HIGH) and Low give 1.5v (LOW)

If you add a serial print are values printed?

How'd that work out for you?

i have a multimeter, at max brightness it gives A0 pin about 5.2 v and about 0.2v for shade

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

Not surprising, since you're printing inside the "if" that checks it's that high already...

Where should i put serial print? (i want it to show the values at all times)

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.

Just in loop(), not inside anything with a condition:

void loop() {

 Serial.print("LDR1 Value is: ");
 Serial.println( analogRead(ldr1) );
...
...
}//loop

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.