Trouble with Robot

Hello, I am building an obstacle avoidance robot using the arduino mega, five hc-sro4 sensors, the adafruit motor shield, and four motors. For some reason whenever I upload code to the mega, it doesn’t seen to change anything. Here is the code.

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
#include <MotorTurn.h>
#include <LiquidCrystal.h>
#define trigPin1 22
#define echoPin1 24
#define trigPin2 26
#define echoPin2 28
#define trigPin3 30
#define echoPin3 32
#define trigPin4 34
#define echoPin4 36
#define trigPin5 38
#define echoPin5 40
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 

// Select which 'port' M1, M2, M3 or M4. In this case, M1
 LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// You can also make another motor on port M2
Adafruit_DCMotor *myMotor1 = AFMS.getMotor(1);
Adafruit_DCMotor *myMotor2 = AFMS.getMotor(2);
Adafruit_DCMotor *myMotor3 = AFMS.getMotor(3);
Adafruit_DCMotor *myMotor4 = AFMS.getMotor(4);

unsigned long currentMillis = millis();
unsigned long previousMillis = 0;
int interval = 50; 
void setup() {
  digitalWrite(22, OUTPUT);
  digitalWrite(24, INPUT);
  digitalWrite(26, OUTPUT);
  digitalWrite(28, INPUT);
  digitalWrite(30, OUTPUT);
digitalWrite(32, INPUT);  
  digitalWrite(34, OUTPUT);
  digitalWrite(36, INPUT);
  digitalWrite(38, OUTPUT);
  digitalWrite(40, INPUT);
  lcd.begin(16, 2);
  // Print a message to the LCD
unsigned long currentMillis = millis();
unsigned long previousMillis = 0;
int interval = 15; 
  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
  
  // Set the speed to start, from 0 (off) to 255 (max speed)
  myMotor1->setSpeed(150);
  myMotor1->run(BACKWARD);
  myMotor1->run(RELEASE);
  myMotor2->setSpeed(150);
  myMotor2->run(BACKWARD);
  myMotor2->run(RELEASE);
  myMotor3->setSpeed(150);
  myMotor3->run(BACKWARD);
  myMotor3->run(RELEASE);
  myMotor4->setSpeed(150);
  myMotor4->run(BACKWARD);
  myMotor4->run(RELEASE);

}
long duration1;
long duration2;
long duration3;
long duration4;
long duration5;
int distance1;
int distance2;
int distance3;
int distance4;
int distance5;
void loop() {
  uint8_t i;

  lcd.print("hello, world!");

myMotor2->setSpeed(150);
  myMotor3->setSpeed(150);
  myMotor1->run(BACKWARD);
  myMotor2->run(BACKWARD);
  myMotor3->run(BACKWARD);
  myMotor4->run(BACKWARD);
    unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;   
    digitalWrite(trigPin1, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin1, LOW);
  duration1 = pulseIn(echoPin1, HIGH);
  distance1 = (duration1/2) / 29.1;
  
  digitalWrite(trigPin2, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin2, LOW);
  duration2 = pulseIn(echoPin2, HIGH);
  distance2 = (duration2/2) / 29.1;
  
  digitalWrite(trigPin3, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin3, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin3, LOW);
  duration3 = pulseIn(echoPin3, HIGH);
  distance3 = (duration3/2) / 29.1;
  
  digitalWrite(trigPin4, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin4, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin4, LOW); 
  duration4 = pulseIn(echoPin4, HIGH);
  distance4 = (duration4/2) / 29.1;
  
  digitalWrite(trigPin5, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin5, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin5, LOW);
  duration5 = pulseIn(echoPin5, HIGH);
  distance5 = (duration5/2) / 29.1;
  }
  if(distance1 <75 || distance2 <75 || distance3 <75 || distance4 <75 || distance5 <75){
  myMotor1->run(RELEASE);
  myMotor2->run(RELEASE);
  myMotor3->run(RELEASE);
  myMotor4->run(RELEASE);
  left(90);
  if(distance1 <100 || distance2 <100 || distance3 <100 || distance4 <100 || distance5 <100){
    right(180);
    if(distance1 <100 || distance2 <100 || distance3 <100 || distance4 <100 || distance5 <100){
      right(90);
  }else{myMotor2->setSpeed(150);
  myMotor3->setSpeed(150);

  myMotor1->run(BACKWARD);
  myMotor2->run(BACKWARD);
  myMotor3->run(BACKWARD);
  myMotor4->run(BACKWARD);}
  }else{myMotor2->setSpeed(150);
  myMotor3->setSpeed(150);

  myMotor1->run(BACKWARD);
  myMotor2->run(BACKWARD);
  myMotor3->run(BACKWARD);
  myMotor4->run(BACKWARD);}
  }else{myMotor2->setSpeed(150);
  myMotor3->setSpeed(150);

  myMotor1->run(BACKWARD);
  myMotor2->run(BACKWARD);
  myMotor3->run(BACKWARD);
  myMotor4->run(BACKWARD);}
}
void left(int x)
{
myMotor2->setSpeed(75);
  myMotor3->setSpeed(75);
  myMotor1->run(BACKWARD);
    myMotor2->run(BACKWARD);
      myMotor3->run(FORWARD);
  myMotor4->run(FORWARD);
  delay(x*10.5777-(x*x/200.3));
  myMotor1->run(RELEASE);
  myMotor2->run(RELEASE);
  myMotor3->run(RELEASE);
  myMotor4->run(RELEASE);
}

void right(int x){
  myMotor2->setSpeed(75);
  myMotor3->setSpeed(75);
  myMotor4->run(BACKWARD);
    myMotor3->run(BACKWARD);
  myMotor1->run(FORWARD);
  myMotor2->run(FORWARD);
  delay(x*10.5777-(x*x/200.3));
  myMotor1->run(RELEASE);
  myMotor2->run(RELEASE);
  myMotor3->run(RELEASE);
  myMotor4->run(RELEASE);
}

void forward(int x){
  myMotor2->setSpeed(150);
  myMotor3->setSpeed(150);
  myMotor1->run(BACKWARD);
  myMotor2->run(BACKWARD);
  myMotor3->run(BACKWARD);
  myMotor4->run(BACKWARD);
  delay(x*66.6666666);
  myMotor1->run(RELEASE);
  myMotor2->run(RELEASE);
  myMotor3->run(RELEASE);
  myMotor4->run(RELEASE);
}
  void backward(int x){
myMotor2->setSpeed(150);
  myMotor3->setSpeed(150);
  myMotor1->run(FORWARD);
  myMotor2->run(FORWARD);
  myMotor3->run(FORWARD);
  myMotor4->run(FORWARD);
  delay(x*66.6666666);
  myMotor1->run(RELEASE);
  myMotor2->run(RELEASE);
  myMotor3->run(RELEASE);
  myMotor4->run(RELEASE);
}

Any help would be greatly appreciated.

it doesn’t seen to change anything

Explanation, please.

delay(x*10.5777-(x*x/200.3));

?

AWOL:

delay(x*10.5777-(x*x/200.3));

?

-You- can't take -anything- at face value? :astonished:

Sorry for the poor explanation. What happens is that when I have the arduino connected to my computer via usb cord it works fine but when I try to use a 9v battery to power it via vin it does the same (wrong) thing every time. When it is connected to the computer via usb, it works.

when I try to use a 9v battery

One of those tiny 9V batteries that runs your smoke alarm?
I think we have an answer.

Whats wrong with a 9v battery?

Apart from the cost, the low energy density and all the heat it wastes in the regulator?

Why doesn't the arduino run the proper sketch with a 9v via the vin port?

CoolBot:
Why doesn't the arduino run the proper sketch with a 9v via the vin port?

You can't run motors off of a 9V battery so don't waste your time/batteries!
Use a big Lead Acid or NiMH battery pack. Its also very much suggested that you set up two power supplies (split supply) one for the Arduino and one for the motors. 99% of 'weird motor problems' are due to noise on the power line from sharing power supplies and/or not having a powerful enough supply! Even small DC motors can draw up to 3 Amps when they stall.