Hi, as part f my project i have to write program to find light using arduino. I have to use two LDR as sensors. Each LDRs showing different values. Can any one help me to code this.
Can any one help me to code this.
Sure what have you got already? Post that and we will start from there.
I would start by writing a sketch that gets raw analogRead() values for both LDR's and prints them out once per second.
Put a baffle between the LDR's and point them toward a light. As you turn them to the left and right you should see one number going down and one number going up. Figure out which conditions mean "turn left" and which mean "turn right".
Write your sketch to steer left or right based on the values from the LDR's.
Hi,
What hardware do you have?
We can then suggest a course of action.
Tom....... ![]()
thanks for your reply.
its for polulo 3pi robot.
problem I am facing is that two LDR's showing different values as it approaches to the light
example at a distance 1.2 m away from light L1 is 601 & L2 is 504,
at .6 m ldr values are 381 & 310 and at 0.1 m values are 143 & 125
how can I calibrate it & compare the LDR value to find the light.
I tried few codes, below is one of them..it is not at all working..truing right..
#include <Pololu3pi.h>
#include <OrangutanLCD.h>
#include <PololuQTRSensors.h>
#include <OrangutanMotors.h>
#define SENSOR_LEFT 6 //pin where the left LDR is attached
#define SENSOR_RIGHT 7 //pin where the right LDR is attached
#define buzz 10
int LDR1, LDR2,LeftLimit=150,RightLimit=150;
int leftOffset = 0, rightOffset = 0;
int L=0, R=0;
int LeftSensorMax=0,LeftSensorMin=1023;
int RightSensorMax=0, RightSensorMin=1023;
void setup()
{
//calibration();
void motors_init(void);
}
void loop()
{
ReadSensors();
while (LDR1>250 || LDR2>250)
{
set_motors(25,25);
delay(10);
if (-10<=SensorDiff<=10)
{
clear();
print("straight");
}
//turn right
else if (SensorDiff<-10)
{
for (L=-30,R=30;L<0,R>0;L++,R--)
{
set_motors(L,R);
print("right");
delay(10);
}
}
else if (SensorDiff>10)
{
for (L=30,R=-30;L>0,R<0;L--,R++)
{
set_motors(L,R);
print("left");
delay(10);
}
}
calibration();
LCD_display(leftOffset,rightOffset);
}
LCD_display(leftOffset,rightOffset);
set_motors(0,0) ;
delay(500);
}
// calibrate using first 10 readings from the sensors
void ReadSensors()
{
LDR1 = analogRead(SENSOR_LEFT); // read the 2 sensors
LDR2 = analogRead(SENSOR_RIGHT)+20;
}
void calibration()
{
for (int x=0; x<10; x++)
{
ReadSensors();
leftOffset = leftOffset + LDR1; // add value of left sensor to total
rightOffset = rightOffset + LDR2; // add value of right sensor to total
//delay(5);
}
// obtain average for each sensor
leftOffset = leftOffset / 10;
rightOffset = rightOffset / 10;
// SensorDiff=leftOffset-rightOffset;
}
void LCD_display(long x,long y)
{
//long x, y;
clear();
lcd_goto_xy(3,0);
print_long(x);
lcd_goto_xy(3,1);
print_long(y);
delay(2000);
}
for (L=30,R=-30;L>0,R<0;L--,R++)
You're going to have to talk us through that.
if (-10<=SensorDiff<=10)
And that
(Did you see how I used code tags there?)
ReadSensors();
while (LDR1>250 || LDR2>250)
And what, in the body of the while loop, causes the values of LDR1 and LDR2 to change?
Its for turning the robot to left to find light if difference of two sensors is less below some value..
HI,
You will have to look at the Learning, reference section at the top of this page to see how to use if and for statements in C++.
Tom.... ![]()
AWOL was being polite and saying your use of code is very very poor and while it might compile it is not going to do what you think it will.
That code quite simply is rubbish.
please help me to improve it...It has to find light using two sensors..& it should be in C.
re-write my code... have a look & please correct me if I am wrong any part of the code
#include <Pololu3pi.h>
#include <OrangutanLCD.h>
#include <PololuQTRSensors.h>
#include <OrangutanMotors.h>
#define SENSOR_LEFT 6 //pin where the left LDR is attached
#define SENSOR_RIGHT 7 //pin where the right LDR is attached
int LDR1=0, LDR2=0;
void setup()
{
void motors_init(void);
}
void loop()
{
LDR1=analogRead(SENSOR_LEFT);
LDR2=analogRead(SENSOR_RIGHT);
LDR1=map(LDR1,0,1023,0,5);
LDR2=map(LDR2,0,1023,0,5);
LCD_display(LDR1,LDR2);
if (LDR1==0 && LDR2 ==0)
{
set_motors(0,0);
clear();
print("stop");
delay(100);
}
else if (LDR1 == LDR2)
{
set_motors(20,20);
clear();
print("striaght");
delay(100);
}
else if (LDR1>LDR2)
{
set_motors(20,10);
clear();
print("right");
delay(100);
}
else if (LDR1<LDR2)
{
set_motors(10,20);
clear();
print("left");
delay(100);
}
}
void LCD_display(int x,int y)
{
clear();
lcd_goto_xy(0,0);
print("L:");
lcd_goto_xy(3,0);
print_long(x);
lcd_goto_xy(0,1);
print("R:");
lcd_goto_xy(3,1);
print_long(y);
delay(100);
}
If your code is syntactically wrong, the compiler will tell you.
If your code is semantically wrong, your testing will tell you.
void setup()
{
void motors_init(void);
}
Using the word void in front of a function says you are going to define a function. You can not define a function inside another function which is what you are attempting to do.
Sibi78:
please help me to improve it...It has to find light using two sensors..& it should be in C.
Yes well it can't be in anything else but being in C
This is going to be a long haul. Get out all the compiler errors then we can start.
When it compiles post what code you have, and use code tags. Read the how to use this forum sticky post to see why this is very important.
void setup()
{
void motors_init(void);
}
All you're doing there is providing a function prototype for "motors_init"
This is not syntactically incorrect, so the compiler won't complain.
However, your testing will reveal that the function "motors_init" is not being called.
it was a mistake, it should read as below
void setup()
{
void motors_init();
}
here I am trying to invoke a built in function using <OrangutanMotors.h> library. It should be called once to initialize the motor, and I think it is working .
void loop()
{
//reading sensor values
LDR1=analogRead(SENSOR_LEFT);
LDR2=analogRead(SENSOR_RIGHT);
//rescaling the sensor values using map function
LDR1=map(LDR1,0,1023,0,10);
LDR2=map(LDR2,0,1023,0,10);
//comparing sensor values, & stop robot if both sensor's showing zero
if (LDR1 == 0 && LDR2 == 0)
{
delay(100);
set_motors(0,0);
clear();
print("stop");
tone(buzzor,200);
delay(50);
flag=1;
}
//comparing sensor values, & go straight if both sensor's have same value
else if (LDR1 == LDR2)
{
set_motors(leftSpeed,rightSpeed);
clear();
print("striaght");
delay(100);
}
else if (LDR1>LDR2)
{
set_motors(leftSpeed,rSpeed);
clear();
print("right");
delay(100);
}
else if (LDR1<LDR2)
{
set_motors(rSpeed,rightSpeed);
clear();
print("left");
delay(100);
}
}
It just turning (depends on the LDR value) when it is far away from the light & follow the light when it is near to the light..how can I modify the code to move the robot straight to light when it is away?
OK let's get serious. You were asked to post any code correctly using code tags.
If you are not clever enough to work out how to do that you are not clever enough to understand any answer.
If you are arrogant enough to ignore this request the you do not deserve an answer.
sorry Mike, just getting used with all these features..
my sincere apology, if you felt anything bad about me.
here is the code I would like to get it improved..
#include <Pololu3pi.h>
#include <OrangutanLCD.h>
#include <PololuQTRSensors.h>
#include <OrangutanMotors.h>
#define SENSOR_LEFT 6 //pin where the left LDR is attached
#define SENSOR_RIGHT 7 //pin where the right LDR is attached
#define buzzor 10 //pin where the buzzor is attached
int LDR1=0, LDR2=0;
// initial speeds of left and right motors
int leftSpeed=20,rightSpeed =20, rSpeed = 10;
void setup()
{
void motors_init();
//delay(1000);
}
void loop()
{
//reading sensor values
LDR1=analogRead(SENSOR_LEFT);
LDR2=analogRead(SENSOR_RIGHT);
//rescaling the sensor values using map function
LDR1=map(LDR1,0,1023,0,10);
LDR2=map(LDR2,0,1023,0,10);
//comparing sensor values, & stop robot if both sensor's showing zero
if (LDR1 == 0 && LDR2 == 0)
{
// delay(100);
set_motors(0,0);
clear();
print("stop");
tone(buzzor,200);
//delay(50);
}
//comparing sensor values, & go straight if both sensor's have same value
else if (LDR1 == LDR2)
{
set_motors(leftSpeed,rightSpeed);
clear();
print("striaght");
delay(100);
}
else if (LDR1>LDR2)
{
set_motors(leftSpeed,rSpeed);
clear();
print("right");
//delay(100);
}
else if (LDR1<LDR2)
{
set_motors(rSpeed,rightSpeed);
clear();
print("left");
//delay(100);
}
}
I would like to get it improved
In what way?
Improve the 10Hz update rate?
Get rid of the calls to delay()?
while using this code robot is turning right/left (depending on sensor value) when it is away from light.
working fine when close to light.
I want to run it straight as soon as it start (1.2 m away from light)..what condition should I use?
I want to run it straight as soon as it start (1.2 m away from light)..what condition should I use?
Print out the values from your two light sensors and see how they act. Then modify your code to take account of the real values.
Also add a range on your 'if' statements. So instead of saying if they are equal say if they are within so much of each other. To do this subtract one from the other, use the abs function to remove the sign and set a threshold below which you consider them the same.