Go Down

### Topic: 3 LDR's Coding Modification for 2 LDR's (Read 1 time)previous topic - next topic

#### AbarthGT

##### Mar 29, 2013, 12:09 am
Hi Guys,

This is my 1st post to this forum, I'm currently working on a Project on a robot that follows a black line on a white surface, I found a few projects with the codes but they're all with 3 or more LDR's, My robot only has 2 LDR's one right and one left.

Could anyone help me with the modification so it only works with 2 LDR's with the coding below please(I have tried but failed)?

Code: [Select]
`#define lights 9int LDR1, LDR2, LDR3; // sensor values// calibration offsetsint leftOffset = 0, rightOffset = 0, centre = 0;// pins for motor speed and directionint speed1 = 3, speed2 = 11, direction1 = 12, direction2 = 13;// starting speed and rotation offsetint startSpeed = 150, rotate = 30; /* CHANGE START SPEED IF YOUR ROBOT IS TOO SLOW, AND CHANGE THE ROTATE VALUE ACCORDING TO YOUR TURNING ANGLE OF BLACK LINE.*/// sensor thresholdint threshhold = 5;/*Threshold is the difference in values required between the center sensor and the left or right sensorsbefore the robot decides to make a turn. In my case, a setting of 5 worked well. This means that the left and rightsensors would need to detect a value greater than the value read from the center sensor plus thethreshold value before action is taken. In other words, if the center sensor is reading a value of 600 andthe left sensor is reading 603, then the robot will keep going straight. However, a left sensor value of 612(which is higher than the center value plus threshold) means that the left sensor is detecting the backline, indicating that the robot is too far over to the left. So the motors would adjust to make the robotturn to the right to compensate.The threshold value will vary depending on the contrast between your floor (or whatever surfaceyou use) and the black line. This may need to be adjusted to ensure the robot only turns when it hasdetected enough of a difference between floor and line to ascertain it had moved too far left or right.*/// initial speeds of left and right motorsint left = startSpeed, right = startSpeed;// Sensor calibration routinevoid calibrate() {for (int x=0; x<10; x++) { // run this 10 times to obtain averagedigitalWrite(lights, HIGH); // lights ondelay(100);LDR1 = analogRead(0); // read the 3 sensorsLDR2 = analogRead(1);LDR3 = analogRead(2);leftOffset = leftOffset + LDR1; // add value of left sensor to totalcentre = centre + LDR2; // add value of centre sensor to totalrightOffset = rightOffset + LDR3; // add value of right sensor to totaldelay(100);digitalWrite(lights, LOW); // lights offdelay(100);}// obtain average for each sensorleftOffset = leftOffset / 10;rightOffset = rightOffset / 10;centre = centre /10;// calculate offsets for left and right sensorsleftOffset = centre - leftOffset;rightOffset = centre - rightOffset;}void setup(){// set the motor pins to outputspinMode(lights, OUTPUT); // lightspinMode(speed1, OUTPUT);pinMode(speed2, OUTPUT);pinMode(direction1, OUTPUT);pinMode(direction2, OUTPUT);// calibrate the sensorscalibrate();delay(3000);digitalWrite(lights, HIGH); // lights ondelay(100);// set motor direction to forwarddigitalWrite(direction1, HIGH);digitalWrite(direction2, HIGH);// set speed of both motorsanalogWrite(speed1,left);analogWrite(speed2,right);}void loop() {// make both motors same speedleft = startSpeed;right = startSpeed;// read the sensors and add the offsetsLDR1 = analogRead(0) + leftOffset;LDR2 = analogRead(1);LDR3 = analogRead(2) + rightOffset;// if LDR1 is greater than the centre sensor + threshold turn rightif (LDR1 > (LDR2+threshhold)) {left = startSpeed + rotate;right = startSpeed - rotate;}// if LDR3 is greater than the centre sensor + threshold turn leftif (LDR3 > (LDR2+threshhold)) {left = startSpeed - rotate;right = startSpeed + rotate;}// send the speed values to the motorsanalogWrite(speed1,left);analogWrite(speed2,right);}`

This was from http://www.buildcircuit.com/ldr-based-line-following-robot-using-arduino-and-ardumoto/ Great Tutorial

#### PeterH

#1
##### Mar 29, 2013, 12:44 am
I don't see why it couldn't be done, but eliminating the third LDR would need a substantially different algorithm and not just a matter of making some minor modifications.

You need to understand how the existing algorithm works, come up with your new algorithm, and then code and test it.

Go Up