1
2
3#include <SoftwareSerial.h>
4#include "VoiceRecognitionV3.h"
5
6
12VR myVR(2, 3);
13
14uint8_t records[7];
15uint8_t buf[64];
16
17int lock = 6;
18int unlock = 7;
19
20#define onRecord (0)
21#define offRecord (1)
22
23void printSignature(uint8_t *buf, int len)
24{
25 int i;
26 for (i = 0; i < len; i++) {
27 if (buf[i] > 0x19 && buf[i] < 0x7F) {
28 Serial.write(buf[i]);
29 }
30 else {
31 Serial.print("[");
32 Serial.print(buf[i], HEX);
33 Serial.print("]");
34 }
35 }
36}
37
38
39void printVR(uint8_t *buf)
40{
41 Serial.println("VR Index\ Group\ RecordNum\ Signature");
42
43 Serial.print(buf[2], DEC);
44 Serial.print("\ \ ");
45
46 if (buf[0] == 0xFF) {
47 Serial.print("NONE");
48 }
49 else if (buf[0] & 0x80) {
50 Serial.print("UG ");
51 Serial.print(buf[0] & (~0x80), DEC);
52 }
53 else {
54 Serial.print("SG ");
55 Serial.print(buf[0], DEC);
56 }
57 Serial.print("\ ");
58
59 Serial.print(buf[1], DEC);
60 Serial.print("\ \ ");
61 if (buf[3] > 0) {
62 printSignature(buf + 4, buf[3]);
63 }
64 else {
65 Serial.print("NONE");
66 }
67 Serial.println("\
\
68");
69}
70
71void setup()
72{
73
74 myVR.begin(9600);
75
76 Serial.begin(115200);
77 Serial.println("Elechouse Voice Recognition V3 Module\
\
78Control LED sample");
79
80 pinMode(lock, OUTPUT);
81 pinMode(unlock, OUTPUT);
82
83 if (myVR.clear() == 0) {
84 Serial.println("Recognizer cleared.");
85 } else {
86 Serial.println("Not find VoiceRecognitionModule.");
87 Serial.println("Please check connection and restart Arduino.");
88 while (1);
89 }
90
91 if (myVR.load((uint8_t)onRecord) >= 0) {
92 Serial.println("onRecord loaded");
93 }
94
95 if (myVR.load((uint8_t)offRecord) >= 0) {
96 Serial.println("offRecord loaded");
97 }
98}
99
100void loop()
101{
102 int ret;
103 ret = myVR.recognize(buf, 50);
104 if (ret > 0) {
105 switch (buf[1]) {
106 case onRecord:
107
108 digitalWrite(lock, HIGH);
109 delay(500);
110 digitalWrite(lock, LOW);
111 break;
112 case offRecord:
113
114 digitalWrite(unlock, HIGH);
115 delay(500);
116 digitalWrite(unlock, LOW);
117 break;
118 default:
119 Serial.println("Record function undefined");
120 break;
121 }
122
123 printVR(buf);
124 }
125}
126
joeflyeraloha
2 years ago
Can the voice recognition module respond to a progression of piano notes to unlock? Or does it have to be voice?