-
Notifications
You must be signed in to change notification settings - Fork 0
/
Voice Controlled Vehicle
131 lines (123 loc) · 3.15 KB
/
Voice Controlled Vehicle
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
//Motor A
const int motorPin1 = 5;
const int motorPin2 = 6;
//Motor B
const int motorPin3 = 10;
const int motorPin4 = 9;
// For distance
int trigPin = 12;
int echoPin = 11;
void setup() {
Serial.begin(9600);
// pin 0 and 1 == Rx and Tx resp, are connected with Tx and Rx of Bolt.
pinMode(2,INPUT); //pin connected to BOLT frwd -> P1
pinMode(4,INPUT); //pin connected to BOLT bkwd -> P2
pinMode(7,INPUT); //pin connected to BOLT lft -> P3
pinMode(8,INPUT); //pin connected to BOLT ryt -> P4
pinMode(3,INPUT); // to stop Mickey!
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
pinMode(trigPin, OUTPUT); //for dist
pinMode(echoPin, INPUT);
}
float calcdist()
{
float duration, distance;
digitalWrite(12, LOW);
delayMicroseconds(2);
digitalWrite(12, HIGH);
delayMicroseconds(10);
digitalWrite(12, LOW);
duration = pulseIn(11, HIGH);
distance = (duration*.0343)/2;
Serial.print("Distance: ");
Serial.println(distance);
delay(100);
return distance;
}
void loop() {
int left, right, forward, backward,Stop;
float dist;
dist = calcdist();
if(dist>20)
{
forward = digitalRead(2); //p1
backward = digitalRead(4); //p2
left = digitalRead(7); //p3
right = digitalRead(8); //p4
Stop = digitalRead(3); //p0
Serial.print("\t forward:");
Serial.print(forward);
Serial.print("\t backward:");
Serial.print(backward);
Serial.print("\t left:");
Serial.print(left);
Serial.print("\t right:");
Serial.print(right);
Serial.print("\t Stop:");
Serial.print(Stop);
if(forward == 1) //p1
{
backward = digitalRead(4);
left = digitalRead(7);
right = digitalRead(8);
Stop = digitalRead(3);
digitalWrite(motorPin1, HIGH); //5
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH); //10
digitalWrite(motorPin4, LOW);
}
if(backward == 1) //p2
{
forward = digitalRead(2);
left = digitalRead(7);
right = digitalRead(8);
Stop = digitalRead(3);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
}
if(left == 1) //p3
{
backward = digitalRead(4);
forward = digitalRead(2);
right = digitalRead(8);
Stop = digitalRead(3);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
}
if(right == 1) //p4
{
backward = digitalRead(4);
forward = digitalRead(2);
left = digitalRead(7);
Stop = digitalRead(3);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
}
if(Stop == 1)
{
forward = digitalRead(2);
backward = digitalRead(4);
left = digitalRead(7);
right = digitalRead(8);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
}
}
else
{ digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
}
}