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.