Is it even posable to make a line following robot with just 2 Photo-Resistors?
int pRL = 2;
int pRR = 0;
int RL;
int RR;
int R1;
int R2;
void setup(void) {
Serial.begin(9600);
}
void loop(void) {
#define DIFF 25
RL = analogRead(pRL);
RR = analogRead(pRR);
R1 = (RR-DIFF);
R2 = (RR+DIFF);
if(RL > (RR-DIFF) && RL < (RR+DIFF)){
Serial.println("Go");
}
if(RL < R1 || RL > R2){
Serial.println("Left");
}else{
Serial.println("Right");
}
delay(100);
}
it works for turning left, but as soon as its suppose to go straight, it says forward and right. is there any posable logic to make this work?
Is it even posable to make a line following robot with just 2 Photo-Resistors?
Well, you'll need some motors and some kind of controller, but yes.
I've even seen line followers with only one photo-sensor.
R1 = (RR-DIFF);
R2 = (RR+DIFF);
if(RL > (RR-DIFF) && RL < (RR+DIFF)){
Why bother calculating R1 and R2 if you don't use them?
Can I introduce you to "abs" (nothing to do with tummy muscles)
http://arduino.cc/en/Reference/Abs
(Uncompiled, untested)
const int deadBand = 25;
const int leftPin = 2;
const int rightPin = 0;
void setup(void)
{
Serial.begin(9600);
}
void loop(void)
{
int diff = analogRead(leftPin) - analogRead(rightPin);
if(abs (diff) < deadBand) { // "equal" illumination
Serial.println("Go");
} else {
if(diff < 0){
Serial.println("Left"); // or vice versa
}else{
Serial.println("Right");
}
}
delay (100);
}