Robot Code

I want to make a line following robot, with an HC SR04 attached to a servo which can then constantly turn 45 degrees, 90 degree and 135 degree angles, looking for objects in the way. However, I wan this to happen while the robot is following the line. I do not kno how to code it at all. Here is what I have done so far:

#include<Ultrasonic.h>
#include<Servo.h>
const int motorr_f=2;
const int motorr_b= 3;
const int motorl_f=4;
const int motorl_b=5;
const int left_sensor = 7;
const int right_sensor = 9;
int left_reading;
int right_reading;
int motorr_fvalue;
int motorr_bvalue;
int motorl_fvalue;
int motorl_bvalue;
Servo servo;
int output_value ;
int reading=11;
int reading2=11;
int reading3=11;


Ultrasonic ultrasonic(8, 10);
void setup() {
  // put your setup code here, to run once:
pinMode(motorr_f, OUTPUT);
pinMode(motorr_b, OUTPUT);
pinMode(motorl_f, OUTPUT);
pinMode(motorl_b, OUTPUT);
servo.attach(11);
Serial.begin(9600);
servo.write(90);
int reading = ultrasonic.read();
servo.write(45);
int reading2 = ultrasonic.read();
servo.write(135);
int reading3 = ultrasonic.read();


}

void loop() {
  // put your main code here, to run repeatedly:
left_reading=digitalRead(left_sensor);
right_reading=digitalRead(right_sensor);

while(motorr_fvalue==255 || motorl_fvalue==255){
  servo.write(90);
  reading = ultrasonic.read();
  Serial.println(reading);
   delay(250);
   servo.write(45);
   reading2 = ultrasonic.read();
   delay(250);
   Serial.println(reading2);
  servo.write(135);
  reading3 = ultrasonic.read();
  Serial.println(reading3);
  delay(250);
  
}

if(left_reading==0 && right_reading==1){
  if(reading<10 || reading2<10){
    motorr_fvalue = 0;
    motorr_bvalue = 0;
  motorl_fvalue = 0;
  motorl_bvalue = 0;
  }
  else{
    motorr_fvalue = 0;
    motorr_bvalue = 0;
  motorl_fvalue = 255;
  motorl_bvalue = 0;
  }
}
if(left_reading==1 && right_reading==0){
  if(reading<10 || reading2<10){
    motorr_fvalue = 0;
    motorr_bvalue = 0;
  motorl_fvalue = 0;
  motorl_bvalue = 0;
  }
  else{motorr_fvalue = 255;
  motorr_bvalue = 0;
  motorl_fvalue = 0;
  motorl_bvalue = 0;}
}
if(left_reading==0 && right_reading==0){
  motorr_fvalue = 0;
  motorr_bvalue = 0;
  motorl_fvalue = 0;
  motorl_bvalue = 0;
}
if(left_reading==1 && right_reading==1){
  if(reading<10 || reading2<10){
    motorr_fvalue = 0;
    motorr_bvalue = 0;
  motorl_fvalue = 0;
  motorl_bvalue = 0;
  }
 else{ motorr_fvalue = 255;
  motorr_bvalue = 0;
  motorl_fvalue = 255;
  motorl_bvalue = 0;}
}
analogWrite(motorr_f, motorr_fvalue);
analogWrite(motorr_b, motorr_bvalue);
analogWrite(motorl_f, motorl_fvalue);
analogWrite(motorl_b, motorl_bvalue);


}

What does your code do? What should it do that is different?

If it finds something blocking the line that it’s following what is it intended to do? If you put some comments in the code it would be a lot easier to see what you think that code is doing.

Steve

I just want it to stop if it sees an object. Also, I want the servo motor to continuosly be moving and the HC SR04 to continuously be recording distances as the robot moves. However, it doe snot seem to be working well

Also, the code is not 100% complete, as I am quite confused. Whenever, I just connect the components to try it out, all that happens is that one motor just keeps on rotating, and the servo just goes on moving. So, I am not so sure... It is okay even if you (or anyone) suggests a completely new code.
Please do help though, as this is for school. :confused: :confused: :confused: :confused:

I want to make a line follower that will follow a line for some time(depending on far away an object is), and then it will stop aafter that period of time, check for obstacles again, and then resume its path. While checking for obstacles, I want to use a servo to make it look left, measure distance with HC SR04, look right, do the same, and finally straight ahead. If the distance is less than 10, it should stop. Else, it should continue.
I do not know how to make it follow for a certain amount of time.
Does anyone have any code or logic diagram or flowchart?

When it starts, capture millis() into startTime. Then each visit through loop() compare the (ever increasing) new millis() to that startTime, and if "that period of time" has elapsed, stop.

You probably need to read about State Machines here, here, or here.

Here is the Arduino reference on switch...case, which is a useful coding technique when "if" gets a bit cumbersome.

What Arduino are you using? Pins 2 and 4 are not PWM pins on most so doing analogWrites to them isn't going to work.

Are you getting the values you expect from all your Serial.printlns? Why not put some extra ones in to show you the values you're writing to the motors? And why don't you ever use reading3?

Steve

Why didn't you continue your original thread? Having multiple threads for basically the same problem just wastes everyone's time.

Steve

@MukeshArvindh, do not cross-post. Threads merged.

Sorry about the cross-threading issue. It was by mistake.
I actually had two issues.
I had two codes in mind:
1-Make the servo continuosly move as the robot follows the line and take readings.
2-Makes the robot stop every once in a while(1/10 seconds of the distance from object) and then take readings.
Hence, I thought two threads would do the trick.

@slipstick,

I did not use reading 3 as I am not so sure about how to integrate it into my code.
I am using an arduino Uno R3.

As I said pins 2 and 4 are not PWM pins so move those things to pins that are PWM pins.

With the servo you move the sensor so it points ahead (90) and then to either side (45 and 135). Now describe exactly what you want the robot to do when one or more of those 3 readings detects an object with in 10cm. E.g. if there is an object to the left but not straight ahead what happens?

I don't know what "1/10 seconds of the distance from object" means? Try a different way of explaining what you want to see if it makes more sense.

You can't write a program until you have a clear idea of what you want it to do and we can't help you until you can clearly explain for us what you want it to do.

Steve

1/10 seconds of distance of object,
What I meant by this was that when we get the distance in centimetres, we divide that by 10, and then allow the robot to move for that number of seconds.

Next, if the forward distance is more than 10, and the left and right are not, then it stops. All the distances have to be more than 10.

If they are more than 10, it measures the distance, divides it by 10, runs for that amount of time, stops and takes readings again. So, does anyone know how to program the robot? I am having severe trouble. Btw, I am a beginner, and started just a few months ago.