BTW...DFRobot voice module code is blocking. After a few runs through the loop, if you are doing anything more serious than turning on or off a relay, the module locks up. I have read through the DFRobot voice library looking for a way to modify it, and also tried many different non-blocking codes with fade LED and servo motions, but it still locks up while trying to read the DFRobot in either UART or I2C (works fine on it's own as shown in my first post code) . So, ONE simple working answer to this is the following. I use two MCUs. I read the DFRobot voice unit with UART. I transmit the trained switch case integer via I2C to the second MCU. This works without issue and of course, the voice module code never locks up. Here are my codes in case anyone wants to use them. I am using arduino Wire and x2 Teensy 4.0s...
This is the voice side with DF module, and it 'sends' via Wire..
#include "DFRobot_DF2301Q.h"
#include <Wire.h>
#define TESTLED 13
const int LED_RIGHT_EYE = 4; //teensy pins
const int LED_LEFT_EYE = 3;
//voice sercom 'voice' variable
int v = 0;
//UART serial1
DFRobot_DF2301Q_UART asr(/*hardSerial =*/&Serial1);
void setup() {
Serial.begin(115200);
Serial1.begin(9600);//teensy serial1 0,1
pinMode(TESTLED, OUTPUT); //Init LED pin to output mode
digitalWrite(TESTLED, HIGH); //Set LED pin to low
delay(6000); //must wait for DF2301Q_I2C boot!!
asr.begin(); //EDIT..moved begin to after DF2301Q boot delay
asr.settingCMD(DF2301Q_UART_MSG_CMD_SET_MUTE, 0); //1: mute, 0: unmute
asr.settingCMD(DF2301Q_UART_MSG_CMD_SET_VOLUME, 3); //Volume range(1-7)
asr.settingCMD(DF2301Q_UART_MSG_CMD_SET_WAKE_TIME, 20); //Wake-up duration(0-255)
//asr.settingCMD(DF2301Q_UART_MSG_CMD_SET_ENTERWAKEUP, 0);//0 = wake state
Wire.begin(); //send from this teensy to Wire.read(1);
Wire.setClock(100000L);
//Wire.setSDA(18);
//Wire.setSCL(19);
delay(40);
digitalWrite(TESTLED, LOW);
}
void loop() {
uint8_t CMDID = asr.getCMDID();
switch (CMDID) {
case 1: //the first command is to 'WAKE'
v = 1;
Wire.beginTransmission(1);
Wire.write(v);
Wire.endTransmission();
digitalWrite(TESTLED, HIGH);
Serial.println("HEY WALL-E");
Serial.print("v = "); //Printing command ID
Serial.println(v);
break;
case 5: //the first custom command begins at '5'
v = 5;
Wire.beginTransmission(1);
Wire.write(v);
Wire.endTransmission();
digitalWrite(TESTLED, HIGH);
Serial.println("received 'LED ON',command flag '5'");
break;
case 6:
v = 6;
Wire.beginTransmission(1);
Wire.write(v);
Wire.endTransmission();
digitalWrite(TESTLED, LOW);
Serial.println("received 'LED OFF',command flag '6'");
break;
case 7:
v = 7;
Wire.beginTransmission(1);
Wire.write(v);
Wire.endTransmission();
break;
case 103: //If the command is “Turn on the light”
v = 103; //Turn on the LED
Wire.beginTransmission(1);
Wire.write(v);
Wire.endTransmission();
digitalWrite(TESTLED, HIGH);
Serial.println("received'Turn on the light',command flag'103'"); //Serial transmits "received"Turn on the light",command flag"103
break;
case 104:
v = 104;
Wire.beginTransmission(1);
Wire.write(v);
Wire.endTransmission();
digitalWrite(TESTLED, LOW);
Serial.println("received'Turn off the light',command flag'104'"); //The serial transmits "received"Turn off the light",command flag"104""
break;
default: //when voice recog is not active
if (CMDID != 0) {
v = 0;
Serial.print("CMDID = "); //Printing command ID
Serial.println(CMDID);
}
}
delay(300);
}
this is the 'receive' side...which will run more code based on Wire reads...
#include <Wire.h>
#define TESTLED 13
//voice sercom 'voice' variable
int v = 0;//do nothing at '0'
void receiveEvent(int bytes) {
v = Wire.read();
Serial.print("INCOMING WIRE READ INT: ");
Serial.println(v);
}
void setup() {
Serial.begin(115200);
pinMode(TESTLED, OUTPUT); //Init LED pin to output mode
digitalWrite(TESTLED, HIGH); //Set LED pin to low
delay(6000); //must wait for DF2301Q_I2C boot!!
Wire.begin(1);
Wire.setClock(100000L);
//Wire.setSDA(18);
//Wire.setSCL(19);
Wire.onReceive(receiveEvent);
delay(40);
digitalWrite(TESTLED, LOW);
}
void loop() {
switch (v) {
case 1: //the first command is to 'WAKE'
digitalWrite(TESTLED, HIGH);
Serial.println("RECEIVE from I2C: HEY WALL-E");
v = 0;
break;
case 5: //the first custom command begins at '5'
digitalWrite(TESTLED, HIGH);
//Serial.println("RECEIVE from I2C: 'LED ON',command flag '5'");
v = 0;
break;
case 6:
digitalWrite(TESTLED, LOW);
// Serial.println("RECEIVE from I2C: 'LED OFF',command flag '6'");
v = 0;
break;
case 7:
v = 0;
break;
case 103:
digitalWrite(TESTLED, HIGH);
//Serial.println("received'Turn on the light',command flag'103'");
v = 0;
break;
case 104:
digitalWrite(TESTLED, LOW);
//Serial.println("received'Turn off the light',command flag'104'");
v = 0;
break;
}
}