-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoice_controlled_car.ino
106 lines (94 loc) Β· 2.34 KB
/
voice_controlled_car.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
//viral science
//voice controlled car
#include <AFMotor.h> //Adafruit Motor Shield Library. First you must download and install AFMotor library
#include <Servo.h> //Servo library. This is standard library. (Sketch -> Include Library -> Servo)
String voice;
AF_DCMotor motor1 (1, MOTOR12_1KHZ); //create motor #1 using M1 output on Motor Drive Shield, set to 1kHz PWM frequency
AF_DCMotor motor2 (2, MOTOR12_1KHZ); //create motor #2 using M2 output on Motor Drive Shield, set to 1kHz PWM frequency
Servo myServo; //define servo name
void setup()
{
Serial.begin(9600); //start serial communication
myServo.attach(10); //define our servo pin (the motor shield servo1 input = digital pin 10)
myServo.write(90); //servo position is 90 degrees
}
void loop()
{
while (Serial.available()){ //Check if there is an available byte to read
delay(10); //Delay added to make thing stable
char c = Serial.read(); //Conduct a serial read
if (c == '#') {break;} //Exit the loop when the # is detected after the word
voice += c; //Shorthand for voice = voice + c
}
if (voice.length() > 0){
if(voice == "*go ahead"){
forward_car();
}
else if(voice == "*go back"){
back_car();
}
else if(voice == "*right") {
right_car();
}
else if(voice == "*left") {
left_car();
}
else if(voice == "*stop") {
stop_car();
}
voice=""; //Reset the variable after initiating
}
}
void forward_car()
{
motor1.run(FORWARD);
motor1.setSpeed(700);
motor2.run(FORWARD);
motor2.setSpeed(700);
delay(2000);
motor1.run(RELEASE);
motor2.run(RELEASE);
}
void back_car()
{
motor1.run(BACKWARD);
motor1.setSpeed(700);
motor2.run(BACKWARD);
motor2.setSpeed(700);
delay(2000);
motor1.run(RELEASE);
motor2.run(RELEASE);
}
void right_car()
{
myServo.write(0);
delay(1000);
myServo.write(90);
delay(1000);
motor1.run(FORWARD);
motor1.setSpeed(190);
motor2.run(BACKWARD);
motor2.setSpeed(190);
delay(1000);
motor1.run(RELEASE);
motor2.run(RELEASE);
}
void left_car()
{
myServo.write(180);
delay(1000);
myServo.write(90);
delay(1000);
motor1.run(BACKWARD);
motor1.setSpeed(190);
motor2.run(FORWARD);
motor2.setSpeed(190);
delay(1000);
motor1.run(RELEASE);
motor2.run(RELEASE);
}
void stop_car ()
{
motor1.run(RELEASE);
motor2.run(RELEASE);
}