Smart and Autonomous parking.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

278 lines
4.4 KiB

5 years ago
/*
* Athors: Oulis Evnagelos, Oulis Nikolaos, Katsibras Drosos
* Ultrasonic Sensor HC-SR04 and Arduino
* Dual Stepper Motor Shiled 1.1
*
*/
/*
* Include all libraries.
5 years ago
*
5 years ago
*/
5 years ago
/*
* Include the Arduino Servo Library
*/
#include <Servo.h>
/*
* Include the Ping Ulptrasonic Library
*/
#include <NewPing.h>
5 years ago
/*
* -------------------------------------------------------
*/
/*
* All Definitions.
*/
5 years ago
#define ULTRASONIC_TIMEOUT 100000
#define SERVO_SPEED 6
#define MAX_DISTANCE 200
5 years ago
/*
* -------------------------------------------------------
*/
5 years ago
/*
* Define Ultrasonic Pins
*/
5 years ago
const int trigPin = 12;
5 years ago
const int echoPin = 11;
5 years ago
/*
* For Motor Driver Pis.
*/
const int in1 = 2;
const int in2 = 4;
const int in3 = 7;
const int in4 = 8;
const int rightSpeed = 5;
const int leftSpeed = 3;
/*
5 years ago
* For Servo Motor Pins
5 years ago
*/
5 years ago
const int servoPin = 6;
5 years ago
5 years ago
/*
* Define Servo Data Struct
*/
Servo myServo;
5 years ago
5 years ago
/*
* Define Ultrasonic Struct
*/
NewPing sonar1 (trigPin, echoPin, MAX_DISTANCE);
5 years ago
5 years ago
/*
* Define all useful variables
*/
5 years ago
long duration;
5 years ago
float distance, distanceCm;
int pos, dir;
5 years ago
/*
5 years ago
* Turn Servo using custom Speed
*/
void turnServo(int from, int deg){
int d;
if (from > deg){
for(d=from; d>=deg; d--){
myServo.write(d);
delay(SERVO_SPEED);
}
} else{
for(d=from; d<=deg; d++){
myServo.write(d);
delay(SERVO_SPEED);
}
}
5 years ago
}
5 years ago
/*
* Setup Wiring Components
*/
5 years ago
void setup() {
5 years ago
//pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
//pinMode(echoPin, INPUT); // Sets the echoPin as an Input
5 years ago
//pinMode(response, OUTPUT); // Sets to led aoutput.
pinMode(in1, OUTPUT); //Motor Driver 1st Pin
pinMode(in2, OUTPUT); //Motor Driver 2nd Pin
pinMode(in3, OUTPUT); //Motor Driver 3rd Pin
pinMode(in4, OUTPUT); //Motor Driver 4th Pin
pinMode(rightSpeed, OUTPUT);
pinMode(leftSpeed, OUTPUT);
5 years ago
//Initialize Servo Motor
myServo.attach(servoPin);
pos = 1;
dir = 1;
//Initialize car direction and make walk
absolute();
forward();
5 years ago
Serial.begin(9600); // Starts the serial communication
5 years ago
// Clears the trigPin
analogWrite(trigPin, LOW);
delayMicroseconds(2);
5 years ago
}
/*
5 years ago
* Stepper Function for Servo.
* 1st Step: straight
* 2nd Step: left
* 3rd Step: straight
* 4th Step: right
5 years ago
*/
void next_step()
{
if(pos == 1)
5 years ago
{ //Servo move to right
turnServo(90, 30);
5 years ago
pos = 2;
}
else if(pos ==2)
5 years ago
{ //Servo move to center
turnServo(30, 90);
5 years ago
pos = 3;
}
else if(pos == 3)
5 years ago
{ //Servo move to left
turnServo(90, 150);
5 years ago
pos = 4;
}
else if(pos == 4)
5 years ago
{ //Servo move to center
turnServo(150, 90);
5 years ago
pos = 1;
}
}
/*
* Distance meter via Ultrasonic
*/
5 years ago
float getDistance(int echoPin, int trigPin){
do{
int uS1 = sonar1.ping();
distanceCm = uS1 / US_ROUNDTRIP_CM;
} while(distanceCm == 0);
5 years ago
return distanceCm;
}
/*
5 years ago
* Left turn walk
5 years ago
*/
void turnLeft(){
analogWrite(leftSpeed, 255);
analogWrite(rightSpeed, 0);
}
5 years ago
/*
* Right turn walk
*/
5 years ago
void turnRight(){
analogWrite(leftSpeed, 0);
analogWrite(rightSpeed, 255);
}
5 years ago
/*
* Straight walk
*/
5 years ago
void absolute(){
analogWrite(leftSpeed, 255);
analogWrite(rightSpeed, 255);
}
5 years ago
/*
* Backward walk
*/
5 years ago
void backward(){
// Set Motor A backward
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Set Motor B backward
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
5 years ago
/*
* Forward walk
*/
5 years ago
void forward(){
// Set Motor A forward
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// Set Motor B forward
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}
5 years ago
/*
* Stop motors
*/
5 years ago
void motor_stop(){
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void turn(){
if (pos == 1){
absolute();
5 years ago
} else if (pos == 2){
turnLeft();
5 years ago
} else if (pos == 3){
absolute();
5 years ago
} else if (pos == 4){
turnRight();
}
}
void moveCar(){
if (pos == 3){
if (dir == 1){
turnRight();
backward();
dir = 2;
} else{
turnLeft();
backward();
dir = 1;
}
delay(1500);
forward();
} else{
forward();
5 years ago
}
}
void loop() {
// Calculating the distance
distance= getDistance(echoPin, trigPin);
// Prints the distance on the Serial Monitor
5 years ago
Serial.print("Distance: ");
Serial.println(distance);
5 years ago
if (distance < 6){
5 years ago
motor_stop();
5 years ago
// Set Ultrasonic to next step
next_step();
// Change direction to motors
5 years ago
turn();
5 years ago
// Move car
moveCar();
delay(1000);
5 years ago
}
5 years ago
delay(100);
5 years ago
}