-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutonomous Rover.ino
147 lines (130 loc) · 4.47 KB
/
Autonomous Rover.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <Servo.h>
//#include <SoftwareSerial.h>
#include "Ultrasonic.h"
//#include "DHT.h"
//#define dht_pin=A0
//dht DHT;
//Constants - Connections
const int motorA1= 10;
const int motorA2= 11;
//const int enableA = 3;
const int motorB1= 12;
const int motorB2= 13;
//SoftwareSerial mySerial(0,1);//RX,TX
//const int enableB = 7;
Ultrasonic ultrasonic(A2 ,A3); //Create Ultrsonic object ultrasonic(trig pin,echo pin)
Servo myservo; //Create Servo object to control a servo
//Variables
int distance; //Variable to store distance from an object
int checkRight;
int checkLeft;
int pos=100; //Variable to store the servo position. By default 90 degrees - sensor will 'look' forward
//int speedPWM = 255; //Change speed (PWM max 255)
void setup()
{
Serial.begin(9600);
myservo.attach(6); //Servo pin connected to pin 7
myservo.write(pos); // tell servo to go to position in variable 'pos'
pinMode(3,OUTPUT);
pinMode(motorA1,OUTPUT);
pinMode(motorA2,OUTPUT);
pinMode(motorB1,OUTPUT);
pinMode(motorB2,OUTPUT);
//pinMode(enableA, OUTPUT);
//pinMode(enableB, OUTPUT);
delay(5000); //Wait 5 seconds...
distance = ultrasonic.Ranging(CM);
}
void loop(){
//analogWrite(enableA, speedPWM);
//analogWrite(enableB, speedPWM);
//Read distance...
//while(mySerial.available())
//{
//char state = mySerial.read();
//if(state=='1')
//{
distance = ultrasonic.Ranging(CM); //Tip: Use 'CM' for centimeters or 'INC' for inches
delay(20);
//Check for objects...
if (ultrasonic.Ranging(CM) > 15){
forward();
Serial.print("Voyage started");
Serial.print("Object is at:");
Serial.println(distance);
}
else if (ultrasonic.Ranging(CM) <=15){
stop();
Serial.print("Object is at: ");
Serial.println(distance);//Object detected! Stop the robot and check left and right for the better way out!
delay(500);
Serial.println("Sensing the area...");
for(pos = 30; pos < 170; pos += 1){ //goes from 0 degrees to 180 degrees
myservo.write(pos);
//digitalWrite(3,HIGH);//tell servo to go to position in variable 'pos'
delay(10); //waits 10ms for the servo to reach the position
}
checkLeft = ultrasonic.Ranging(CM); //Take distance from the left side
for(pos = 170; pos>=30; pos-=1){ //goes from 180 degrees to 0 degrees
myservo.write(pos);
//digitalWrite(3,HIGH);//tell servo to go to position in variable 'pos'
delay(10); //waits 10ms for the servo to reach the position
}
checkRight= ultrasonic.Ranging(CM);
myservo.write(100); // Sensor "look" forward again
//Finally, take the right decision, turn left or right?
if (checkLeft < checkRight){
left();
//digitalWrite(3,HIGH);
//delay(500);
//digitalWrite(3,LOW);// delay, change value if necessary to make robot turn.
}
else if (checkLeft > checkRight){
right();
// digitalWrite(3,HIGH);
//delay(500);
//digitalWrite(3,LOW); // delay, change value if necessary to make robot turn.
}
else if (checkLeft <=10 && checkRight <=10){
backward(); //The road is closed... go back and then left ;)
left();
}
}
delay(150);
}
void forward(){
digitalWrite(motorA1, HIGH);
digitalWrite(motorA2, LOW);
digitalWrite(motorB1, HIGH);
digitalWrite(motorB2, LOW);
//digitalWrite(3,HIGH);
//delay(500);
//digitalWrite(3,LOW);
}
void backward(){
digitalWrite(motorA1, LOW);
digitalWrite(motorA2, HIGH);
digitalWrite(motorB1, LOW);
digitalWrite(motorB2, HIGH);
}
void left(){
digitalWrite(motorA1, HIGH);
digitalWrite(motorA2, LOW);
digitalWrite(motorB1, LOW);
digitalWrite(motorB2, HIGH);
}
void right(){
digitalWrite(motorA1, LOW);
digitalWrite(motorA2, HIGH);
digitalWrite(motorB1, HIGH);
digitalWrite(motorB2, LOW);
}
void stop(){
digitalWrite(motorA1, LOW);
digitalWrite(motorA2, LOW);
digitalWrite(motorB1, LOW);
digitalWrite(motorB2, LOW);
//digitalWrite(3,HIGH);
//delay(200);
//digitalWrite(3,LOW);
}