The Antisocial RobotCar moves away from you if you get too close and stops if there is nothing close.
Step 1: Overview
We wanted to build an antisocial robot car that moves away from obstacles/people.
The way it works is by having 2 Ultra sonic sensors in each end of the car. The car can only move forward and backwards.
If anything get to close to the car <50cm away="" drive="" is="" it="" obstacle="" of="" out="" p="" range.="" the="" until="" will="">It uses 2 DC motors controlled by a L298 DC dual motor controller to move both forwards and backwards.
To power the DC motors we use 3 LIthium batteries cabable to deliver approx 11V to the motors.
Everything is controlled by the Arduino Mega 2560 which all of the components is connected to.
50cm>
The way it works is by having 2 Ultra sonic sensors in each end of the car. The car can only move forward and backwards.
If anything get to close to the car <50cm away="" drive="" is="" it="" obstacle="" of="" out="" p="" range.="" the="" until="" will="">It uses 2 DC motors controlled by a L298 DC dual motor controller to move both forwards and backwards.
To power the DC motors we use 3 LIthium batteries cabable to deliver approx 11V to the motors.
Everything is controlled by the Arduino Mega 2560 which all of the components is connected to.
50cm>
Step 2: Part List
2 X DC Motor, 4 to 24V
1 X Battery Case 3 battery slots
3 X 3,7V Battery
1 X L298N DC Motordriver
2 X Ultrasonic modul hc-SR04
1 X Arduino mega 2560
1 X 9V Battery Cable for Arduino
1 X 9V Battery
3 X Wheels.
1 X Battery Case 3 battery slots
3 X 3,7V Battery
1 X L298N DC Motordriver
2 X Ultrasonic modul hc-SR04
1 X Arduino mega 2560
1 X 9V Battery Cable for Arduino
1 X 9V Battery
3 X Wheels.
Step 3: Building the Car
Start
by getting a large piece of plastic board and make holes for you to
strap on the components. After having drawn up the holes and made them,
attach them to the board.
Use the wiring diagram to connet the components corretly together.
Remeber to make shared ground and wire up the motor driver to the Arduino correctly. If not connected correct, it might not drive or it might drive in the wrong direction.
Remember to secure the connection between the Ultrasonic moduls to the Arduino correctly, or you might have some instable readings.
Use the wiring diagram to connet the components corretly together.
Remeber to make shared ground and wire up the motor driver to the Arduino correctly. If not connected correct, it might not drive or it might drive in the wrong direction.
Remember to secure the connection between the Ultrasonic moduls to the Arduino correctly, or you might have some instable readings.
Step 5: The Code
[code]
//Keyboard Controls: // // 1 -Motor 1 Left // 2 -Motor 1 Stop // 3 -Motor 1 Right // // 4 -Motor 2 Left // 5 -Motor 2 Stop // 6 -Motor 2 Right
// Declare L298N Dual H-Bridge Motor Controller directly since there is not a library to load.
// Motor 1 int dir1PinA = 9; int dir2PinA = 8; int speedPinA = 10; // Needs to be a PWM pin to be able to control motor speed
// Motor 2 int dir1PinB = 5; int dir2PinB = 4; int speedPinB = 7; // Needs to be a PWM pin to be able to control motor speed
int inByteRight; int inByteLeft;
// Ultra-Sound-Sensor 1
#define echoPin1 2 #define trigPin1 3
long duration1, distanceFront;
// Ultra-Sound-Sensor 2
#define echoPin2 11 #define trigPin2 12
long duration2, distanceBack;
void setup() { // Setup runs once per reset
// initialize serial communication @ 9600 baud: Serial.begin(9600);
//Define L298N Dual H-Bridge Motor Controller Pins
pinMode(dir1PinA,OUTPUT); pinMode(dir2PinA,OUTPUT); pinMode(speedPinA,OUTPUT); pinMode(dir1PinB,OUTPUT); pinMode(dir2PinB,OUTPUT); pinMode(speedPinB,OUTPUT);
//Define Ultra-sound-sensor Pins
pinMode(trigPin1, OUTPUT); pinMode(trigPin2, OUTPUT); pinMode(echoPin1, INPUT); pinMode(echoPin2, INPUT);
}
void loop() {
// Ultra-sound-sensor 1 Loop
digitalWrite(trigPin1, LOW); delayMicroseconds(2); digitalWrite(trigPin1, HIGH); delayMicroseconds(10); digitalWrite(trigPin1, LOW); duration1 = pulseIn(echoPin1, HIGH); distanceFront = duration1/58.2;
Serial.print("DistanceFront in cm = "); Serial.println(distanceFront);
delay(10);
// Ultra-sound-sensor 2 Loop
digitalWrite(trigPin2, LOW); delayMicroseconds(2); digitalWrite(trigPin2, HIGH); delayMicroseconds(10); digitalWrite(trigPin2, LOW); duration2 = pulseIn(echoPin2, HIGH); distanceBack = duration2/58.2;
Serial.print("DistanceBack in cm = "); Serial.println(distanceBack);
delay(10);
//Moving Backwards....
if (distanceBack > 25 && distanceFront < 50) { inByteLeft=1; inByteRight=3; } else { Serial.println("Else"); }
//Moving Forwards....
if (distanceBack < 50 && distanceFront > 25) { inByteLeft=3; inByteRight=1; } else { Serial.println("Else"); }
//Stop....
if (distanceBack > 25 && distanceFront > 25) { inByteLeft=2; inByteRight=2; } else { Serial.println("Else"); }
//too close....
if (distanceBack < 25 && distanceFront < 25) { inByteLeft=2; inByteRight=2; } else { Serial.println("Else"); }
switch (inByteLeft) { //______________Motor 1______________
case 1: // Motor 1 Forward analogWrite(speedPinA, 255);//Sets speed variable via PWM digitalWrite(dir1PinA, LOW); digitalWrite(dir2PinA, HIGH); Serial.println("Motor 1 Forward"); // Prints out “Motor 1 Forward” on the serial monitor Serial.println(" "); // Creates a blank line printed on the serial monitor break;
case 2: // Motor 1 Stop (Freespin) analogWrite(speedPinA, 0); digitalWrite(dir1PinA, LOW); digitalWrite(dir2PinA, HIGH); Serial.println("Motor 1 Stop"); Serial.println(" "); break;
case 3: // Motor 1 Reverse analogWrite(speedPinA, 255); digitalWrite(dir1PinA, HIGH); digitalWrite(dir2PinA, LOW); Serial.println("Motor 1 Reverse"); Serial.println(" "); break;
default: // turn all the connections off if an unmapped key is pressed: Serial.println(" "); }
switch (inByteRight) { //______________Motor 2______________
case 1: // Motor 2 Forward analogWrite(speedPinB, 255); digitalWrite(dir1PinB, LOW); digitalWrite(dir2PinB, HIGH); Serial.println("Motor 2 Forward"); Serial.println(" "); break;
case 2: // Motor 2 Stop (Freespin) analogWrite(speedPinB, 0); digitalWrite(dir1PinB, LOW); digitalWrite(dir2PinB, HIGH); Serial.println("Motor 2 Stop"); Serial.println(" "); break;
case 3: // Motor 2 Reverse analogWrite(speedPinB, 255); digitalWrite(dir1PinB, HIGH); digitalWrite(dir2PinB, LOW); Serial.println("Motor 2 Reverse"); Serial.println(" "); break;
default: // turn all the connections off if an unmapped key is pressed: Serial.println(" "); } }
[/code]
//Keyboard Controls: // // 1 -Motor 1 Left // 2 -Motor 1 Stop // 3 -Motor 1 Right // // 4 -Motor 2 Left // 5 -Motor 2 Stop // 6 -Motor 2 Right
// Declare L298N Dual H-Bridge Motor Controller directly since there is not a library to load.
// Motor 1 int dir1PinA = 9; int dir2PinA = 8; int speedPinA = 10; // Needs to be a PWM pin to be able to control motor speed
// Motor 2 int dir1PinB = 5; int dir2PinB = 4; int speedPinB = 7; // Needs to be a PWM pin to be able to control motor speed
int inByteRight; int inByteLeft;
// Ultra-Sound-Sensor 1
#define echoPin1 2 #define trigPin1 3
long duration1, distanceFront;
// Ultra-Sound-Sensor 2
#define echoPin2 11 #define trigPin2 12
long duration2, distanceBack;
void setup() { // Setup runs once per reset
// initialize serial communication @ 9600 baud: Serial.begin(9600);
//Define L298N Dual H-Bridge Motor Controller Pins
pinMode(dir1PinA,OUTPUT); pinMode(dir2PinA,OUTPUT); pinMode(speedPinA,OUTPUT); pinMode(dir1PinB,OUTPUT); pinMode(dir2PinB,OUTPUT); pinMode(speedPinB,OUTPUT);
//Define Ultra-sound-sensor Pins
pinMode(trigPin1, OUTPUT); pinMode(trigPin2, OUTPUT); pinMode(echoPin1, INPUT); pinMode(echoPin2, INPUT);
}
void loop() {
// Ultra-sound-sensor 1 Loop
digitalWrite(trigPin1, LOW); delayMicroseconds(2); digitalWrite(trigPin1, HIGH); delayMicroseconds(10); digitalWrite(trigPin1, LOW); duration1 = pulseIn(echoPin1, HIGH); distanceFront = duration1/58.2;
Serial.print("DistanceFront in cm = "); Serial.println(distanceFront);
delay(10);
// Ultra-sound-sensor 2 Loop
digitalWrite(trigPin2, LOW); delayMicroseconds(2); digitalWrite(trigPin2, HIGH); delayMicroseconds(10); digitalWrite(trigPin2, LOW); duration2 = pulseIn(echoPin2, HIGH); distanceBack = duration2/58.2;
Serial.print("DistanceBack in cm = "); Serial.println(distanceBack);
delay(10);
//Moving Backwards....
if (distanceBack > 25 && distanceFront < 50) { inByteLeft=1; inByteRight=3; } else { Serial.println("Else"); }
//Moving Forwards....
if (distanceBack < 50 && distanceFront > 25) { inByteLeft=3; inByteRight=1; } else { Serial.println("Else"); }
//Stop....
if (distanceBack > 25 && distanceFront > 25) { inByteLeft=2; inByteRight=2; } else { Serial.println("Else"); }
//too close....
if (distanceBack < 25 && distanceFront < 25) { inByteLeft=2; inByteRight=2; } else { Serial.println("Else"); }
switch (inByteLeft) { //______________Motor 1______________
case 1: // Motor 1 Forward analogWrite(speedPinA, 255);//Sets speed variable via PWM digitalWrite(dir1PinA, LOW); digitalWrite(dir2PinA, HIGH); Serial.println("Motor 1 Forward"); // Prints out “Motor 1 Forward” on the serial monitor Serial.println(" "); // Creates a blank line printed on the serial monitor break;
case 2: // Motor 1 Stop (Freespin) analogWrite(speedPinA, 0); digitalWrite(dir1PinA, LOW); digitalWrite(dir2PinA, HIGH); Serial.println("Motor 1 Stop"); Serial.println(" "); break;
case 3: // Motor 1 Reverse analogWrite(speedPinA, 255); digitalWrite(dir1PinA, HIGH); digitalWrite(dir2PinA, LOW); Serial.println("Motor 1 Reverse"); Serial.println(" "); break;
default: // turn all the connections off if an unmapped key is pressed: Serial.println(" "); }
switch (inByteRight) { //______________Motor 2______________
case 1: // Motor 2 Forward analogWrite(speedPinB, 255); digitalWrite(dir1PinB, LOW); digitalWrite(dir2PinB, HIGH); Serial.println("Motor 2 Forward"); Serial.println(" "); break;
case 2: // Motor 2 Stop (Freespin) analogWrite(speedPinB, 0); digitalWrite(dir1PinB, LOW); digitalWrite(dir2PinB, HIGH); Serial.println("Motor 2 Stop"); Serial.println(" "); break;
case 3: // Motor 2 Reverse analogWrite(speedPinB, 255); digitalWrite(dir1PinB, HIGH); digitalWrite(dir2PinB, LOW); Serial.println("Motor 2 Reverse"); Serial.println(" "); break;
default: // turn all the connections off if an unmapped key is pressed: Serial.println(" "); } }
[/code]
Step 7: I/O List for the Arduino
Digital Outputs
Motor A
dir1PinA = 9;
dir2PinA = 8;
speedPinA = 10'
Motor B
dir1PinB = 5;
dir2PinB = 4;
speedPinB = 7;
Ultrasonic Front
trigPin1 = 3
Ultrasonic Back
trigPin2 = 12
Digital Inputs
Ultrasonic Front
echoPin1 = 2
Ultrasonic Back
echoPin2 = 11
Power
5v to ultrasonic
2x GND shared
Motor A
dir1PinA = 9;
dir2PinA = 8;
speedPinA = 10'
Motor B
dir1PinB = 5;
dir2PinB = 4;
speedPinB = 7;
Ultrasonic Front
trigPin1 = 3
Ultrasonic Back
trigPin2 = 12
Digital Inputs
Ultrasonic Front
echoPin1 = 2
Ultrasonic Back
echoPin2 = 11
Power
5v to ultrasonic
2x GND shared
No comments:
Post a Comment