Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EDISON-SCIENCE-CORNER/7a46fa4b099ac8045a2263e0e893dc8c to your computer and use it in GitHub Desktop.
Save EDISON-SCIENCE-CORNER/7a46fa4b099ac8045a2263e0e893dc8c to your computer and use it in GitHub Desktop.
dfrvrm.ino
#include "DFRobot_DF2301Q.h"
#define whiteLed 8
#define blueLed 9
//I2C communication
DFRobot_DF2301Q_I2C asr;
void setup() {
Serial.begin(115200);
pinMode(whiteLed, OUTPUT); //Init LED pin to output mode
pinMode(blueLed, OUTPUT);
// Init the sensor
while (!(asr.begin())) {
Serial.println("Communication with device failed, please check connection");
delay(3000);
}
Serial.println("Begin ok!");
/**
* @brief Set voice volume
* @param voc - Volume value(1~7)
*/
asr.setVolume(7);
/**
@brief Set mute mode
@param mode - Mute mode; set value 1: mute, 0: unmute
*/
asr.setMuteMode(0);
/**
@brief Set wake-up duration
@param wakeTime - Wake-up duration (0-255)
*/
asr.setWakeTime(255);
/**
@brief Get wake-up duration
@return The currently-set wake-up period
*/
uint8_t wakeTime = 0;
wakeTime = asr.getWakeTime();
Serial.print("wakeTime = ");
Serial.println(wakeTime);
// asr.playByCMDID(1); // Wake-up command
/**
@brief Play the corresponding reply audio according to the ID
@param CMDID - command word ID
*/
//asr.playByCMDID(23); // Command word ID
}
void loop() {
/**
@brief Get the ID corresponding to the command word
@return Return the obtained command word ID, returning 0 means no valid ID is obtained
*/
uint8_t CMDID = asr.getCMDID();
switch (CMDID) {
case 5: //If the command is “Turn on the light”
digitalWrite(blueLed, HIGH); //Turn on the LED
Serial.println("received'Turn on blue led',command flag'5'"); //Serial transmits "received"Turn on the light",command flag"103
break;
case 6: //If the command is “Turn off the light”
digitalWrite(blueLed, LOW); //Turn off the LED
Serial.println("received'Turn off blue led',command flag'6'"); //The serial transmits "received"Turn off the light",command flag"104""
break;
case 7: //If the command is “Turn on the light”
digitalWrite(whiteLed, HIGH); //Turn on the LED
Serial.println("received'Turn on white led',command flag'7'"); //Serial transmits "received"Turn on the light",command flag"103
break;
case 8: //If the command is “Turn off the light”
digitalWrite(whiteLed, LOW); //Turn off the LED
Serial.println("received'Turn off white led ',command flag'8'"); //The serial transmits "received"Turn off the light",command flag"104""
break;
default:
if (CMDID != 0) {
Serial.print("CMDID = "); //Printing command ID
Serial.println(CMDID);
}
}
delay(300);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment