Hello there. My Task is writing code for robot that needs to 3 tasks

the robot needs first follow a black line, then enter a maze, then hit a patch of the black tape and go back to follow a black line but i never done any coding projekt this big before so I copy paste a lot from what i found. i have to use the motor shield and the adafruit V1 Libary. I cannot use New Ping since the V1 libary also uses the Timer 2 from what i could read. The line follow works well enough for my purpose. To make it more mangeable i only do the maze mode for now. My problem is that the robot is really allover the place, sometimes it gets stuck for no reason and sometimes i just spinns in cirles and the most frustation thing is that i cannot reproduce the issues. Maybe somebody has any ideas what i can do ?

#include <AFMotor.h>

#define trigPin1 10 //Front
#define echoPin1 7

#define trigPin2 12 //Right
#define echoPin2 13 

#define trigPin3 8 //Left
#define echoPin3 9



AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
//AF_DCMotor motor3(3, MOTOR12_1KHZ);
//AF_DCMotor motor4(4, MOTOR12_1KHZ);


void setup() {

  //declaring pin types
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);

  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);

  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);
  delay(250);
  //begin serial communication
  Serial.begin(9600);
}

//-----------------------------------------------------------------------------------------------------------------Move command

void forward() {

  motor1.run(FORWARD);
  motor1.setSpeed(35);
  motor2.run(FORWARD);
  motor2.setSpeed(35);
  
}

void veryfast() {

  motor1.run(FORWARD);
  motor1.setSpeed(255);
  motor2.run(FORWARD);
  motor2.setSpeed(255);

}

void turn_left() {

  motor1.run(BACKWARD);
  motor1.setSpeed(40);
  motor2.run(FORWARD);
  motor2.setSpeed(40);
  
}

void turn_right() {

  motor1.run(FORWARD);
  motor1.setSpeed(40);
  motor2.run(BACKWARD);
  motor2.setSpeed(40);

}

void reverse() {

  motor1.run(BACKWARD);
  motor1.setSpeed(40);
  motor2.run(BACKWARD);
  motor2.setSpeed(40);

}

void stop() {

  motor1.run(RELEASE);
  motor1.setSpeed(0);
  motor2.run(RELEASE);
  motor2.setSpeed(0);
}


void loop() {

  //------------------------------------------------------------------------------------------------------------------------------

  long durfront, durright, durleft, FrontSensor, RightSensor, LeftSensor;

  digitalWrite(trigPin1, LOW);
  delayMicroseconds(2);  // delays are required for a succesful sensor operation.
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10);  
  digitalWrite(trigPin1, LOW);
  delayMicroseconds(2);  
  durfront = pulseIn(echoPin1, HIGH);
  FrontSensor = durfront * 0.034 / 2;  // convert the distance to centimeters.


  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);  // delays are required for a succesful sensor operation.
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);  
  durright = pulseIn(echoPin2, HIGH);
  RightSensor = durright * 0.034 / 2;  // convert the distance to centimeters.


  digitalWrite(trigPin3, LOW);
  delayMicroseconds(2);  // delays are required for a succesful sensor operation.
  digitalWrite(trigPin3, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigPin3, LOW);
  delayMicroseconds(2); 
  durleft = pulseIn(echoPin3, HIGH);
  LeftSensor = durleft * 0.034 / 2;  // convert the distance to centimeters.}


  Serial.print("Front = ");
  Serial.println(FrontSensor);
  Serial.print("Right = ");
  Serial.println(RightSensor);
  Serial.print("Left = ");
  Serial.println(LeftSensor);
  Serial.println();

  //delay(500);

  //------------------------------------------------------------------------------------------------ Maze:  
  
  /*if (FrontSensor > 10)
  { 
           if(LeftSensor > 7 && LeftSensor < 11) 
              {  

              forward();

              }
         
           if(LeftSensor >=11)  
              {

               turn_left();

              }
             
                 
           if(LeftSensor <=6)   
              {

               turn_right();

              }
  }*/

  if (FrontSensor <= 6 && RightSensor <= 7 && LeftSensor <= 7) 
  {  
    
    reverse();
    delay(70);
    turn_right();
    delay(300);  

  }
  //------------------------------------------------------------------------------------ obstacle on right and front sides
  else if (FrontSensor <= 5 && RightSensor <= 6 && LeftSensor >= 6) 
  {
    
    turn_left();
    //delay(2);
    // turn left side
   
  }
  //------------------------------------------------------------------------------------ obstacle on left and front sides
  else if (FrontSensor <= 5 && RightSensor >= 6 && LeftSensor <= 6)  
  {
    
    turn_right();
    //delay(2);  
    // turn right side

  }
  //----------------------------------------------------------------------------------- right > left 
  else if (FrontSensor <= 6 && RightSensor >= LeftSensor)  
  {

    turn_right();
    delay(2); 

  }
  //----------------------------------------------------------------------------------- right < left 
  else if (FrontSensor <= 6 && RightSensor <= LeftSensor)  
  {

    turn_left();
    delay(2);  

  }
  //----------------------------------------------------------------------------------- right 1 sensor 
  
  /*else if (FrontSensor >= 6 && RightSensor <= 7 && LeftSensor >= 7)  
  {
    
    turn_right();
    //delay(30);
   
  }
  //------------------------------------------------------------------------------------ left 1 sensor
  else if (FrontSensor >= 6 && RightSensor >= 7 && LeftSensor <= 7)  
  {
    
    turn_left();
    //delay(30);

  }*/

  else  
  {

    forward();
    
  }
}
    
 

Take it step by step.
Test test test.
Do not take the next step before you are sure that your last step is tested and approved.
Come here with code, wiring schematic, expected behaviour, observed behaviour of your robot...
Most people here will not do your homework for you...

Hello idontknowwhatidoo

In general, you should not use magic numbers and comments.

The I/O pins love to have a functional name.

Have a nice day and enjoy coding in C++.

did you even read what i wrote above my code ?
i tested lots. .. that why e.G. New Ping and V1 adafruit motor shield wont ever work.
i know that sensors work as expected and in range of +- 0.5 cm of the real distance.
the thing is i really dont know why robot does what it does.
i dont expect that you do my home work. But a real tipp would be helpful

magic numbers ?
i named every pin didnt i ?
i dont really understand that could you explain that part ?

Sure, magic numbers.

Are you really considered this pin names to be informative?

You added comments like "Left", "Front" after defines. Why not included it in the names?

#define frontTrigPin 10 //Front
#define frontEchoPin 7
1 Like

ok sure. has it any meanigful impact ?
i think its very clear which pin goes to where.
especially since in only use it in the US_SENS function.
and for the logic is every where else its front, right and left.

If it is all over the place, you most go back and take smaller steps...
Sorry if that upsets you.

Think of it like a recipe. The first thing we read are the ingredients.

Bad

ingredient A
ingredient B

Better

white powder A
white powder B

Best

sugar
salt

Now this is a tiny sketch, and you do have some comments. I for one don't make much use of comments like that, at this point I don't even see them. Too often they are either redundant, out of date, or just plain wrong. I have no brain power to memorize that left is this and right is that and so forth.

And after a slew of writing to pins we may or may not remember are left or right or salt and sugar you do

LeftSensor = durleft * 0.034 / 2

which relieves the tension of "what pins are these anyways?".

But numbered variables are easily typographic errors, so to find errors in your code, we have to scrutinize them - if they included left, right, up &c. neither of us cou,d miss ( or woukd likely make) a mistake.

Also, look at those blocks of code… three nearly identical sets of lines you must squint at - are they the same, except a bit different, or are they different beyond addressing another set of pins?

A perfect reason to make a function that does those lines, and at the higher level call it three times. No question that you are doing the same thing. No opportunity for a stray error in one block hanging you (or us) up.

I say again with a trivial sketch it makes little difference. But this is a forum where we hope ppl come not only to fix a problem quick before <whatever>, but to learn.

So take our remarks not as criticism but advice. As your projects grow larger, having established good habits even when you are just at McDonalds will mean better success, very especially if you are not the only one you expect to read, understand, modify and fix any code you are working with.

HTH and srsly, not criticizing, trying to explain why we so uptight.

a7

i am sorry if i sounded salty. i know you spend time with my stupid issue.
the thing is that i have done all the smaller steps. i am already working on this for 3 months.
I have done Just the motors.
i have tested 1 sensor.
i have tested 3 sensors.
i have working code for the line follow.
i really would like any comment on the maze logic, since there is my problem.

Follow the wall on the right hand side. If there is no multiple levels in your maze, that will always get you out...

It gets stuck FOR a reason. Here is one "no reason"... you check right and left... if both are <= 7, turn left (reverse and turn right)...

  if (FrontSensor <= 6 && RightSensor <= 7 && LeftSensor <= 7)
  {

    reverse();
    delay(70);
    turn_right();
    delay(300);

  }

First line should be:

  if (FrontSensor <= 6 && RightSensor <= 7 && LeftSensor >= 7)

Hi, @idontknowwhatidoo
Welcome to the forum

Can you please tell us what "maze mode" is and how your robot detects the maze walls?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Wait a couple of months, and try to remember what you did.

I now need to wait far less than a period measured in months. :expressionless:

a7

i have 3 Ultrasonic senssors (thats the frist thing i do in the loop with digital write etc.) . And the maze mode i did is in the void loop above. I tried quiet bit diffrent things. thats why there is a lots of code commented out.

doenst really help. -_-

Thank you for a real helpful comment. i will try that later

My two cents:
You have too much code, and you're pinging too frequently.

Hi,

Have you written three lots of code to test each of the three ultrasonic units.

Don't go rushing into masses of code and try and get it working.

Start in stages, develop your code, you will learn more than just cut and pasting everyone elses.

Tom... :smiley: :+1: :coffee: :australia: