Ok so far I got the DC motors connected and the servo along with the Ultrasonic Sensor. I got the code uploaded and it only seems to run the DC motors forwards then back. I think within the code I don't have some sort of connection line for the Ultrasonic sensor and the servo. Could someone please help me out with this I think it's a coding issue.
//Servo
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
//Ultra sonic sensor
#include <AFMotor.h > //Adafruit Motor Driver Shield library
long duration; //duration of ultrasonic pulse
int distanceCm; //distance in cm
//Motors
#include <AFMotor.h>
AF_DCMotor m1(1);//define motor 1 as m1
AF_DCMotor m2(2);//define motor 2 as m2
AF_DCMotor m3(3);//define motor 3 as m3
AF_DCMotor m4(4);//define motor 4 as m4
void setup() {
//Servo
myservo.attach(9); // attaches the servo on pin 9 to the servo object
//Ultra sonic sensor
Serial.begin (9600);
pinMode(A1, OUTPUT); //Analog pin A1 connected to TRIG
pinMode(A0, INPUT); //Analog pin A0 connected to ECHO
//Motors
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Robojax Motor test!");
// initial state of motor turn on motor
m1.setSpeed(0);//motor 1 is turned off to turn on change 0, to 255
m2.setSpeed(0);//motor 2 is turned off
m3.setSpeed(0);//motor 3 is turned off
m4.setSpeed(0); //motor 4 is turned off
}
void loop() {
//servo
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
//Ultra sonic sensor
digitalWrite(A1, LOW);
delayMicroseconds(2);
digitalWrite(A1, HIGH); //give a pulse of 10us on TRIG
delayMicroseconds(10);
digitalWrite(A1, LOW);
duration = pulseIn(A0, HIGH); //check time elasped in receiving back the pulse on ECHO
distanceCm = duration * 0.034 / 2; //convert to distance in cm
if (distanceCm <= 20) //if distance less than 20cm display on monitor
{
Serial.println(distanceCm);
delay(1000);
}
else
{}
//motors
uint8_t i;
Serial.println("Motor 1 FORWARD 100% speed");
m1.run(FORWARD);
m1.setSpeed(speed(100));
Serial.println("m2 FORWARD 100%");
m2.run(FORWARD);
m2.setSpeed(speed(100));
Serial.println("Motor 3 FORWARD 100% speed");
m3.run(FORWARD);
m3.setSpeed(speed(100));
Serial.println("Motor 4 FORWARD 100% speed");
m4.run(FORWARD);
m4.setSpeed(speed(100));
delay(3000);
m1.run(RELEASE);
Serial.println("M1 RELEASE");
m2.run(RELEASE);
Serial.println("m2 RELEASE");
m3.run(RELEASE);
Serial.println("M3 RELEASE");
m4.run(RELEASE);
Serial.println("M4 RELEASE");
delay(3000);
Serial.println("M1 BACKWARD 80%");
m1.run(BACKWARD );
m1.setSpeed(speed(80));
Serial.println("m4 BACKWARD 80%");
m2.run(BACKWARD );
m2.setSpeed(speed(80));
Serial.println("M1 BACKWARD 80%");
m3.run(BACKWARD );
m3.setSpeed(speed(80));
Serial.println("M1 BACKWARD 80%");
m4.run(BACKWARD );
m4.setSpeed(speed(80));
delay(3000);
delay(3000);
m1.run(RELEASE);
Serial.println("M1 RELEASE");
m2.run(RELEASE);
Serial.println("m2 RELEASE");
m3.run(RELEASE);
Serial.println("M3 RELEASE");
m4.run(RELEASE);
Serial.println("M4 RELEASE");
delay(3000);
Serial.println("=============");
}
/*
* get pereentage value 0 to 100% and
* conversts it to 0 to 255 which is motor speed used by Arduino
* written by Ahmad Shamshiri on Feb 12, 2021
*/
int speed(int b)
{
return map(b, 0, 100, 0, 255);
}
This code was created by cutting and pasting blocks of code without understanding how they work? If so, that approach is a lottery.
Test each sensor and motor separately, using example code from the library for each one. Then you know that you have connected each one correctly and that your hardware is not faulty.
maybe the hardware is faulty the servo only moves like a degree like there's power, a couple of times it did move, which is hard too explain it was going wild
Ok what's wrong with this code it comes up with an error on the first line saying no such file or directory when I installed the adafruit motor drivers.
This code seems to make way more sense even though I don't fully understand it I suppose but this error comes up!
#include <AfMotor.h> //Import library to control motor shield
#include <Servo.h> //Import library to control the servo
AF_DCMotor rightBack(1) //Create an object to control each motor
AF_DCMotor rightFront(2)
AF_DCMotor leftFront(3)
Af_DCMotor leftBack(4)
Servo servoLook; //Create an object to control the servo
byte trig = 2; //Assign the ultrasonic sensor pins
byte echo = 13;
byte maxDist = 150; //Maximum sensing distance (Objects further than this distance are ignored)
byte stopDist = 50; //Minimum distance from an object to stop in cm
float timeOut = 2*(maxDist+10)/100/340*1000000; //Maximum time to wait for a return signal
byte motorSpeed = 55; //The maximum motor speed
int motorOffset = 10; //factor to account for one side being more powerful
int turnSpeed = 50; //Amount to add to motor speed when turning
void setup()
{
rightBack.setSpeed(motorSpeed); //Set the motors to the motor speed
rightFront.setSpeed(motorSpeed);
leftFront.setSpeed(motorSpeed+motorOffset;
rightBack.run(RELEASE); //Ensure all motors are stopped
rightFront.run(RELEASE);
leftFront.run(RELEASE);
leftBack.run(RELEASE);
servoLook.attach(10); //Assign the servo pin
pinMode(trig,OUTPUT); //Assign ultrasonic sensor pin modes
pinMode(echo,INPUT);
}
void loop()
{
servoLook.write(90); //Set the servo to look straight ahead
delay(750);
int distance = getDistance(); //Check that there are no objects ahead
if(distance >= stopDist) //If there are no objects within the stopping distance, move forward
{
moveforward();
}
while(distance >= stopDist) //Keep checking the object distance until it is within the minimum stopping distance
{
distance = getDistance();
delay(250);
}
stopMove(); //Stop the motors
int turnDir = checkDirection(); //Check the left and right object distances amd get the turning instruction
Serial.print(turnDir);
switch (turnDir) //Turn left, turn around or turn right depending on the instruction
{
case 0: //Turn left
turnLeft (400);
break;
case 1: //Turn around
turnLeft (700);
break;
case 2: //Turn right
turnRight (400);
break;
}
}
void accelerate() //Function to accelerate the motors from 0 to full speed
{
for (int i=0; i<motorSpeed; i++) //Loop from 0 to full speed
{
rightBack.setSpeed(i); //Set the motors to the current loop speed
rightFront.setSpeed(i);
leftFront.setSpeed(i+motorOffset);
leftBack.setSpeed(i+motorOffset);
delay(10);
}
}
void moveForward() //Set all motors to run forward
{
rightBack.run(FORWARD);
rightFront.run(FORWARD);
leftFront.run(FORWARD);
leftBack.run(FORWARD);
}
void stopMove() //Set all motors to stop
{
rightBack.run(RELEASE);
rightFront.run(RELEASE);
leftFront.run(RELEASE);
leftBack.run(RELEASE);
}
void turnLeft(int duration) //Set motors to turn left for the specified duration then stop
{
rightBack.setSpeed(motorSpeed+turnSpeed); //Set the motors to the motor speed
rightFront.setSpeed(motorSpeed+turnSpeed;
leftFront.setSpeed(motorSpeed+motorOffset+turnSpeed);
leftBack.setSpeed(motorSpeed+motorOffset+turnSpeed);
rightBack.run(FORWARD);
rightFront.run(FORWARD);
leftFront.run(BACKWARD);
leftBack.run(BACKWARD);
delay(duration);
rightBack.setSpeed(motorSpeed); //Set the motors to the motor speed
rightFront.setSpeed(motorSpeed);
leftFront.setSpeed(motorSpeed+motorOffset);
leftBack.setSpeed(motorSpeed+motorOffset);
rightBack.run(RELEASE);
rightFront.run(RELEASE);
leftBack.run(RELEASE);
leftFront.run(RELEASE);
}
void turnRight(int duration) //Set motors to turn right for the specified duration then stop
{
rightBack.setSpeed(motorSpeed+turnSpeed); //Set the motors to the motor speed
rightFront.setSpeed(motorSpeed+turnSpeed);
leftFront.setSpeed(motorSpeed+motorOffset+turnSpeed);
leftBack.setSpeed(motorSpeed+motorOffset+turnSpeed);
rightBack.run(BACKWARD);
rightFront.run(BACKWARD);
leftFront.run(FORWARD);
leftBack.run(FORWARD);
delay(duration);
rightBack.setSpeed(motorSpeed); //Set the motors to the motor speed
rightFront.setSpeed(motorSpeed);
leftFront.setSpeed(motorSpeed+motorOffset);
leftBack.setSpeed(motorSpeed+motorOffset;
rightBack.run(RELEASE);
rightFront.run(RELEASE);
leftFront.run(RELEASE);
leftBack.run(RELEASE);
}
int getDistance() //Measure the distance of an object
{
unsigned long pulseTime; //Create a variable to store the pulse travel time
int distance; //Create a variable to store the calculated distance
digitalWrite(trig. HIGH); //Generate a 10 microsecond pulse
delayMicroseconds(10);
digitalWrite(trig,LOW);
pulseTime = pulseIn(echo, HIGH, timeOut); //Measure the time for the pulse to return
distance = (float)pulseTime *340 / 2 / 10000; //Calculate the object distance based on the pulse time
return distance;
}
int checkDirection() //check the left and right directions and decide which way to turn
{
int distances [2] = {0,0}; //Left and right distances
int turnDir = 1; //Direaction to turn, 0 left, 1 reverse, 2 right
servoLook.write(180); //Turn servo to look left
delay(500);
distances [0] = getDistance(); //Get the left object distance
servoLook.write(0); //Turn servo to look right
delay(1000);
distances [1] = setDistance(); //Get the right object distance
if (distances[0]>=200 && distances[1]>=200) //If both directions are clear, turn left
turnDir = 0;
else if (distances[0]<=stopDist && distances[1]<=stopDist) //If both directions are blocked, turn around
turnDir = 1;
else if (distances[0]>=distances[1]) //If left has more space, turn left
turnDir = 0;
else if (distances[0]<distances[1]) //If right has more space, turn right
turnDir = 2;
return turnDir;
}
I fixed some small errors in the code and added a few things that sort of made sense when something wasn't declared but not it's saying 'Error compiling for board Arduino Uno.'
I installed about every motor shield library I could think of
What's wrong with the code?
#include <AFMotor.h> //Import library to control motor shield
#include <Servo.h> //Import library to control the servo
AF_DCMotor rightBack(1) //Create an object to control each motor
;AF_DCMotor rightFront(2)
;AF_DCMotor leftFront(3)
;AF_DCMotor leftBack(4)
;Servo servoLook; //Create an object to control the servo
byte trig = 2; //Assign the ultrasonic sensor pins
byte echo = 13;
byte maxDist = 150; //Maximum sensing distance (Objects further than this distance are ignored)
byte stopDist = 50; //Minimum distance from an object to stop in cm
float timeOut = 2*(maxDist+10)/100/340*1000000; //Maximum time to wait for a return signal
byte motorSpeed = 55; //The maximum motor speed
int motorOffset = 10; //factor to account for one side being more powerful
int turnSpeed = 50; //Amount to add to motor speed when turning
void setup()
{
rightBack.setSpeed(motorSpeed); //Set the motors to the motor speed
rightFront.setSpeed(motorSpeed);
leftFront.setSpeed(motorSpeed+motorOffset);
rightBack.run(RELEASE); //Ensure all motors are stopped
rightFront.run(RELEASE);
leftFront.run(RELEASE);
leftBack.run(RELEASE);
servoLook.attach(10); //Assign the servo pin
pinMode(trig,OUTPUT); //Assign ultrasonic sensor pin modes
pinMode(echo,INPUT);
}
void loop()
{
servoLook.write(90); //Set the servo to look straight ahead
delay(750);
int distance = getDistance(); //Check that there are no objects ahead
if(distance >= stopDist) //If there are no objects within the stopping distance, move forward
{
void moveforward();
}
while(distance >= stopDist) //Keep checking the object distance until it is within the minimum stopping distance
{
distance = getDistance();
delay(250);
}
stopMove(); //Stop the motors
int turnDir = checkDirection(); //Check the left and right object distances amd get the turning instruction
Serial.print(turnDir);
switch (turnDir) //Turn left, turn around or turn right depending on the instruction
{
case 0: //Turn left
turnLeft (400);
break;
case 1: //Turn around
turnLeft (700);
break;
case 2: //Turn right
turnRight (400);
break;
}
}
void accelerate() //Function to accelerate the motors from 0 to full speed
{
for (int i=0; i<motorSpeed; i++) //Loop from 0 to full speed
{
rightBack.setSpeed(i); //Set the motors to the current loop speed
rightFront.setSpeed(i);
leftFront.setSpeed(i+motorOffset);
leftBack.setSpeed(i+motorOffset);
delay(10);
}
}
void moveForward() //Set all motors to run forward
{
rightBack.run(FORWARD);
rightFront.run(FORWARD);
leftFront.run(FORWARD);
leftBack.run(FORWARD);
}
void stopMove() //Set all motors to stop
{
rightBack.run(RELEASE);
rightFront.run(RELEASE);
leftFront.run(RELEASE);
leftBack.run(RELEASE);
}
void turnLeft(int duration) //Set motors to turn left for the specified duration then stop
{
rightBack.setSpeed(motorSpeed+turnSpeed); //Set the motors to the motor speed
rightFront.setSpeed(motorSpeed+turnSpeed);
leftFront.setSpeed(motorSpeed+motorOffset+turnSpeed);
leftBack.setSpeed(motorSpeed+motorOffset+turnSpeed);
rightBack.run(FORWARD);
rightFront.run(FORWARD);
leftFront.run(BACKWARD);
leftBack.run(BACKWARD);
delay(duration);
rightBack.setSpeed(motorSpeed); //Set the motors to the motor speed
rightFront.setSpeed(motorSpeed);
leftFront.setSpeed(motorSpeed+motorOffset);
leftBack.setSpeed(motorSpeed+motorOffset);
rightBack.run(RELEASE);
rightFront.run(RELEASE);
leftBack.run(RELEASE);
leftFront.run(RELEASE);
}
void turnRight(int duration) //Set motors to turn right for the specified duration then stop
{
rightBack.setSpeed(motorSpeed+turnSpeed); //Set the motors to the motor speed
rightFront.setSpeed(motorSpeed+turnSpeed);
leftFront.setSpeed(motorSpeed+motorOffset+turnSpeed);
leftBack.setSpeed(motorSpeed+motorOffset+turnSpeed);
rightBack.run(BACKWARD);
rightFront.run(BACKWARD);
leftFront.run(FORWARD);
leftBack.run(FORWARD);
delay(duration);
rightBack.setSpeed(motorSpeed); //Set the motors to the motor speed
rightFront.setSpeed(motorSpeed);
leftFront.setSpeed(motorSpeed+motorOffset);
leftBack.setSpeed(motorSpeed+motorOffset);
rightBack.run(RELEASE);
rightFront.run(RELEASE);
leftFront.run(RELEASE);
leftBack.run(RELEASE);
}
int getDistance() //Measure the distance of an object
{
unsigned long pulseTime; //Create a variable to store the pulse travel time
int distance; //Create a variable to store the calculated distance
digitalWrite(trig. HIGH); //Generate a 10 microsecond pulse
delayMicroseconds(10);
digitalWrite(trig,LOW);
pulseTime = pulseIn(echo, HIGH, timeOut); //Measure the time for the pulse to return
distance = (float)pulseTime *340 / 2 / 10000; //Calculate the object distance based on the pulse time
return distance;
}
int checkDirection() //check the left and right directions and decide which way to turn
{
int distances [2] = {0,0}; //Left and right distances
int turnDir = 1; //Direaction to turn, 0 left, 1 reverse, 2 right
servoLook.write(180); //Turn servo to look left
delay(500);
distances [0] = getDistance(); //Get the left object distance
servoLook.write(0); //Turn servo to look right
delay(1000);
distances [1] = getDistance(); //Get the right object distance
if (distances[0]>=200 && distances[1]>=200) //If both directions are clear, turn left
turnDir = 0;
else if (distances[0]<=stopDist && distances[1]<=stopDist) //If both directions are blocked, turn around
turnDir = 1;
else if (distances[0]>=distances[1]) //If left has more space, turn left
turnDir = 0;
else if (distances[0]<distances[1]) //If right has more space, turn right
turnDir = 2;
return turnDir;
}
The first semicolon is after a comment... ooh... just tried it again (after failing)... it will compile. : /
I can't wait for the "one line" self balancing robot.