Advertisement
Guest User

YARIK LOH

a guest
Apr 28th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. #include <TimerOne.h>
  3. #include "VoiceRecognitionV3.h"
  4. //#include <DFPlayer_Mini_Mp3.h>
  5. #define ButPin 7
  6.  
  7.  
  8. //SoftwareSerial mySerial ( 5 , 4);
  9.  
  10. VR myVR(3, 2); // 2:RX 3:TX, you can choose your favourite pins.
  11. uint8_t records[7];
  12. uint8_t buf[64];
  13. #define forwardRecord (0)
  14. #define backRecord (1)
  15. #define leftRecord (2)
  16. #define rightRecord (3)
  17. #define stopRecord (4)
  18. /**
  19. @brief Print signature, if the character is invisible,
  20. print hexible value instead.
  21. @param buf --> command length
  22. len --> number of parameters
  23. */
  24.  
  25.  
  26.  
  27. void forward() {
  28. pressbutton(1, 300);
  29.  
  30. }
  31. void back() {
  32. pressbutton(2, 300);
  33.  
  34. }
  35. void left() {
  36. pressbutton(3, 300);
  37.  
  38. }
  39. void right() {
  40. pressbutton(4, 300);
  41.  
  42. }
  43. void stope() {
  44. pressbutton(5, 300);
  45.  
  46.  
  47. }
  48.  
  49. void printSignature(uint8_t *buf, int len)
  50. {
  51. int i;
  52. for (i = 0; i < len; i++) {
  53. if (buf[i] > 0x19 && buf[i] < 0x7F) {
  54. Serial.write(buf[i]);
  55. } else {
  56. Serial.print("[");
  57. Serial.print(buf[i], HEX);
  58. Serial.print("]");
  59. }
  60. }
  61. }
  62.  
  63. void pressbutton(int count, int del) // count-количество нажатий del- пауза между нажатиями
  64. {
  65. for (int i = 0; i < (count + 1); i++)
  66. {
  67. digitalWrite(ButPin, LOW);
  68. delay(del);
  69. digitalWrite(ButPin, HIGH);
  70. delay(del);
  71. }
  72. }
  73.  
  74. /**
  75. @brief Print signature, if the character is invisible,
  76. print hexible value instead.
  77. @param buf --> VR module return value when voice is recognized.
  78. buf[0] --> Group mode(FF: None Group, 0x8n: User, 0x0n:System
  79. buf[1] --> number of record which is recognized.
  80. buf[2] --> Recognizer index(position) value of the recognized record.
  81. buf[3] --> Signature length
  82. buf[4]~buf[n] --> Signature
  83. */
  84. void printVR(uint8_t *buf)
  85. {
  86. Serial.println("VR Index\tGroup\tRecordNum\tSignature");
  87. Serial.print(buf[2], DEC);
  88. Serial.print("\t\t");
  89. if (buf[0] == 0xFF) {
  90. Serial.print("NONE");
  91. } else if (buf[0] & 0x80) {
  92. Serial.print("UG ");
  93. Serial.print(buf[0] & (~0x80), DEC);
  94. } else {
  95. Serial.print("SG ");
  96. Serial.print(buf[0], DEC);
  97. }
  98. Serial.print("\t");
  99. Serial.print(buf[1], DEC);
  100. Serial.print("\t\t");
  101. if (buf[3] > 0) {
  102. printSignature(buf + 4, buf[3]);
  103. } else {
  104. Serial.print("NONE");
  105. }
  106. Serial.println("\r\n");
  107. }
  108. void setup() {
  109. pinMode(ButPin, OUTPUT);
  110. myVR.begin(9600);
  111. Serial.begin(115200);
  112. Serial.println("Elechouse Voice Recognition V3 Module\r\nControl LED sample");
  113. if (myVR.clear() == 0) {
  114. Serial.println("Recognizer cleared.");
  115. } else {
  116. Serial.println("Not find VoiceRecognitionModule.");
  117. Serial.println("Please check connection and restart Arduino.");
  118. while (1);
  119. }
  120. if (myVR.load((uint8_t)forwardRecord ) >= 0) {
  121. Serial.println("forwardRecord loaded");
  122. }
  123. if (myVR.load((uint8_t)backRecord ) >= 0) {
  124. Serial.println("backRecord loaded");
  125. }
  126. if (myVR.load((uint8_t)leftRecord) >= 0) {
  127. Serial.println("leftkRecord loaded");
  128. }
  129. if (myVR.load((uint8_t)rightRecord ) >= 0) {
  130. Serial.println("rightRecord loaded");
  131. }
  132. if (myVR.load((uint8_t)stopRecord ) >= 0) {
  133. Serial.println("stopRecord loaded");
  134. }
  135.  
  136. // Timer1.initialize(100000);
  137. // delay(100);
  138. // mp3_play (1);
  139. // delay(1000);
  140. }
  141.  
  142.  
  143. void loop()
  144. {
  145.  
  146. int ret;
  147. ret = myVR.recognize(buf, 50);
  148. if (ret > 0) {
  149. switch (buf[1]) {
  150. case forwardRecord :
  151. forward();
  152. Timer1.detachInterrupt();
  153. break;
  154. case backRecord :
  155. back();
  156. Timer1.detachInterrupt();
  157. break;
  158. case leftRecord:
  159. left();
  160. Timer1.detachInterrupt();
  161. break;
  162. case rightRecord:
  163. right();
  164. Timer1.detachInterrupt();
  165. break;
  166. case stopRecord:
  167. stope();
  168. Timer1.detachInterrupt();
  169. break;
  170. default:
  171. Timer1.detachInterrupt();
  172. Serial.println("Record function undefined");
  173. break;
  174. }
  175. printVR(buf);
  176. }
  177.  
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement