Components and supplies
3.7 volt Batteries
Electric Switches
Wheels
Gear Motors
Battery Holder
12 volt LED
Voice Recognition V3 Module
Jumper wires (generic)
Arduino UNO
Wires
Screws and Nuts
L298n Motor Driver
Project description
Code
Code
arduino
1 2 3#include <SoftwareSerial.h> 4#include "VoiceRecognitionV3.h" 5 6/** 7 Connection 8 Arduino VoiceRecognitionModule 9 2 -------> TX 10 3 -------> RX 11*/ 12VR myVR(2, 3); // 2:RX 3:TX, you can choose your favourite pins. 13 14uint8_t records[7]; // save record 15uint8_t buf[64]; 16 17/* define L298N motor drive control pins */ 18int RightMotorForward = 4; // IN1 19int RightMotorBackward = 5; // IN2 20int LeftMotorForward = 6; // IN3 21int LeftMotorBackward = 7; // IN4 22 23#define left (0) 24#define right (1) 25#define forword (2) 26#define backword (3) 27#define stopp (4) 28 29void printSignature(uint8_t *buf, int len) 30{ 31 int i; 32 for (i = 0; i < len; i++) { 33 if (buf[i] > 0x19 && buf[i] < 0x7F) { 34 Serial.write(buf[i]); 35 } 36 else { 37 Serial.print("["); 38 Serial.print(buf[i], HEX); 39 Serial.print("]"); 40 } 41 } 42} 43 44 45void printVR(uint8_t *buf) 46{ 47 Serial.println("VR Index\ Group\ RecordNum\ Signature"); 48 49 Serial.print(buf[2], DEC); 50 Serial.print("\ \ "); 51 52 if (buf[0] == 0xFF) { 53 Serial.print("NONE"); 54 } 55 else if (buf[0] & 0x80) { 56 Serial.print("UG "); 57 Serial.print(buf[0] & (~0x80), DEC); 58 } 59 else { 60 Serial.print("SG "); 61 Serial.print(buf[0], DEC); 62 } 63 Serial.print("\ "); 64 65 Serial.print(buf[1], DEC); 66 Serial.print("\ \ "); 67 if (buf[3] > 0) { 68 printSignature(buf + 4, buf[3]); 69 } 70 else { 71 Serial.print("NONE"); 72 } 73 Serial.println("\ \ 74"); 75} 76 77void setup() 78{ 79 /** initialize */ 80 myVR.begin(9600); 81 82 Serial.begin(115200); 83 Serial.println("Voice Control car"); 84 85 /* initialize motor control pins as output */ 86 pinMode(LeftMotorForward,OUTPUT); 87 pinMode(RightMotorForward,OUTPUT); 88 pinMode(LeftMotorBackward,OUTPUT); 89 pinMode(RightMotorBackward,OUTPUT); 90 91 if (myVR.clear() == 0) { 92 Serial.println("Recognizer cleared."); 93 } else { 94 Serial.println("Not find VoiceRecognitionModule."); 95 Serial.println("Please check connection and restart Arduino."); 96 while (1); 97 } 98 99 if (myVR.load((uint8_t)left) >= 0) { 100 Serial.println("left loaded"); 101 } 102 103 if (myVR.load((uint8_t)right) >= 0) { 104 Serial.println("right loaded"); 105 } 106 if (myVR.load((uint8_t)forword) >= 0) { 107 Serial.println("forword loaded"); 108 } 109 110 if (myVR.load((uint8_t)backword) >= 0) { 111 Serial.println("backword loaded"); 112 } 113 114 if (myVR.load((uint8_t)stopp) >= 0) { 115 Serial.println("backword loaded"); 116 } 117} 118 119void loop() 120{ 121 int ret; 122 ret = myVR.recognize(buf, 50); 123 if (ret > 0) { 124 switch (buf[1]) { 125 case left: 126 TurnLeft(); 127 delay(500); 128 MotorStop(); 129 break; 130 131 case right: 132 TurnRight(); 133 delay(500); 134 MotorStop(); 135 break; 136 137 case forword: 138 MotorForward(); 139 delay(500); 140 MotorStop(); 141 break; 142 143 case backword: 144 MotorBackward(); 145 delay(500); 146 MotorStop(); 147 break; 148 149 default: 150 Serial.println("Record function undefined"); 151 break; 152 } 153 /** voice recognized */ 154 printVR(buf); 155 } 156} 157 158///* define L298N motor drive control pins */ 159//int RightMotorForward = 4; // IN1 160//int RightMotorBackward = 5; // IN2 161//int LeftMotorForward = 6; // IN3 162//int LeftMotorBackward = 7; // IN4 163 164/* FORWARD */ 165void MotorForward() { 166 167 digitalWrite(RightMotorForward, HIGH); 168 digitalWrite(RightMotorBackward, LOW); 169 digitalWrite(LeftMotorForward, HIGH); 170 digitalWrite(LeftMotorBackward, LOW); 171 172 173 174} 175 176/* BACKWARD */ 177void MotorBackward() { 178 179 digitalWrite(RightMotorForward, LOW); 180 digitalWrite(RightMotorBackward, HIGH); 181 digitalWrite(LeftMotorForward, LOW); 182 digitalWrite(LeftMotorBackward, HIGH); 183 184// digitalWrite(LeftMotorBackward, HIGH); 185// digitalWrite(RightMotorBackward, HIGH); 186// digitalWrite(LeftMotorForward, LOW); 187// digitalWrite(RightMotorForward, LOW); 188} 189 190/* TURN RIGHT */ 191void TurnRight() { 192 digitalWrite(RightMotorForward, LOW); 193 digitalWrite(RightMotorBackward, HIGH); 194 digitalWrite(LeftMotorForward, HIGH); 195 digitalWrite(LeftMotorBackward, LOW); 196// 197// digitalWrite(LeftMotorForward, HIGH); 198// digitalWrite(RightMotorForward, HIGH); 199// digitalWrite(LeftMotorBackward, LOW); 200// digitalWrite(RightMotorBackward, LOW); 201 202} 203 204/* TURN LEFT */ 205void TurnLeft() { 206 207 digitalWrite(RightMotorForward, HIGH); 208 digitalWrite(RightMotorBackward, LOW); 209 digitalWrite(LeftMotorForward, LOW); 210 digitalWrite(LeftMotorBackward, HIGH); 211 212// digitalWrite(RightMotorForward, HIGH); 213// digitalWrite(LeftMotorForward, LOW); 214// digitalWrite(LeftMotorBackward, HIGH); 215// digitalWrite(RightMotorBackward, LOW); 216 217 218 219} 220 221/* STOP */ 222void MotorStop() { 223 224 digitalWrite(LeftMotorBackward, LOW); 225 digitalWrite(RightMotorBackward, LOW); 226 digitalWrite(LeftMotorForward, LOW); 227 digitalWrite(RightMotorForward, LOW); 228} 229
Comments
Only logged in users can leave comments
munir03125344286
16 Followers
•60 Projects
1
0