Devices & Components
Arduino Uno Rev3
HC-05 Bluetooth Module
Arduino Compatible Stepper Motor Controller Module
Hardware & Tools
10 Pc. Jumper Wire Kit, 5 cm Long
9V Battery
Wheel
DC motor
Battery snap
Project description
Code
voiceCar
arduino
1/* Voice Controlled Lego Car 2 * Created by Hanis Rozali 3 */ 4 5String voice; 6 7//motor A 8int ENA = 10; 9int IN1 = 9; 10int IN2 = 8; 11// motor B 12int ENB = 5; 13int IN3 = 7; 14int IN4 = 6; 15 16void setup() { 17 // put your setup code here, to run once: 18 Serial.begin(9600); 19 pinMode(ENA, OUTPUT); 20 pinMode(ENB, OUTPUT); 21 pinMode(IN1, OUTPUT); 22 pinMode(IN2, OUTPUT); 23 pinMode(IN3, OUTPUT); 24 pinMode(IN4, OUTPUT); 25} 26 27// put your main code here, to run repeatedly: 28void loop() { 29 while (Serial.available()){ //Check if there is an available byte to read 30 delay(10); //Delay added to make thing stable 31 char words = Serial.read(); //Conduct a serial read 32 if (words == '#') {break;} //Exit the loop when the # is detected after the word 33 voice += words; //Shorthand for voice = voice + words 34 35 } 36 37 if (voice.length() > 0){ 38 Serial.print(voice); 39 40 if (voice == "*go" || voice == "*now") { 41 forward(); 42 }else if(voice == "*back" || voice == "*move backward") { 43 backward(); 44 }else if (voice == "*turn right") { 45 right(); 46 }else if (voice == "*turn left") { 47 left(); 48 }else if (voice == "*move faster") { 49 faster(); 50 }else if (voice == "*start") { 51 start(); 52 }else if (voice == "*one" || voice == "*1") { 53 one(); 54 }else if (voice == "*three" || voice == "*3") { 55 three(); 56 }else if (voice == "*five" || voice == "*5") { 57 five(); 58 }else if (voice == "*zero" || voice == "*stop") { 59 Stop(); 60 } 61 62 voice=""; //Reset the variable after initiating 63} 64} 65void forward(){ 66 digitalWrite(IN1, HIGH); 67 digitalWrite(IN2, LOW); 68 analogWrite(ENA, 180); 69 digitalWrite(IN3, LOW); 70 digitalWrite(IN4, HIGH); 71 analogWrite(ENB, 180); 72 delay(5000); 73 digitalWrite(IN1, LOW); 74 digitalWrite(IN2, LOW); 75 digitalWrite(IN3, LOW); 76 digitalWrite(IN4, LOW); 77 78} 79 80void backward() { 81 digitalWrite(IN1, LOW); 82 digitalWrite(IN2, HIGH); 83 digitalWrite(IN3, HIGH); 84 digitalWrite(IN4, LOW); 85 delay(5000); 86 digitalWrite(IN1, LOW); 87 digitalWrite(IN2, LOW); 88 digitalWrite(IN3, LOW); 89 digitalWrite(IN4, LOW); 90} 91 92void right() { 93 digitalWrite(IN1, LOW); 94 digitalWrite(IN2, HIGH); 95 digitalWrite(IN3, LOW); 96 digitalWrite(IN4, HIGH); 97 delay(500); 98 forward(); 99} 100 101void left() { 102 digitalWrite(IN1, HIGH); 103 digitalWrite(IN2, LOW); 104 digitalWrite(IN3, HIGH); 105 digitalWrite(IN4, LOW); 106 delay(500); 107 forward(); 108} 109 110void start(){ 111 digitalWrite(IN1, HIGH); 112 digitalWrite(IN2, LOW); 113 analogWrite(ENA, 50); 114 digitalWrite(IN3, LOW); 115 digitalWrite(IN4, HIGH); 116 analogWrite(ENB, 50); 117} 118 119void one() { 120 analogWrite(ENA, 100); 121 analogWrite(ENB, 100); 122} 123 124void three() { 125 analogWrite(ENA, 180); 126 analogWrite(ENB, 180); 127} 128void five() { 129 analogWrite(ENA, 225); 130 analogWrite(ENB, 225); 131} 132 133void faster() { 134 analogWrite(ENA, 210); 135 analogWrite(ENB, 210); 136} 137 138void Stop() { 139 digitalWrite(IN1, LOW); 140 digitalWrite(IN2, LOW); 141 digitalWrite(IN3, LOW); 142 digitalWrite(IN4, LOW); 143}
Downloadable files
VoiceCar Sch
Schematic Diagram of the Voice-Controlled Lego Car
VoiceCar Sch
VoiceCar Sch
Schematic Diagram of the Voice-Controlled Lego Car
VoiceCar Sch
Comments
Only logged in users can leave comments