Problem with the arduino car

I am not sure if I am posting this in the right section, so apologies in advance!
So I have this portion of code:

#include <AFMotor.h>
#include <NewPing.h>
#include <Servo.h> 

#define trigPin A1 
#define echoPin A0 
#define maxDistance 200 
#define maxSpeed 50 
#define speedOffset 20

NewPing sonar(trigPin, echoPin, maxDistance); 

AF_DCMotor motor1(1); 
AF_DCMotor motor2(2);

Servo servo;   

boolean movesForward = false;
int distance = 100;
int speedSet = 0;

void setup() {
  servo.attach(10);  
  servo.write(58); 
  delay(20);
  distance = readDistance();
}


void loop() {
 
 if(distance <= 20){
       
   moveBackward();
   delay(300);
   stopMovement();
   turnLeft90();
   delay(300);
   stopMovement();
   moveUp();
   delay(350);
   stopMovement();
   turnRight90();
   delay(300);
   stopMovement();
   moveUp();
   delay(350);
   stopMovement();
   turnRight90();
   delay(300);
   stopMovement();
   moveUp();
   delay(50);
   turnLeft90();
   delay(300);
   stopMovement();

 } 
 distance = readDistance();
}

int readDistance() { 
  delay(70);
  int cm = sonar.ping_cm();
  if(cm == 0){
    cm = 200;
  }
  return cm;
}

void stopMovement() {
  motor1.run(RELEASE); 
  motor2.run(RELEASE);
} 

void moveUp(){
  
    Serial.println("Up!");
    motor1.run(FORWARD);      
    motor2.run(FORWARD);  
    
}
void moveBackward() {
    movesForward = false;
    motor1.run(BACKWARD);      
    motor2.run(BACKWARD);  
    for (speedSet = 0; speedSet < maxSpeed; speedSet += 2){
      motor1.setSpeed(speedSet + speedOffset);
      motor2.setSpeed(speedSet + speedOffset);
      delay(5);
    }
}  
void turnRight90() {

  motor2.run(RELEASE); 
  motor1.run(FORWARD);
  delay(90);      
   
}

void turnLeft90() {
  motor1.run(RELEASE);
  motor2.run(FORWARD);     
  delay(90);
}

What happens here is that the arduino car sees an obstacle and avoids it on the left side, going back to the route.
However when i add this method "pathSquare()" anywhere in the loop(), it doesn't do anything. The code for the method (it should make the car drive in a square path):

void pathSquare(){
 for(int i = 1; i < 4; i++){
   Serial.println(i);
   moveUp();
   delay(300);
   stopMovement();
   turnLeft90();
   delay(300);
 }
}

Congratulations on finding the information about using code tags. Unfortunately you missed the part about posting your complete code.

Your snippets contain all sorts of variables and function calls and we can have no idea of how they are defined or what they actually do. So there's not much chance of working out what is wrong.

Steve

I didn't want to make it longer than it has to be, but i'll add them in a sec :slight_smile:

dawnR:
I didn't want to make it longer than it has to be, but i'll add them in a sec :slight_smile:

Please post the complete program in your next Reply rather than editing earlier Replies. It makes the Thread easier to read from top to bottom.

...R

I think you've missed the point here. What we need to see is the complete program THAT HAS THE PROBLEM. Showing us a program that doesn't contain the code that you say doesn't work is completely pointless.

Steve

dawnR:
However when i add this method "pathSquare()" anywhere in the loop(), it doesn't do anything. The code for the method (it should make the car drive in a square path):

void pathSquare(){

for(int i = 1; i < 4; i++){
  Serial.println(i);
  moveUp();
  delay(300);
  stopMovement();
  turnLeft90();
  delay(300);
}
}

I think i stated that clearly here. The pathSquare() method doesn't work when i call it inside the loop() method. I tried calling moveUp();
delay(300);
stopMovement();
turnLeft90();
delay(300);
without the for loop but that also doesn't work.

dawnR:
I think i stated that clearly here.

Please post the complete program that demonstrates the problem. If we make assumptions about what you did we are likely to be wrong.

...R

Basically this:

void loop() {
pathSquare();

}

No, you're lying. That doesn't even compile.

If you can't be bothered to provide any useful information when requested then I expect most people won't bother trying to help you. Good luck.

Steve

I truly don't understand what is the problem here, i posted the whole code, said that the loop function works with the code inside of it, but when i put pathSquare method instead of that code, it doesn't do what it's supposed to, also i posted the code for pathSquare method. I don't know how else to explain it???

dawnR:
I truly don't understand what is the problem here, i posted the whole code,

In which of your Replies?

(just in case I missed it).

...R