so guys i am making a line follower robot and wanted to do some initial line detection , so that even if i dont put it on the line it will detect it. So i cant get the motion to work, it does not even start. im rotating the robot 180 degree if no line is detected, it will move a bit forward and start motion again until a line is found..
i have tested components individually so problem is with the code.
im using an arduino uno, Adafruit motor shield v1 and two 5v hobby motors and a single ir sensor.
P.s I haven't added line following yet..the code is only for line detection
#include <AFMotor.h>
AF_DCMotor LM(3);
AF_DCMotor RM(4);
bool State;
int i;
int y =0;
int Motion(){
while (State==HIGH) {
for (i=0;i<=2000;i++)
{
State=digitalRead(A0);
if (State==HIGH){
LM.run(BACKWARD);
delay(i);
RM.run(FORWARD);
delay(i); }
else {
y=1;
break;
} }
for (i=0;i<=2000;i++)
{
State=digitalRead(A0);
if (State==HIGH){
LM.run(FORWARD);
delay(i);
RM.run(BACKWARD);
delay(i);}
else {
y=1;
break;
}
State = digitalRead(A0);
if (State==HIGH) {
LM.run(FORWARD);
delay(500);
RM.run(FORWARD);
delay(500);}
State = digitalRead(A0);
if (y=1) {
break;
}
} } }
void setup() {
LM.setSpeed(255);
RM.setSpeed(255);
pinMode(A0,INPUT_PULLUP);
//IR Sensor Is At A0 Pin
State = digitalRead(A0);
Motion();
}
void loop() {
// put your main code here, to run repeatedly:
}
Because there's nothing wrong with it as far as the compiler's concerned.
As long as you comply with a fairly basic set of rules you can put just about anything in those bits of a for statement, and the compiler will dutifully do it's job. Even if it's nonsense.
what's the point of trying to move backward and forward for 1/a few millisecond? how fast does your motor spins? may be you could start with i at 1 second or something. give a chance for the robot to do actually something
Actually i am saying for the program to stop as soon as it detects the line... So actually if no line is detected it will move onto completing a whole 2 secs. Else if it detects a line it will stop its motion. Thats why delay is set to 1/1000th of sec so that it can stop at a point if line is detected.
SighBorg121:
Actually i am saying for the program to stop as soon as it detects the line... So actually if no line is detected it will move onto completing a whole 2 secs. Else if it detects a line it will stop its motion. Thats why delay is set to 1/1000th of sec so that it can stop at a point if line is detected.
well the way I would read this:
for (i = 0; i <= 2000; i++) {
State = digitalRead(A0);
if (State == HIGH) {
LM.run(BACKWARD);
delay(i);
RM.run(FORWARD);
delay(i);
}
else {
y = 1;
break;
}
}
is like this:
if you are not on the line
MOVE BAKWARD FOR A BIT OF TIME (super short initially)
MOVE FORWARD FOR THE SAME BIT OF TIME (super short initially)
--> so you end up in the same place as you started with
then do it again..
as you always come back to the same place (minus motor approximation / slippage) if you are not on the line in the first place, there are very few chances you'll be on the line the next time...
did I miss something ? (does your sensor maintain a LOW as soon as it detects the line?)
Yup, many people believe that a common ir sensor is not very accurate so people actually use two, anyways i tried the code of LINE FOLLOWING and it worked just fine..
Actually the clockwise and anticlockwise rotation is actually for detecting the line if it is not upfront but rather a bit side way..
Just think that im making a robot behave as a servo in order to detect if line is in its parameters.
No worries, yeah my bad i shouldve clarified it a bit more.
But what youre saying does have weight in it..
Lets consider what youve written as our first piece of code.. What if the line is a bit to the side?..... How about just rotating to one side like a complete 360 degree rotation rather than a 180 degree Left to Right And Right To Left Rotation(As ive done in the code).
well I would say pick a direction and move slowly straight for a short while to see if you hit the line as you move. if you hit the line, stop.
if you don't start driving in circle (not easy) to see if you hit the line...
you could also pick the "hub and spoke" approach to always come back to your starting position if terrain might be "hostile"...
so I would turn on the motor to go straight and then do a
unsigned long startMovingTime = millis();
boolean foundTheLine = false;
// start going straight
...
// then detect for a while
while (millis()-startMovingTime <= 2000) {
if (digitalRead(A0) == LOW) {
foundTheLine = true;
// stop motor
...
break;
}
}
if (foundTheLine) {
// you are good
} else {
// timeout, no line was found try going somewhere else and loop
}
So let me just understnd your code... What you have elaborated is how to detect the line while you are moving forward and if line is detected then the robot stops..
Hmmm...
Looks Good... Let me try it out tommorow and notify you back..
Ok ive done the one in which it detects the line while moving forward but ive tried numerous of self made codes and none of them works..
I wanted that if a time passed and the line was not detected then it will stop and rotate for a fixed time while also detecting the line in between rotation.