Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- #include <TimerOne.h>
- #include "VoiceRecognitionV3.h"
- //#include <DFPlayer_Mini_Mp3.h>
- #define ButPin 7
- //SoftwareSerial mySerial ( 5 , 4);
- VR myVR(3, 2); // 2:RX 3:TX, you can choose your favourite pins.
- uint8_t records[7];
- uint8_t buf[64];
- #define forwardRecord (0)
- #define backRecord (1)
- #define leftRecord (2)
- #define rightRecord (3)
- #define stopRecord (4)
- /**
- @brief Print signature, if the character is invisible,
- print hexible value instead.
- @param buf --> command length
- len --> number of parameters
- */
- void forward() {
- pressbutton(1, 300);
- }
- void back() {
- pressbutton(2, 300);
- }
- void left() {
- pressbutton(3, 300);
- }
- void right() {
- pressbutton(4, 300);
- }
- void stope() {
- pressbutton(5, 300);
- }
- void printSignature(uint8_t *buf, int len)
- {
- int i;
- for (i = 0; i < len; i++) {
- if (buf[i] > 0x19 && buf[i] < 0x7F) {
- Serial.write(buf[i]);
- } else {
- Serial.print("[");
- Serial.print(buf[i], HEX);
- Serial.print("]");
- }
- }
- }
- void pressbutton(int count, int del) // count-количество нажатий del- пауза между нажатиями
- {
- for (int i = 0; i < (count + 1); i++)
- {
- digitalWrite(ButPin, LOW);
- delay(del);
- digitalWrite(ButPin, HIGH);
- delay(del);
- }
- }
- /**
- @brief Print signature, if the character is invisible,
- print hexible value instead.
- @param buf --> VR module return value when voice is recognized.
- buf[0] --> Group mode(FF: None Group, 0x8n: User, 0x0n:System
- buf[1] --> number of record which is recognized.
- buf[2] --> Recognizer index(position) value of the recognized record.
- buf[3] --> Signature length
- buf[4]~buf[n] --> Signature
- */
- void printVR(uint8_t *buf)
- {
- Serial.println("VR Index\tGroup\tRecordNum\tSignature");
- Serial.print(buf[2], DEC);
- Serial.print("\t\t");
- if (buf[0] == 0xFF) {
- Serial.print("NONE");
- } else if (buf[0] & 0x80) {
- Serial.print("UG ");
- Serial.print(buf[0] & (~0x80), DEC);
- } else {
- Serial.print("SG ");
- Serial.print(buf[0], DEC);
- }
- Serial.print("\t");
- Serial.print(buf[1], DEC);
- Serial.print("\t\t");
- if (buf[3] > 0) {
- printSignature(buf + 4, buf[3]);
- } else {
- Serial.print("NONE");
- }
- Serial.println("\r\n");
- }
- void setup() {
- pinMode(ButPin, OUTPUT);
- myVR.begin(9600);
- Serial.begin(115200);
- Serial.println("Elechouse Voice Recognition V3 Module\r\nControl LED sample");
- if (myVR.clear() == 0) {
- Serial.println("Recognizer cleared.");
- } else {
- Serial.println("Not find VoiceRecognitionModule.");
- Serial.println("Please check connection and restart Arduino.");
- while (1);
- }
- if (myVR.load((uint8_t)forwardRecord ) >= 0) {
- Serial.println("forwardRecord loaded");
- }
- if (myVR.load((uint8_t)backRecord ) >= 0) {
- Serial.println("backRecord loaded");
- }
- if (myVR.load((uint8_t)leftRecord) >= 0) {
- Serial.println("leftkRecord loaded");
- }
- if (myVR.load((uint8_t)rightRecord ) >= 0) {
- Serial.println("rightRecord loaded");
- }
- if (myVR.load((uint8_t)stopRecord ) >= 0) {
- Serial.println("stopRecord loaded");
- }
- // Timer1.initialize(100000);
- // delay(100);
- // mp3_play (1);
- // delay(1000);
- }
- void loop()
- {
- int ret;
- ret = myVR.recognize(buf, 50);
- if (ret > 0) {
- switch (buf[1]) {
- case forwardRecord :
- forward();
- Timer1.detachInterrupt();
- break;
- case backRecord :
- back();
- Timer1.detachInterrupt();
- break;
- case leftRecord:
- left();
- Timer1.detachInterrupt();
- break;
- case rightRecord:
- right();
- Timer1.detachInterrupt();
- break;
- case stopRecord:
- stope();
- Timer1.detachInterrupt();
- break;
- default:
- Timer1.detachInterrupt();
- Serial.println("Record function undefined");
- break;
- }
- printVR(buf);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement