//Button Input
int j = 1; // integer used in scanning the array designating column number
//2-dimensional array for asigning the buttons and their high and low values
int Button[16][3] = {{1, 310, 343}, // button 1
{2, 179, 218}, // button 2
{3, 85, 134}, // button 3
{4, 30, 84}, // button 4
{5, 418, 438}, // button 1 + button 2
{6, 371, 394}, // button 1 + button 3
{7, 344, 370}, // button 1 + button 4
{8, 256, 290}, // button 2 + button 3
{9, 219, 255}, // button 2 + button 4
{10, 135, 178}, // button 3 + button 4
{11, 931, 932}, //button 1 + button 2 + button 3
{12, 395, 417}, //button 1 + button 3 + button 4
{13, 291, 309}, //button 2 + button 3 + button 4
{14, 439, 457}, //button 1 + button 2 + button 4
{15, 478, 1000}, //all inputs
{16, 0, 29}, //no input
};
int analogpin = 0; // analog pin to read the buttons
int label = 0; // for reporting the button label
int counter = 0; // how many times we have seen new value
long time = 0; // the last time the output pin was sampled
int debounce_count = 100; // number of millis/samples to consider before declaring a debounced input
int current_state = 0; // the debounced input value
int current_label = 0; //the debounced input label
int ButtonVal;
//Distance Sensor
int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
//Voice Recognition
/*!
* @file uart.ino
* @brief Control the voice recognition module via UART
* @n Get the recognized command ID and play the corresponding reply audio according to the ID;
* @n Set the wake-up state duration, set mute mode, set volume, enter the wake-up state, and reset the module
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @licence The MIT License (MIT)
* @author [qsjhyy](yihuan.huang@dfrobot.com)
* @version V1.0
* @date 2022-12-30
* @url https://github.com/DFRobot/DFRobot_DF2301Q
*/
#include "DFRobot_DF2301Q.h"
/**
* @brief DFRobot_URM13_RTU constructor
* @param serial - serial ports for communication, supporting hard and soft serial ports
* @param rx - UART The pin for receiving data
* @param tx - UART The pin for transmitting data
*/
#if (defined(ARDUINO_AVR_UNO) || defined(ESP8266)) // Use software serial
SoftwareSerial softSerial(/*rx =*/4, /*tx =*/5);
DFRobot_DF2301Q_UART DF2301Q(/*softSerial =*/&softSerial);
#elif defined(ESP32) // Use the hardware serial with remappable pin: Serial1
DFRobot_DF2301Q_UART DF2301Q(/*hardSerial =*/&Serial1, /*rx =*/D3, /*tx =*/D2);
#else // Use hardware serial: Serial1
DFRobot_DF2301Q_UART DF2301Q(/*hardSerial =*/&Serial1);
#endif
void setup()
{
//Distance Sensor
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(115200);
// Init the sensor
while( !( DF2301Q.begin() ) ) {
Serial.println("Communication with device failed, please check connection");
delay(3000);
}
Serial.println("Begin ok!");
/**
* @brief Reset the module
*/
// DF2301Q.resetModule();
/**
* @brief Set commands of the module
* @param setType - Set type
* @n DF2301Q_UART_MSG_CMD_SET_VOLUME : Set volume, the set value range 1-7
* @n DF2301Q_UART_MSG_CMD_SET_ENTERWAKEUP : Enter wake-up state; set value 0
* @n DF2301Q_UART_MSG_CMD_SET_MUTE : Mute mode; set value 1: mute, 0: unmute
* @n DF2301Q_UART_MSG_CMD_SET_WAKE_TIME : Wake-up duration; the set value range 0-255s
* @param setValue - Set value, refer to the set type above for the range
*/
// DF2301Q.settingCMD(DF2301Q_UART_MSG_CMD_SET_MUTE, 0);
// DF2301Q.settingCMD(DF2301Q_UART_MSG_CMD_SET_VOLUME, 5);
// DF2301Q.settingCMD(DF2301Q_UART_MSG_CMD_SET_WAKE_TIME, 20);
// DF2301Q.settingCMD(DF2301Q_UART_MSG_CMD_SET_ENTERWAKEUP, 0);
/**
* @brief Play the corresponding reply audio according to the command word ID
* @param CMDID - Command word ID
*/
DF2301Q.playByCMDID(23);
}
void loop()
{
//Button Input
// If we have gone on to the next millisecond
if (millis() != time)
{
// check analog pin for the button value and save it to ButtonVal
ButtonVal = analogRead(analogpin);
ButtonCheck();
if(label == current_label && counter >0)
{
counter--;
}
if(label != current_label)
{
counter++;
}
// If ButtonVal has shown the same value for long enough let's switch it
if (counter >= debounce_count)
{
counter = 0;
current_label = label;
//Checks which button or button combo has been pressed
if (ButtonVal > 0)
{
Action();
}
}
time = millis();
}
//Distance Sensor
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH){
// we have just turned off
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
//Voice Recognition
/**
* @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 = 0;
CMDID = DF2301Q.getCMDID();
if(0 != CMDID) {
Serial.print("CMDID = ");
Serial.println(CMDID);
}
if (CMDID == 5){
Serial.println("Happy");
}
if (CMDID == 6){
Serial.println("Sad");
}
if (CMDID == 7){
Serial.println("Angry");
}
if (CMDID == 8){
Serial.println("Surprised");
}
if (CMDID == 9){
Serial.println("Ambivalent");
}
if (CMDID == 10){
Serial.println("Proud");
}
if (CMDID == 11){
Serial.println("Delightful");
}
if (CMDID == 12){
Serial.println("Irritated");
}
if (CMDID == 13){
Serial.println("Disappointed");
}
if (CMDID == 14){
Serial.println("Outraged");
}
if (CMDID == 15){
Serial.println("Morbid");
}
if (CMDID == 16){
Serial.println("Righteous");
}
if (CMDID == 17){
Serial.println("Grief");
}
if (CMDID == 18){
Serial.println("Shocked");
}
if (CMDID == 19){
Serial.println("Overwhelmed");
}
delay(2000);
}
void ButtonCheck()
{
// loop for scanning the button array.
for(int i = 0; i <= 16; i++)
{
// checks the ButtonVal against the high and low vales in the array
if(ButtonVal >= Button[i][j] && ButtonVal <= Button[i][j+1])
{
// stores the button number to a variable
label = Button[i][0];
//current_label = label;
}
}
}
void Action()
{
if(label == 1)
{
Serial.println("Happy");
}
if(label == 2)
{
Serial.println("Sad");
}
if(label == 3)
{
Serial.println("Angry");
}
if(label == 4)
{
Serial.println("Suprised");
}
if(label == 5)
{
Serial.println("Ambivalent");
}
if(label == 6)
{
Serial.println("Prideful");
}
if(label == 7)
{
Serial.println("Delightful");
}
if(label == 8)
{
Serial.println("Irritated");
}
if(label == 9)
{
Serial.println("Disappointed");
}
if(label == 10)
{
Serial.println("Outraged");
}
if(label == 11)
{
Serial.println("Morbid");
}
if(label == 12)
{
Serial.println("Righteous");
}
if(label == 13)
{
Serial.println("Grief");
}
if(label == 14)
{
Serial.println("Shock");
}
if(label == 15)
{
Serial.println("Overwhelmed");
}
if(label == 16)
{
Serial.println("No Input");
}
}
This is my code for the project; the goal is to create some automated systems for a therapeutic children's toy called a "Worry Monster".