Hello first post here. I have a final exam project due in 24 hours and my robot dosent work.
Its a small RC car. Four servo motors to control the wheels, a servo motor to turn/pan an ultrasonic sensor placed on top in order to detect objects. Its supposed to drive until it sees a wall, detect it, and find an appropriate alternative route before colliding with it. It literally dosent do anything as of right now. Checked all the wiring, double checked the pins and made sure they're labelled correcly. The board also keeps disconnecting and reconnecting but I think thats just the board being faulty, I can still manage to download the code before it gives me the error code. The following is the code that I made, don't know whats wrong with it, any and all help is appreaciated.
#include <Adafruit_MotorShield.h>
#include <Servo.h>
// Define pin numbers
const int trigPin = 8;
const int echoPin = 9;
const int servoPin = 10;
// Define variables
long duration;
int distance;
// Motor shield
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *Motor1 = AFMS.getMotor(1);
Adafruit_DCMotor *Motor2 = AFMS.getMotor(2);
Adafruit_DCMotor *Motor3 = AFMS.getMotor(3);
Adafruit_DCMotor *Motor4 = AFMS.getMotor(4);
Servo panServo; // Create a Servo object for pan movement
// Function to get distance using ultrasonic sensor
int getPingDist() {
digitalWrite(trigPin, LOW);
delayMicroseconds(10);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration * 0.0343) / 2;
Serial.print("Distance: ");
Serial.println(distance);
delay(1000);
return distance;
}
// Function to stop the motors
void moveStop() {
Serial.println("Stop!");
Motor1->run(RELEASE);
Motor2->run(RELEASE);
Motor3->run(RELEASE);
Motor4->run(RELEASE);
}
// Function to turn right
void turnRight() {
Serial.println("Turn Right");
Motor1->run(BACKWARD);
Motor2->run(FORWARD);
Motor3->run(FORWARD);
Motor4->run(BACKWARD);
delay(650);
moveStop();
}
// Function to turn left
void turnLeft() {
Serial.println("Turn Left");
Motor1->run(FORWARD);
Motor2->run(BACKWARD);
Motor3->run(BACKWARD);
Motor4->run(FORWARD);
delay(650);
moveStop();
}
// Function to move forward
void moveForward() {
Serial.println("Move Forward");
Motor1->run(FORWARD);
Motor2->run(FORWARD);
Motor3->run(FORWARD);
Motor4->run(FORWARD);
}
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
panServo.attach(servoPin); // Attach the pan servo to the correct pin
panServo.write(90); // Center position
Serial.println("Test Start");
if (!AFMS.begin()) {
Serial.println("Could not find Motor Shield. Check wiring");
while (1);
}
Serial.println("Motor Shield found");
Motor1->setSpeed(100);
Motor2->setSpeed(100);
Motor3->setSpeed(100);
Motor4->setSpeed(100);
}
void loop() {
int dist, ldist, rdist;
// I dont know if this is supposet to have a value of 0 or 90
int pos = 0;
panServo.write(90);
dist = getPingDist();
Serial.print("Distance = ");
Serial.println(dist);
if (dist < 10) {
moveStop();
Serial.println("Looking left");
panServo.write(60);
delay(1000);
ldist = getPingDist();
Serial.println("Looking right");
panServo.write(120);
delay(1000);
rdist = getPingDist();
panServo.write(90);
delay(100);
if (ldist > rdist) {
turnRight();
} else if (ldist < rdist) {
turnLeft();
} else {
moveForward();
delay(100);
}
}
}
This is your Arduino undergoing repeated brown-out condition. You must not use your Arduino as a power supply. Use right-sized external power to supply your motor shield. Arduino has enough power to supply the HC-SR04.
Why did it take you so long to put it together to find it did not work before asking for asking help? I beleave one of your classmates asked weeks ago, it only took a few day and posted a schematic. I suggest you ask for additional time as your apparent lack of knowledge of what you built will take some time to get around and find your projects problems.
If you need a good set of answers fast post an annotated schematic, not a bunch of pictures showing how everything is connected, all grounds, power sources, and links to technical information on the hardware items. I hope you did not take a shortcut and build it without a schematic.
I never expected to get replies so quickly, thanks for all the responses. I am the most ill equipped for the job so sorry if I'm missing information, this was handed off to me last second.
It keeps disconnecting and reconnecting to the Arduino program is best as I can describe it. When it tells you what usb input is connected. It says Arduino Uno. But then it keeps making a sound that it connects and drops and so forth.
Its powered by two battery cells that are pretty large, I cant tell you exactly what kind by looking at it, they do say 3.7v if that gives any clue. So there is external power Im not using the board as a source itself. Ive charged them up all the way to see if thats the issue, but no luck. I may be wrong on this point but these were school provided materials so I don't think I could get to the point of overpowering it and causing it to overload.
The serial monitor is showing me real time values from the ultrasonic sensor, but also says that it keeps disconnecting and reconnecting to the sensor.
I don't really know if I have the right code for DC motors. I tried changing the code to a stripped down version where it only drives in a straight line and that seems to work fine, its only after I add the rest of the code where nothing seems to work. I wish I could provide further details but this is the best of my abilities.
For those frustrated with this last second hail mary, totally get it, but Im trying every avenue I have. There are others who may share the same assignment, but probably unrelated. I was in charge of other aspect of this project, coding is nowhere near my strong suit. The more experienced member is pretty much MIA, but thats not my problem for now anyways.
So for sure I took a shortcut, I have no schematics. Regardless, thanks for all the input and help I didn't really expect much this is a pleasant surprise.
So I think the board is called Adafruit Motor Shield v2.3
The pictures are, I can explain.
So the motors that are connected to the wheels only have two wires that come out of them. They're connected to what I think are called terminal blocks, Im pretty sure they're clamped the right way. There is one on each side of the board so two sets of cables on each one, but theres nothing else going in/out of them. The servo thats supposed to pan the ultrasonic sensor has only three wires and they're fused together. There is a stand alone set of pins called 'servo 1' and 'servo 2' its plugged in to servo 1 and I found a resource that says that the corresponding pin value for the code should be 10. Other than that, the ultrasonic sensor has 4 wires but theyre labeled. One is ground going into a ground pin, another says 'VCC' and thats connected to a 5v pin, 'echo' wire is set to pin 13 on the board, and 'trig' wire is connected to pin 12 on the board. The code I posted may have different numbers but I was messing with it and updated the code accordingly. All the wires go through a breadboard, but I made sure all the wires are in the right columns to complete the circuit.
The power is coming from the batteries like I mentioned. Theres only what looks like ac/dc male output, but there is a same connection on the board for it
Sure, they're three independent wires, but there's like a plastic mold binding them together. From where they come out of from the servo motor there are no labels, and it would be hard to pull them out of there mold so I'm assuming you're not supposed to separate them. There are only 3 so on the board where its labeled 'servo 1' I was assuming that was the right spot for them. The are also different colors than the rest of the wires. They are red, orange, and a brownish purpleish color. In addition to why I think thats the right spot, the casing around the end of the wires make it so that they're 'female' and the two labeled spots for the servo 1 and 2 have the pins sticking upward as a 'male' connection. There's no other place on the arduino board like that.