“语音情景灯/zh”的版本间的差异

来自Microduino Wikipedia
跳转至: 导航搜索
手机APP
程序说明
第192行: 第192行:
 
*主函数
 
*主函数
 
<source lang="cpp">
 
<source lang="cpp">
#include "Micromenu_user.h" 
+
#include "Arduino.h"
#include <Servo.h> 
+
#include "audio.h"
#include <Adafruit_NeoPixel.h> 
+
#include "JQ6500.h"
//EEPROM--------------------- 
+
#include "JQ6500_def.h"
#include <EEPROM.h>  
+
#include "def.h"
//用户自定义部分------------------------ 
+
#include <Wire.h>
#include <SoftwareSerial.h> 
+
#include <AM2321.h>
 
+
#include <Adafruit_NeoPixel.h>
void setup() 
+
//用户自定义部分------------------------
+
AM2321 am2321;
    SETUP_begin();     //该函数定义于 Micromenu_user.
+
//用户自定义部分------------------------
    //用户自定义部分======================== 
+
#define PIN A0
} 
+
#define NOTICE A2
 
+
Adafruit_NeoPixel strip = Adafruit_NeoPixel(6, PIN, NEO_GRB + NEO_KHZ800);
void loop() 
+
 
{ 
+
Adafruit_NeoPixel notice = Adafruit_NeoPixel(6, NOTICE, NEO_GRB + NEO_KHZ800);
    { 
+
uint32_t color[9] =
        LOOP_priority_1ST(); //该函数定义于 Micromenu_user.
+
{
    } 
+
  strip.Color(0, 0, 0), strip.Color(255, 0, 0), strip.Color(248, 141, 30), strip.Color(255, 255, 0), strip.Color(0, 255, 0), strip.Color(0, 127, 255), strip.Color(0, 0, 255), strip.Color(139, 0, 255), strip.Color(255, 255, 255)
} 
+
};
 +
//用户自定义部分------------------------
 +
int music_vol = 25;        //初始音量0~30
 +
int music_Mode = MODE_ONE_STOP;
 +
JQ6500 AUDIO(&VOICE);        //声明audio类
 +
//用户自定义部分------------------------
 +
int num = 0;
 +
boolean buffer_sta = false;
 +
boolean start = false;
 +
int brightness = 255;
 +
unsigned long timer_voice = millis();
 +
int flag_on = 0;
 +
 
 +
int c = 0;
 +
int DHTPIN = 8;
 +
int temperature;
 +
//func===================================
 +
int judge(String cmd);
 +
void writecommand();
 +
void colorWipe(uint32_t c);
 +
void dht11();
 +
void tempsound();
 +
//SETUP===================================
 +
void SETUP_begin()
 +
{
 +
  DEBUG.begin(9600);
 +
  VOICE.begin(9600);
 +
  VOICE.begin(9600);
 +
  pinMode(DHTPIN,INPUT);
 +
  //EEPROM---------------------
 +
  config = eeprom_READ();
 +
  music_Mode = config.EEPROM_music_mode;
 +
  music_vol = config.EEPROM_music_vol;
 +
  AUDIO.init(DEVICE_TF, music_Mode, music_vol);
 +
  delay(1000);
 +
//  AUDIO.choose(1);
 +
  AUDIO.pause();
 +
  strip.begin(); //初始化LED
 +
  notice.begin();
 +
  pinMode(6,OUTPUT);
 +
    notice.setPixelColor(0, strip.Color(0, 0, 0));
 +
    notice.show();
 +
  for (int i = 0; i < 6; i++) // 初始化所有灯都为灭
 +
  {
 +
    strip.setPixelColor(i, strip.Color(0, 0, 0));
 +
    strip.show();
 +
  }
 +
writecommand();    //语音模块配置
 +
}
 +
//LOOP====================================
 +
void LOOP_priority_1ST()
 +
{
 +
  if (millis() < timer_voice) {
 +
    timer_voice = millis();  //同步
 +
  }
 +
  if (millis() - timer_voice < 10000)  //从有配置指令声音输入开始计时
 +
  {
 +
    notice.setPixelColor(0,notice.Color(255, 255, 255));
 +
    notice.show();
 +
  }
 +
  else
 +
  {
 +
    notice.setPixelColor(0,notice.Color(0, 0, 0));
 +
    notice.show();
 +
    flag_on = 0;
 +
  }
 +
 
 +
  if (VOICE.available())
 +
  {
 +
    String cmd = "";
 +
    if (VOICE.available()) //如果从语音芯片接收到用户的语音输入数据
 +
    {
 +
      cmd += char(VOICE.read()); //读出串口内的内容
 +
      delay(10);
 +
      cmd += char(VOICE.read());
 +
      delay(10);
 +
    }
 +
    Serial.println(flag_on);
 +
    Serial.println(cmd);
 +
    if (cmd == "pp") //如果是语音开关指令对应的内容
 +
    {
 +
      DEBUG.println("Voice IN!");  //在串口提示 输入声音
 +
      AUDIO.choose(1);
 +
      delay(2000);
 +
      AUDIO.pause();
 +
      flag_on = 1;
 +
      notice.Color(0, 0, 0);
 +
      timer_voice = millis(); //记录当前的系统运行时间
 +
    }
 +
    if (flag_on)  //如果进入了语音输入状态
 +
    {
 +
      c = judge(cmd);
 +
      DEBUG.print("C is :");
 +
      DEBUG.println(c);
 +
      switch (c)  //根据不同的语音指令,选择对应的应答
 +
      {
 +
        case 1:          //名字
 +
          timer_voice = 0;
 +
          AUDIO.choose(4);
 +
          timer_voice = millis();
 +
          break;
 +
        case 2:        //年龄
 +
          timer_voice = 0;
 +
          AUDIO.choose(6);
 +
          timer_voice = millis();
 +
          break;
 +
        case 3:                      //讲故事
 +
          timer_voice = 0;
 +
          AUDIO.choose(3);
 +
          timer_voice = millis();
 +
          delay(9000);    //防止误触发打断
 +
          break;
 +
        case 4:                //背诗
 +
          timer_voice = 0;
 +
          AUDIO.choose(2);
 +
          delay(3000);
 +
          AUDIO.choose(5);
 +
          delay(10000);
 +
          timer_voice = millis();
 +
          break;
 +
        case 5:                      //天气如何
 +
          timer_voice = 0;
 +
          am2321.read();
 +
          temperature = am2321.temperature/10.0;
 +
          Serial.println(temperature);
 +
          if(temperature<20)AUDIO.choose(15);
 +
          if(temperature>=20&&temperature<27)AUDIO.choose(14);
 +
          if(temperature>=27)AUDIO.choose(16);
 +
          timer_voice = millis();
 +
          break;
 +
        case 6:                    //好黑
 +
          timer_voice = 0;
 +
          AUDIO.choose(9);
 +
          colorWipe(color[8]);
 +
          timer_voice = millis();
 +
          break;
 +
        case 7:                      //再见
 +
          timer_voice = 0;
 +
          AUDIO.choose(11);
 +
          timer_voice = millis();
 +
          break;
 +
        case 8:              //傻瓜
 +
          timer_voice = 0;
 +
          AUDIO.choose(12);
 +
          timer_voice = millis();
 +
          break;
 +
        case 9:                    //关灯
 +
          timer_voice = 0;
 +
          AUDIO.choose(10);        //好的
 +
          colorWipe(color[0]);
 +
          timer_voice = millis();
 +
          break;
 +
        case 10:                  //谢谢
 +
          timer_voice = 0;
 +
          AUDIO.choose(13);        //不客气
 +
          timer_voice = millis();
 +
          break;
 +
          case 11:                  //吹泡
 +
          timer_voice = 0;
 +
          digitalWrite(6,HIGH);
 +
          delay(8000);
 +
          digitalWrite(6,LOW);
 +
          timer_voice = millis();
 +
          break;
 +
        case 0:
 +
          timer_voice = 0;
 +
          AUDIO.pause();
 +
          timer_voice = millis();
 +
          break;
 +
      }
 +
      while (VOICE.available()){
 +
      VOICE.read();}
 +
    }
 +
  }
 +
}
 +
 
 +
 
 +
int judge(String cmd)
 +
{
 +
  int c;
 +
  if (cmd == mz)c = 1;
 +
  if (cmd == nl)c = 2;
 +
  if (cmd == gs)c = 3;
 +
  if (cmd == sg)c = 4;
 +
  if (cmd == wd)c = 5;
 +
  if (cmd == kd)c = 6;
 +
  if (cmd == zj)c = 7;
 +
  if (cmd == sm)c = 8;
 +
  if (cmd == gd)c = 9;
 +
  if (cmd == xx)c = 10;
 +
  if(cmd == cp)c = 11;
 +
  if (cmd == "pp") {
 +
    c = 0;
 +
  }
 +
  return c;
 +
}
 +
 
 +
void writecommand()
 +
{
 +
  VOICE.println("{d1}");
 +
  delay(200);
 +
  VOICE.println("{d1}");    //确认进入调试模式
 +
  delay(200);
 +
  VOICE.println("{c0}");      //清除已有指令
 +
  delay(200);
 +
  VOICE.println("{a0pao pao|s0pp}");  //开启语音指令
 +
  delay(200);
 +
  VOICE.println("{a0ni de ming zi|s0mz}"); //你的名字
 +
  delay(200);
 +
  VOICE.println("{a0ni de nian ling|s0nl}");  // 你的年龄
 +
  delay(200);
 +
  VOICE.println("{a0jiang gu shi|s0gs}");  //讲故事
 +
  delay(200);
 +
  VOICE.println("{a0bei shi|s0sg}"); //背诗
 +
  delay(200);
 +
  VOICE.println("{a0sha gua|s0sm}"); //傻瓜
 +
  delay(200);
 +
//  VOICE.println("{a0tian qi ru he|s0wd}"); //天气如何
 +
//  delay(200);
 +
  VOICE.println("{a0kai deng|s0kd}");  //开灯
 +
  delay(200);
 +
  VOICE.println("{a0guan deng|s0gd}");  //关灯
 +
  delay(200);
 +
  VOICE.println("{a0zai jian|s0zj}");  //再见
 +
  delay(200);
 +
  VOICE.println("{a0xie xie|s0xx}");  //谢谢
 +
  delay(200);
 +
  VOICE.println("{l0}");    //加载
 +
  delay(200);
 +
  VOICE.println("{d0}");    //退出调试
 +
  delay(200);
 +
}
 +
void colorWipe(uint32_t c) {
 +
  for (uint16_t i = 0; i < strip.numPixels(); i++)
 +
  {
 +
    strip.setPixelColor(i, c);
 +
    strip.show();
 +
  }
 +
}
 
</source>
 
</source>
*Micromenu_user.h
+
*def.h
 
<source lang="cpp">
 
<source lang="cpp">
#include "Arduino.h" 
+
#include "Arduino.h"
//用户自定义部分------------------------ 
+
 
#include "def.h" 
+
 
#include "led.h" 
+
#include <SoftwareSerial.h>
#include "eep.h" 
+
//定义蓝牙通信串口
//用户自定义部分------------------------ 
+
//SoftwareSerial mySerial(4, 5); // RX, TX
 
+
 
int num_cache = 0; 
+
//定义串口
//用户自定义部分------------------------ 
+
//#define YUYIN mySerial   
//#include <Servo.h> 
+
#define VOICE Serial1
//Servo myservo;   创建一个舵机对象 ,在本套件中没有涉及 
+
#define DEBUG Serial
 
+
 
int card_sta = 0;    //nothing,true,false 
+
String mz = "mz";
unsigned long door_time = 0; 
+
String nl = "nl";
 
+
String gs = "gs";
//用户自定义部分------------------------ 
+
String sg = "sg";
char pkj[128]; 
+
String wd = "wd";
int num = 0; 
+
String kd = "kd";
boolean buffer_sta = false; 
+
String zj = "zj";
boolean start = false; 
+
String sm = "sm";
 
+
String gd = "gd";
unsigned long timer_voice = millis(); 
+
String xx = "xx";
boolean voice_sta = false; 
+
String zl = "zl";
 
+
String cp = "cp";
//SETUP=================================== 
+
 
void SETUP_begin() 
+
int led_style=2;
+
int led_style_vol=2;
    pinMode(SDA, OUTPUT);// 语音输入指示灯的正极 
+
//int led_style_cache=0;
    pinMode(A7, OUTPUT); //语音输入指示灯的负极 
+
 
    DEBUG.begin(9600); 
+
boolean led_sta=true;
    VOICE.begin(9600); 
+
//定义亮度
    FROM.begin(9600);  //串口通信波特率为9600 
+
#define led_brightness  3
 
+
unsigned long led_time;
    //EEPROM--------------------- 
 
    eeprom_READ(); 
 
 
 
    /*servo 用A3口控制舵机,本套件不涉及 
 
    myservo.attach(A3); 
 
    myservo.write(20); 
 
    */ 
 
    //led 
 
    strip.begin();    //初始化LED 
 
    for (int i = 0; i < 6; i++) // 初始化所有灯都为灭 
 
    { 
 
        strip.setPixelColor(i, strip.Color(0, 0, 0)); 
 
    } 
 
    strip.show(); 
 
 
 
    analogWrite(A7, LOW); //将A7口设置为低电平,用作语音指示灯负极 
 
 
  
 
//LOOP==================================== 
 
void LOOP_priority_1ST() 
 
 
    if (FROM.available())  //如果串口收到蓝牙发送的数据 
 
    { 
 
        char c = FROM.read(); 
 
 
 
        if (c == '{')  //如果接收到的是左括号,说明蓝牙有数据发过来而且是接收的开始 
 
            buffer_sta = true; 
 
        if (c == '}')  //如果接收到的是右括号说明传输结束了 
 
        { 
 
            start = true;   //标志位置为true,开始后续的处理 
 
            buffer_sta = false; //蓝牙状态接收结束,标志位置为false 
 
        } 
 
 
 
        if (buffer_sta) //当蓝牙正在传数据时 
 
            pkj[num++] = c; //将收到的字符存入字符数组保存 
 
 
 
        delay(1); 
 
    } 
 
 
 
    if (start)  //如果接收到了蓝牙的配置信息就显示在串口监视器上 
 
    { 
 
        DEBUG.print("pkj:"); //在串口打印出接收到的内容 
 
        for (int a = 1; a < num; a++) 
 
            DEBUG.print(pkj[a]); //打印出接收到的所有字符 
 
        DEBUG.println(); //打印一个空格 
 
 
 
        VOICE.write("{"); //向语音芯片中写入接收到的配置数据 
 
        for (int a = 1; a < num; a++) 
 
            VOICE.write(pkj[a]); 
 
        VOICE.write("}"); 
 
        delay(10); 
 
 
 
        for (int a = 0; a < num; a++)//清空接收的信息 
 
            pkj[a] = NULL; 
 
        num = 0; 
 
 
 
        DEBUG.println("Change OK!"); 
 
        start = false; //显示完把状态设为false避免重复处理 
 
    } 
 
 
 
    //--------------------------- 
 
    if (millis() < timer_voice) 
 
    { 
 
        timer_voice = millis();    //若计时器重新计时,同步 
 
    } 
 
    if (millis() - timer_voice < 5000)  //从有配置指令声音输入开始计时 
 
    { 
 
        voice_sta = true;   //5秒内语音输入指示灯点亮 
 
    } 
 
    else 
 
    { 
 
        voice_sta = false;  //超过5秒指示灯关闭 
 
    } 
 
    digitalWrite(SDA, voice_sta);//通过控制SDA电平的高低开关语音指示灯 
 
 
 
    if (VOICE.available()) //如果从语音芯片接收到用户的语音输入数据 
 
    { 
 
        int c = VOICE.read(); //读出串口内的内容 
 
        //DEBUG.print(c); 
 
        if (c == 0xf1)  //如果是语音开关指令f1对应的内容 
 
        { 
 
            DEBUG.println("Voice IN!");  //在串口提示 输入声音 
 
            timer_voice = millis(); //记录当前的系统运行时间 
 
        } 
 
 
 
        if (voice_sta)   //如果进入了语音输入状态 
 
        { 
 
            switch (c)  //根据不同的语音指令,选择对应的应答 
 
            { 
 
            case 0xa1: 
 
                DEBUG.println("Door Open!"); 
 
                timer_voice = 0; 
 
                // myservo.write(90); 本套件不涉及舵机 
 
                break; 
 
            case 0xa2: 
 
                DEBUG.println("Door Close!"); 
 
                timer_voice = 0; 
 
                // myservo.write(20); 本套件不涉及舵机 
 
                break; 
 
            case 0xb1: //点亮6盏彩灯 
 
                DEBUG.println("Light ON!"); 
 
                timer_voice = 0; 
 
                led_sta = true; 
 
                break; 
 
            case 0xb2: //关闭6盏彩灯 
 
                DEBUG.println("Light OFF!"); 
 
                timer_voice = 0; 
 
                led_sta = false; 
 
                break; 
 
            case 0xc1: 
 
                DEBUG.println("Light TemperatureA!"); 
 
                timer_voice = 0; 
 
                eeprom_WRITE(); 
 
                led_style = 1;//选择模式,1是冷暖色调,2是不同单种颜色, 
 
                //3是彩虹循环 
 
                led_style_vol = 1;  //各种颜色模式0-7分别对应灭红澄黄绿青蓝紫 
 
                break; 
 
            case 0xc2: 
 
                DEBUG.println("Light TemperatureB!"); 
 
                timer_voice = 0; 
 
                eeprom_WRITE(); 
 
                led_style = 1; 
 
                led_style_vol = 2; 
 
                break; 
 
            case 0xd1: 
 
                DEBUG.println("Light Colorful1!"); 
 
                timer_voice = 0; 
 
                eeprom_WRITE(); 
 
                led_style = 2; 
 
                led_style_vol = 1; 
 
                break; 
 
            case 0xd2: 
 
                DEBUG.println("Light Colorful2!"); 
 
                timer_voice = 0; 
 
                eeprom_WRITE(); 
 
                led_style = 2; 
 
                led_style_vol = 2; 
 
                break; 
 
            case 0xd3: 
 
                DEBUG.println("Light Colorful3!"); 
 
                timer_voice = 0; 
 
                eeprom_WRITE(); 
 
                led_style = 2; 
 
                led_style_vol = 3; 
 
                break; 
 
            case 0xd4: 
 
                DEBUG.println("Light Colorful4!"); 
 
                timer_voice = 0; 
 
                eeprom_WRITE(); 
 
                led_style = 2; 
 
                led_style_vol = 4; 
 
                break; 
 
            case 0xd5: 
 
                DEBUG.println("Light Colorful5!"); 
 
                timer_voice = 0; 
 
                eeprom_WRITE(); 
 
                led_style = 2; 
 
                led_style_vol = 5; 
 
                break; 
 
            case 0xd6: 
 
                DEBUG.println("Light Colorful6!"); 
 
                timer_voice = 0; 
 
                eeprom_WRITE(); 
 
                led_style = 2; 
 
                led_style_vol = 6; 
 
                break; 
 
            case 0xd7: 
 
                DEBUG.println("Light Colorful7!"); 
 
                timer_voice = 0; 
 
                eeprom_WRITE(); 
 
                led_style = 2; 
 
                led_style_vol = 7; 
 
                break; 
 
            case 0xe1: 
 
                DEBUG.println("Light RainbowA!"); 
 
                timer_voice = 0; 
 
                eeprom_WRITE(); 
 
                led_style = 3; 
 
                led_style_vol = 1; 
 
                break; 
 
            case 0xe2: 
 
                DEBUG.println("Light RainbowB!"); 
 
                timer_voice = 0; 
 
                eeprom_WRITE(); 
 
                led_style = 3; 
 
                led_style_vol = 2; 
 
                break; 
 
            } 
 
        } 
 
    } 
 
 
 
    if (millis() < led_time) led_time = millis();//和之前同理,同步时间 
 
    if (millis() - led_time > 50) 
 
    { 
 
        led_time = millis(); 
 
 
 
        if (led_sta)  //如果是亮灯状态 
 
        { 
 
            switch (led_style)   //根据LED的类型决定亮的方式 
 
            { 
 
            case 1://冷暖色 
 
                colorWipe(color_t[led_style_vol]); 
 
                break; 
 
            case 2://单色 
 
                colorWipe(color[led_style_vol]); 
 
                break; 
 
            case 3://彩虹循环 
 
                rainbowCycle((led_style_vol - 1) * 3); 
 
                break; 
 
            } 
 
        } 
 
        else 
 
        { 
 
            colorWipe(color[0]);   //如果是关灯状态 
 
        } 
 
    } 
 
 
 
</source>
 
</source>
*def.h
+
*JQ6500.cpp
 +
<source lang="cpp">
 +
//
 +
// JQ6500 audio library for Arduino
 +
//
 +
 
 +
#include "JQ6500.h"
 +
#include "JQ6500_def.h"
 +
#include <SoftwareSerial.h>
 +
 
 +
JQ6500::JQ6500(SoftwareSerial *ser) {
 +
    common_init();
 +
audioSwSerial = ser;
 +
}
 +
 
 +
JQ6500::JQ6500(HardwareSerial *ser) {
 +
    common_init();
 +
audioHwSerial = ser;
 +
}
 +
 
 +
void JQ6500::common_init(void){
 +
audioSwSerial = NULL;
 +
audioHwSerial = NULL;
 +
 +
}
 +
 
 +
void JQ6500::begin(uint16_t baud){
 +
if(audioSwSerial) audioSwSerial->begin(baud);
 +
else   audioHwSerial->begin(baud);
 +
delay(10);
 +
}
 +
 
 +
void JQ6500::sendCommand(uint8_t cmd, uint8_t *buf, uint16_t len){
 +
sendBuffer[0] = STX;
 +
sendBuffer[1] = len+2;
 +
sendBuffer[2] = cmd;
 +
if(len > 0)
 +
memcpy(sendBuffer+3, buf, len);
 +
sendBuffer[len+3] = ETX;
 +
if(audioSwSerial)
 +
audioSwSerial->write(sendBuffer, 4+len);
 +
else
 +
audioHwSerial->write(sendBuffer, 4+len);
 +
delay(100);
 +
}
 +
 
 +
void JQ6500::next(){
 +
 +
sendCommand(CMD_NEXT, NULL, 0);
 +
}
 +
 
 +
void JQ6500::prev(){
 +
 +
sendCommand(CMD_PREV, NULL, 0);
 +
}
 +
 
 +
void JQ6500::choose(uint16_t num){
 +
cmdBuffer[0] = num>>8;
 +
cmdBuffer[1] = num&0x00FF;
 +
sendCommand(CMD_CHOOSE, cmdBuffer, 2);
 +
}
 +
 
 +
void JQ6500::volUp(){
 +
 +
sendCommand(CMD_UP, NULL, 0);
 +
}
 +
 
 +
void JQ6500::volDown(){
 +
 +
sendCommand(CMD_DOWN, NULL, 0);
 +
}
 +
 
 +
void JQ6500::volumn(uint8_t vol){
 +
cmdBuffer[0] = vol;
 +
sendCommand(CMD_VOL, cmdBuffer, 1);
 +
}
 +
 
 +
void JQ6500::eq(uint8_t eq){
 +
cmdBuffer[0] = eq;
 +
sendCommand(CMD_EQ, cmdBuffer, 1);
 +
}
 +
 
 +
void JQ6500::setDevice(uint8_t device){
 +
cmdBuffer[0] = device;
 +
sendCommand(CMD_DEVICE, cmdBuffer, 1);
 +
}
 +
 
 +
void JQ6500::sleep(){
 +
 +
sendCommand(CMD_SLEEP, NULL, 0);
 +
}
 +
 
 +
void JQ6500::reset(){
 +
 +
sendCommand(CMD_RESET, NULL, 0);
 +
}
 +
 
 +
void JQ6500::play(){
 +
 +
sendCommand(CMD_PLAY, NULL, 0);
 +
}
 +
 
 +
void JQ6500::pause(){
 +
 +
sendCommand(CMD_PAUSE, NULL, 0);
 +
}
 +
 
 +
void JQ6500::folder(uint8_t temp){
 +
cmdBuffer[0] = temp;
 +
sendCommand(CMD_FOLDER, cmdBuffer, 1);
 +
}
 +
 
 +
void JQ6500::setMode(uint8_t temp){
 +
cmdBuffer[0] = temp;
 +
sendCommand(CMD_MODE, cmdBuffer, 1);
 +
}
 +
 
 +
void JQ6500::chooseFile(uint8_t folder, uint8_t file){
 +
cmdBuffer[0] = folder;
 +
cmdBuffer[1] = file;
 +
sendCommand(CMD_FILE, cmdBuffer, 2);
 +
}
 +
 
 +
void JQ6500::init(uint8_t device, uint8_t mode, uint8_t vol){
 +
begin(9600);
 +
delay(300);
 +
reset();
 +
setDevice(device);
 +
setMode(mode);
 +
volumn(vol);
 +
}
 +
 
 +
 
 +
uint16_t JQ6500::dataParse(){
 +
uint16_t sum = 0;
 +
uint8_t temp;
 +
if(audioSwSerial){
 +
while(audioSwSerial->available()){
 +
temp = char(audioSwSerial->read());
 +
if(temp>47&&temp<58){
 +
temp -=48; 
 +
}else if(temp>96&&temp<103){
 +
temp -=87; 
 +
}
 +
sum = sum*16+temp;
 +
}
 +
}else{
 +
while(audioHwSerial->available()){
 +
temp = char(audioHwSerial->read());
 +
if(temp>47&&temp<58){
 +
temp -=48; 
 +
}else if(temp>96&&temp<103){
 +
temp -=87; 
 +
}
 +
sum = sum*16+temp;
 +
}
 +
}
 +
return sum;
 +
}
 +
 
 +
uint16_t JQ6500::queryNum(uint8_t cmd){
 +
if(audioSwSerial)
 +
audioSwSerial->flush();
 +
else
 +
audioHwSerial->flush();
 +
sendCommand(cmd, NULL, 0);
 +
    delay(100);
 +
return dataParse();
 +
}
 +
 
 +
uint16_t JQ6500::queryTF(){
 +
return queryNum(QUERY_TF);
 +
}
 +
 
 +
uint16_t JQ6500::queryTFFile(){
 +
    return queryNum(QUERY_TF_FILE);
 +
}
 +
 
 +
uint16_t JQ6500::queryFlash(){
 +
    return queryNum(QUERY_FLASH);
 +
}
 +
 
 +
uint16_t JQ6500::queryFlashFile(){
 +
    return queryNum(QUERY_FLASH_FILE);
 +
}
 +
 
 +
uint16_t JQ6500::queryTotalTime(){
 +
    return queryNum(QUERY_TTIME);
 +
}
 +
 
 +
uint16_t JQ6500::queryPlayTime(){
 +
    return queryNum(QUERY_PTIME);
 +
}
 +
 
 +
String JQ6500::queryName(){
 +
String nameCache = "";
 +
char temp;
 +
uint16_t i = 0;
 +
if(audioSwSerial)
 +
audioSwSerial->flush();
 +
else
 +
audioHwSerial->flush();
 +
sendCommand(QUERY_NAME, NULL, 0);
 +
    delay(100);
 +
if(audioSwSerial){
 +
while(audioSwSerial->available()){
 +
temp = audioSwSerial->read();
 +
nameCache += temp;
 +
if(temp == 32){
 +
i++;
 +
}else{
 +
i = 0;
 +
}
 +
if(i >= 3){
 +
i = 0;
 +
nameCache.replace("  ", ".");
 +
}
 +
}
 +
}else{
 +
while(audioHwSerial->available()){
 +
temp = audioHwSerial->read();
 +
nameCache += temp;
 +
if(temp == 32){
 +
i++;
 +
}else{
 +
i = 0;
 +
}
 +
if(i >= 3){
 +
i = 0;
 +
nameCache.replace("  ", ".");
 +
}
 +
}
 +
}
 +
return nameCache;
 +
}
 +
 
 +
//
 +
// END OF FILE
 +
//
 +
</source>
 +
*JQ6500.h
 
<source lang="cpp">
 
<source lang="cpp">
#include "Arduino.h"  
+
//
#include <SoftwareSerial.h> 
+
// JQ6500 audio library for Arduino
//定义蓝牙通信串口 
+
// VERSION: 0.1.0
SoftwareSerial mySerial(4, 5); // RX, TX 
+
//
 
+
 
//定义串口 
+
#ifndef __JQ6500_H__
#define FROM mySerial 
+
#define __JQ6500_H__
//#define FROM Serial 
+
 
#define VOICE Serial1 
+
#include <Arduino.h>
#define DEBUG Serial 
+
#include <SoftwareSerial.h>
 
+
int led_style = 0; 
+
#define AUDIO_VERSION "0.1.0"
int led_style_vol = 0; 
+
 
//int led_style_cache=0; 
+
#define STX 0x7E
 
+
#define ETX 0xEF
boolean led_sta = true; 
+
 
//定义亮度 
+
#define CMD_NEXT    0x01
#define led_brightness  3 
+
#define CMD_PREV    0x02
unsigned long led_time; 
+
#define CMD_CHOOSE  0x03
 +
#define CMD_UP      0x04
 +
#define CMD_DOWN    0x05
 +
#define CMD_VOL    0x06
 +
#define CMD_EQ      0x07
 +
#define CMD_DEVICE  0x09
 +
#define CMD_SLEEP  0x0A
 +
#define CMD_RESET  0x0C
 +
#define CMD_PLAY    0x0D
 +
#define CMD_PAUSE  0x0E
 +
#define CMD_FOLDER  0x0F
 +
#define CMD_MODE    0x11
 +
#define CMD_FILE    0x12
 +
 
 +
#define QUERY_STA  0x42
 +
#define QUERY_VOL  0x43
 +
#define QUERY_EQ    0x44
 +
#define QUERY_MODE  0x45
 +
#define QUERY_VERSION 0x46
 +
#define QUERY_TF    0x47
 +
#define QUERY_DISK  0x48
 +
#define QUERY_FLASH 0x49
 +
#define QUERY_TF_FILE 0x4B
 +
#define QUERY_DISK_FILE 0x4C
 +
#define QUERY_FLASH_FILE 0x4D
 +
#define QUERY_PTIME 0x50
 +
#define QUERY_TTIME 0x51
 +
#define QUERY_NAME  0x52
 +
#define QUERY_FILES 0x53
 +
 
 +
 
 +
class JQ6500{
 +
public:
 +
JQ6500(SoftwareSerial *ser);
 +
JQ6500(HardwareSerial *ser);
 +
 +
void begin(uint16_t baud);
 +
void next();
 +
void prev();
 +
void choose(uint16_t num);
 +
void volUp();
 +
void volDown();
 +
void volumn(uint8_t vol);
 +
void eq(uint8_t eq);
 +
void setDevice(uint8_t device);
 +
void sleep();
 +
void reset();
 +
void play();
 +
void pause();
 +
void folder(uint8_t temp);
 +
void setMode(uint8_t temp);
 +
void chooseFile(uint8_t folder, uint8_t file);
 +
void init(uint8_t device, uint8_t mode, uint8_t vol);
 +
 +
uint16_t queryTF();
 +
uint16_t queryTFFile();
 +
uint16_t queryFlash();
 +
uint16_t queryFlashFile();
 +
uint16_t queryTotalTime();
 +
 
 +
uint16_t queryPlayTime();
 +
String queryName();
 +
 +
private:
 +
uint8_t sendBuffer[8];
 +
uint8_t cmdBuffer[8];
 +
 
 +
SoftwareSerial *audioSwSerial;
 +
HardwareSerial *audioHwSerial;
 +
 
 +
void common_init(void);
 +
void sendCommand(uint8_t cmd, uint8_t *buf, uint16_t len);
 +
uint16_t dataParse();
 +
uint16_t queryNum(uint8_t cmd);
 +
};
 +
 
 +
#endif
 +
//
 +
// END OF FILE
 +
//
 
</source>
 
</source>
*eep.h
+
JQ6500_def.h
 
<source lang="cpp">
 
<source lang="cpp">
#include "Arduino.h" 
+
//
 
+
// AM2321 Temperature & Humidity Sensor library for Arduino
//USER----------------------------- 
+
// VERSION: 0.1.0
#include <EEPROM.h> 
+
//
#define EEPROM_write(address, p) { 
+
// The MIT License (MIT)
int i = 0; 
+
//
byte *pp = (byte *) & (p); 
+
// Copyright (c) 2013 Wang Dong
for(; i < sizeof(p); i++) EEPROM.write(address + i, pp[i]); 
+
//
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
#define EEPROM_read(address, p)  { 
+
// of this software and associated documentation files (the "Software"), to deal
int i = 0; 
+
// in the Software without restriction, including without limitation the rights
byte *pp = (byte *) & (p); 
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
for(; i < sizeof(p); i++) pp[i] = EEPROM.read(address + i); 
+
// copies of the Software, and to permit persons to whom the Software is
+
// furnished to do so, subject to the following conditions:
 
+
//
struct config_type 
+
// The above copyright notice and this permission notice shall be included in
+
// all copies or substantial portions of the Software.
    int EEPROM_led_style_vol; 
+
//
    int EEPROM_led_style; 
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
}; 
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//向EEPROM里写数据 
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
void eeprom_WRITE() 
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    config_type config;    // 定义结构变量config,并定义config的内容 
+
// THE SOFTWARE.
 
+
//
    config.EEPROM_led_style_vol = led_style_vol; 
+
 
    config.EEPROM_led_style = led_style; 
+
#ifndef __JQ6500_DEF_H__
 
+
#define __JQ6500_DEF_H__
    EEPROM_write(0, config); // 变量config存储到EEPROM,地址0写入 
+
 
+
#define EQ_NORMAL    0x00
//从EEPROM里读数据 
+
#define EQ_POP    0x01
void eeprom_READ() 
+
#define EQ_ROCK  0x02
+
#define EQ_JAZZ      0x03
    config_type config_readback; 
+
#define EQ_CLASSIC    0x04
    EEPROM_read(0, config_readback); 
+
#define EQ_BASE 0x05
 
+
 
    led_style_vol = config_readback.EEPROM_led_style_vol; 
+
#define DEVICE_U    0x00
    led_style = config_readback.EEPROM_led_style; 
+
#define DEVICE_TF    0x01
+
#define DEVICE_AUX  0x02
 
+
#define DEVICE_SLEEP    0x03
unsigned int freeRam () 
+
#define DEVICE_FLASH    0x04
+
 
    extern unsigned int __heap_start, *__brkval; 
+
#define MODE_ALL 0x00
    unsigned int v; 
+
#define MODE_FOL 0x01
    return (unsigned int) &v - (__brkval == 0 ? 
+
#define MODE_ONE 0x02
(unsigned int) &__heap_start : (unsigned int) __brkval); 
+
#define MODE_RAM 0x03
}  
+
#define MODE_ONE_STOP 0x04
 +
 
 +
 
 +
#endif
 +
//
 +
// END OF FILE
 +
//
 
</source>
 
</source>
*Led.h
+
Audio.h
 
<source lang="cpp">
 
<source lang="cpp">
#include "arduino.h" 
+
#include "arduino.h"
#include <Adafruit_NeoPixel.h> 
+
#include <EEPROM.h>
//定义Lamp模块所用引脚 
+
 
#define LED_PIN A0 
+
#define EEPROM_write(address, p) {int i = 0; byte *pp = (byte*)&(p);for(; i < sizeof(p); i++) EEPROM.write(address+i, pp[i]);}
//定义Lamp灯对象,控制6盏彩灯并通过A0口控制 
+
#define EEPROM_read(address, p) {int i = 0; byte *pp = (byte*)&(p);for(; i < sizeof(p); i++) pp[i]=EEPROM.read(address+i);}
Adafruit_NeoPixel strip = 
+
 
Adafruit_NeoPixel(6, LED_PIN, NEO_GRB + NEO_KHZ800); 
+
struct config_type
//先定义8种颜色,分别是灭,红,澄,黄,绿,青,蓝,紫 
+
{
uint32_t color[8] = 
+
  int EEPROM_music_mode;
+
  int EEPROM_music_vol;
    strip.Color(0, 0, 0)
+
};
strip.Color(255 / led_brightness, 0, 0)
+
 
strip.Color(255 / led_brightness, 165 / led_brightness, 0)
+
config_type config;      // 定义结构变量config,并定义config的内容
strip.Color(255 / led_brightness, 255 / led_brightness, 0)
+
 
strip.Color(0, 255 / led_brightness, 0)
+
void eeprom_WRITE(int mode, int vol)
strip.Color(0, 127 / led_brightness, 255 / led_brightness)
+
{
strip.Color(0, 0, 255 / led_brightness)
+
  config_type config;      // 定义结构变量config,并定义config的内容
strip.Color(139 / led_brightness, 0, 255 / led_brightness) 
+
  config.EEPROM_music_mode=mode;
}; 
+
  config.EEPROM_music_vol=vol;
 
+
 
uint32_t color_t[3] = //3种颜色分别为,灭,暖色,冷色 
+
  EEPROM_write(0, config)// 变量config存储到EEPROM,地址0写入
+
}
    strip.Color(0, 0, 0)
+
 
strip.Color(255/led_brightness,137/led_brightness,18/led_brightness),
+
config_type eeprom_READ()
strip.Color(254/led_brightness,249/led_brightness,255/led_brightness) 
+
{
}; 
+
  config_type config_readback;
//设置全部彩灯颜色,参数c是strip.Color(int a, int b, int c)的返回值 
+
  EEPROM_read(0, config_readback);
//相当于对应颜色的编码 
+
  return config_readback;
void colorWipe(uint32_t c) 
+
}
{ 
+
</cpp>
    for(uint16_t i = 0; i < strip.numPixels(); i++) 
+
<source lang="cpp">
//通过循环达到控制6盏灯的效果 
+
#include "arduino.h"
    { 
+
 
        strip.setPixelColor(i, c);//设置第i个彩灯为c编码对应的颜色 
+
int vol,volCache;
    } 
+
unsigned long time_cache=0;
    strip.show(); 
+
 
+
int keyRead()
 
+
{
// 输入0-255任意一个数得到对应的唯一的一种颜色 
+
  if(analogRead(A6)<10)
// 颜色会从红-绿->蓝->红依次渐变循环 
+
  {
uint32_t Wheel(byte WheelPos) 
+
    delay(50);
{ 
+
    if(analogRead(A6)<10)
    if(WheelPos < 85) 
+
      return 1;
    { 
+
  }
        return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); 
+
  if(analogRead(A6)>200 && analogRead(A6)<300)
    } 
+
  {
    else if(WheelPos < 170) 
+
    delay(50);
    { 
+
    if(analogRead(A6)>200 && analogRead(A6)<300)
        //因为WheelPos * 3在85到170的情况下会超过255,因此要先自减85 
+
      return 2;
        WheelPos -= 85; 
+
  }
        return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); 
+
  if(analogRead(A6)>300 && analogRead(A6)<400)
    } 
+
  {
    else 
+
    delay(50);
    { 
+
    if(analogRead(A6)>300 && analogRead(A6)<400)
        //因为WheelPos * 3在170以上的情况下会超过255,因此要先自减170 
+
      return 3;
        WheelPos -= 170; 
+
  }
        return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); 
+
  return 0;
    } 
+
}
} 
+
 
 
+
int keyGet()
uint16_t i, j; 
+
{
//控制6盏彩灯以彩虹方式显示颜色 
+
  int key = 0;
void rainbowCycle(uint8_t wait) 
+
  vol = keyRead();  //检测输入动作
{ 
+
  if(vol==0){
    if(j < 256 * 5) 
+
      if(volCache == 1){
    { 
+
          key = 1;
        j++; 
+
      }else if(volCache > 1){
        for(i = 0; i < strip.numPixels(); i++) 
+
          key = volCache + 3;  
        { 
+
      }else{
            strip.setPixelColor(i, 
+
          key = 0;  
Wheel(((i * 256 / strip.numPixels()+ j) & 255)); 
+
      }
        } 
+
      time_cache=millis();
        strip.show(); 
+
  }
        delay(wait); 
+
  volCache = vol;
    } 
+
  if(millis() > time_cache + 1000){
    else 
+
      key = vol + 1;
        j = 0; 
+
      volCache = 0;
 +
  }
 +
  return key;
 
}
 
}
 
</source>
 
</source>
 +
 
==视频==
 
==视频==

2015年11月24日 (二) 02:52的版本

概述

  • 项目名称:Microduino语言情景灯
  • 目的:通过语音控制彩灯的各种亮法
  • 难度:中
  • 耗时:2小时
  • 制作者:
  • 简介:

本次教程我们使用Microduino产品模块快速搭建一个可以识别人类语音的灯光控制系统,玩家可以迅速上手,并且可以通过说话,控制灯光模块的亮灭,选色等功能。

材料清单

  • Microduino设备
模块 数量 功能
Microduino-Core+/zh 1 核心板
Microduino-USBTTL/zh 1 下载程序
Microduino-BM/zh 1 供电
Microduino-BT/zh 1 无线通信
Microduino-Duo-H/zh 1 平面扩展
Microduino-Sensorhub/zh 1 连接传感器
Microduino-Lamp/zh 1 连接传感器
  • 其他设备
模块 数量 功能
锂电池 1 供电
Sensor连接线 1 下载程序
语音识别版和输入话筒 1 语音套件,识别声音
发光二极管 1 语音输入指示
Micro USB数据线 1 下载程序

实验原理

语音情景灯模块的原理是记录语音和情景的一一映射关系,通过拼音识别输入的语音,返回给核心对应的情景编号,核心根据得到的情景编号,通过模拟口控制灯光模块每盏led灯的亮灭,实现不同情境下的不同颜色组合。 整个灯光控制系统包括四个部分:

  • 供电系统

使用[Microduino-BM]电源管理模块和外接电池组合为语音灯供电

  • 中央处理器

中央处理器是语音情景灯系统的核心。采用Microduino-Core+作为核心。

  • 无线通讯

语音情景灯系统采用Bluetooth无线通讯方案,使用[Microduino-BT(4.0)]模块,通讯速度响应快,在空阔地域的控制范围大概8米左右。

  • 语音控制

采集并识别语音内容。

套件使用Microduino-Lamp模块作为发光设备,该模块级联了6盏RGB3色彩灯,并可以使用相应的库函数控制每盏彩灯的颜色,为了清晰说明彩灯的控制方法,用一个简单的程序举例。 打开Arduino IDE,在文件(File)—>示例(Examples)—>_99_LCD_NeoPixel目录下,点开例程strandtest。

Emotionlightlib.jpg
#include <Adafruit_NeoPixel.h> 
#define PIN 6 //定义控制引脚
// 参数 1 = strip中彩灯的数目 
// 参数 2 = 引脚号
// 参数 3 = 彩灯类型, 可多选(前两个中选一个,后两个中选一个): 
//   NEO_RGB     Pixels are wired for RGB bitstream 
//   NEO_GRB     Pixels are wired for GRB bitstream 
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels) 
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip) 
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800); 
void setup() 
{ 
    strip.begin(); 
    strip.show(); //初始化所有彩灯都为灭
} 
void loop() 
{ 
    // 点亮彩灯的方法 
    colorWipe(strip.Color(255, 0, 0), 50); // 点亮红色
    colorWipe(strip.Color(0, 255, 0), 50); // 点亮绿色
    colorWipe(strip.Color(0, 0, 255), 50); // 点亮蓝色
    rainbow(20); 
    rainbowCycle(20); 
} 
//用“c”所代表的颜色依次点亮各盏彩灯,每点亮一盏等“wait”秒
void colorWipe(uint32_t c, uint8_t wait) 
{ 
    for(uint16_t i = 0; i < strip.numPixels(); i++)  //依次点亮
    { 
       strip.setPixelColor(i, c); //这个函数用于把第i盏灯用“c”所指颜色点亮
        strip.show(); //这个函数会将setPixelColor函数所写入的控制信息显示
                        //出来,也就是靠它点亮LAMP模块 
        delay(wait); 
    } 
} 

void rainbow(uint8_t wait) //彩虹显示
{ 
    uint16_t i, j; 
 
    for(j = 0; j < 256; j++)  //渐变255种颜色
    { 
      for(i = 0; i < strip.numPixels(); i++) //依次点亮彩灯,间隔wait毫秒
        { 
            strip.setPixelColor(i, Wheel((i + j) & 255)); 
        } 
        strip.show(); 
        delay(wait); 
    } 
} 
// 与上面的函数稍有区别,添加了彩虹的循环
void rainbowCycle(uint8_t wait) 
{ 
    uint16_t i, j; 
 
    for(j = 0; j < 256 * 5; j++) //彩虹循环5次
    { 
        for(i = 0; i < strip.numPixels(); i++) 
        { 
            strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));  //为了循环而添加的数学变换
        } 
        strip.show(); 
        delay(wait); 
    } 
} 
// 输入0-255任意一个数得到对应的唯一的一种颜色 
// 颜色会从红-绿->蓝->红依次渐变循环 
uint32_t Wheel(byte WheelPos) 
{ 
    if(WheelPos < 85) 
    { 
        return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); 
    } 
    else if(WheelPos < 170) 
    { 
        //因为WheelPos * 3在85到170的情况下会超过255,因此要先自减85 
        WheelPos -= 85; 
        return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); 
    } 
    else 
    { 
        //因为WheelPos * 3在170以上的情况下会超过255,因此要先自减170 
        WheelPos -= 170; 
        return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); 
    } 

文档

调试过程

  • 程序下载

Microduino-Core+/zhMicroduino-USBTTL/zh堆叠在一起.用数据线将写好的程序通过Microduino-USBTTL/zh上传到Microduino-Core+/zh上。 注意:最好不要将所有模块堆叠在一起之后再上传程序

Download1.jpg

找到该程序的物理位置,"语音情景灯/ EGGROOM/ EGGROOM.ino",双击即可

Emotionlightopen.jpg

程序下载 点击"√",编译程序。 点击【工具】,选择正确的板+处理器+端口。 点击"→",进行上传。

Downloadselectcore+.jpg

搭建

搭建硬件电路,将用到的设备叠加起来(无上下顺序) Microduino-Core+ Microduino-USBTTL Microduino-BT(4.0) Microduino-BM Microduino-Duo-H Microduino-sensorhub Microduino-Lamp 锂电池

安装发光二极管 将发光二极管的负极插在Microduino-Sensorhub/zh传感器扩展模块的A7管脚,正极插在SDA管脚

将传感器数据线插入第三行第一列插槽,观察这里插线的部分,4根线从左到右分别是GND,VCC,D2,D3接脚(不要看颜色,实物颜色可能与本例不同)

安装语音话筒 语音话筒无正负极,将两端分别插在语音传感器的MIP和MIN上即可

注意问题

  • 每次语音输入命令,都要先说f1对应的内容,本例是“美科”读者可根据自己的设置输入
  • 二极管极性的判断,反接的话二极管不会发光

程序说明

  • 主函数
#include "Arduino.h"
#include "audio.h"
#include "JQ6500.h"
#include "JQ6500_def.h"
#include "def.h"
#include <Wire.h>
#include <AM2321.h>
#include <Adafruit_NeoPixel.h>
//用户自定义部分------------------------
AM2321 am2321;
//用户自定义部分------------------------
#define PIN A0
#define NOTICE A2
Adafruit_NeoPixel strip = Adafruit_NeoPixel(6, PIN, NEO_GRB + NEO_KHZ800);

Adafruit_NeoPixel notice = Adafruit_NeoPixel(6, NOTICE, NEO_GRB + NEO_KHZ800);
uint32_t color[9] =
{
  strip.Color(0, 0, 0), strip.Color(255, 0, 0), strip.Color(248, 141, 30), strip.Color(255, 255, 0), strip.Color(0, 255, 0), strip.Color(0, 127, 255), strip.Color(0, 0, 255), strip.Color(139, 0, 255), strip.Color(255, 255, 255)
};
//用户自定义部分------------------------
int music_vol = 25;         //初始音量0~30
int music_Mode = MODE_ONE_STOP;
JQ6500 AUDIO(&VOICE);         //声明audio类
//用户自定义部分------------------------
int num = 0;
boolean buffer_sta = false;
boolean start = false;
int brightness = 255;
unsigned long timer_voice = millis();
int flag_on = 0;

int c = 0;
int DHTPIN = 8;
int temperature;
//func===================================
int judge(String cmd);
void writecommand();
void colorWipe(uint32_t c);
void dht11();
void tempsound();
//SETUP===================================
void SETUP_begin()
{
  DEBUG.begin(9600);
  VOICE.begin(9600);
  VOICE.begin(9600);
  pinMode(DHTPIN,INPUT);
  //EEPROM---------------------
  config = eeprom_READ();
  music_Mode = config.EEPROM_music_mode;
  music_vol = config.EEPROM_music_vol;
  AUDIO.init(DEVICE_TF, music_Mode, music_vol);
  delay(1000);
//  AUDIO.choose(1);
  AUDIO.pause();
  strip.begin();	//初始化LED
  notice.begin();
  pinMode(6,OUTPUT);
    notice.setPixelColor(0, strip.Color(0, 0, 0));
    notice.show(); 
  for (int i = 0; i < 6; i++) // 初始化所有灯都为灭
  {
    strip.setPixelColor(i, strip.Color(0, 0, 0));
    strip.show();
  }
 writecommand();    //语音模块配置
}
//LOOP====================================
void LOOP_priority_1ST()
{
  if (millis() < timer_voice) {
    timer_voice = millis();  //同步
  }
  if (millis() - timer_voice < 10000)  //从有配置指令声音输入开始计时
  {
    notice.setPixelColor(0,notice.Color(255, 255, 255));
    notice.show();
  }
  else
  {
    notice.setPixelColor(0,notice.Color(0, 0, 0));
    notice.show();
    flag_on = 0;
  }

  if (VOICE.available())
  {
    String cmd = "";
    if (VOICE.available()) //如果从语音芯片接收到用户的语音输入数据
    {
      cmd += char(VOICE.read()); //读出串口内的内容
      delay(10);
      cmd += char(VOICE.read());
      delay(10);
    }
    Serial.println(flag_on);
    Serial.println(cmd);
    if (cmd == "pp") //如果是语音开关指令对应的内容
    {
      DEBUG.println("Voice IN!");  //在串口提示 输入声音 
      AUDIO.choose(1);
      delay(2000);
      AUDIO.pause();
      flag_on = 1;
      notice.Color(0, 0, 0);
      timer_voice = millis(); //记录当前的系统运行时间
    }
    if (flag_on)   //如果进入了语音输入状态
    {
      c = judge(cmd);
      DEBUG.print("C is :");
      DEBUG.println(c);
      switch (c)  //根据不同的语音指令,选择对应的应答
      {
        case 1:          //名字
          timer_voice = 0;
          AUDIO.choose(4);
          timer_voice = millis();
          break;
        case 2:        //年龄
          timer_voice = 0;
          AUDIO.choose(6);
          timer_voice = millis();
          break;
        case 3:                      //讲故事
          timer_voice = 0;
          AUDIO.choose(3);
          timer_voice = millis();
          delay(9000);     //防止误触发打断
          break;
        case 4:                 //背诗
          timer_voice = 0;
          AUDIO.choose(2);
          delay(3000);
          AUDIO.choose(5);
          delay(10000);
          timer_voice = millis();
          break;
        case 5:                       //天气如何
          timer_voice = 0;
          am2321.read();
          temperature = am2321.temperature/10.0;
          Serial.println(temperature);
          if(temperature<20)AUDIO.choose(15);
          if(temperature>=20&&temperature<27)AUDIO.choose(14);
          if(temperature>=27)AUDIO.choose(16);
          timer_voice = millis();
          break;
        case 6:                     //好黑
          timer_voice = 0;
          AUDIO.choose(9);
          colorWipe(color[8]);
          timer_voice = millis();
          break;
        case 7:                       //再见
          timer_voice = 0;
          AUDIO.choose(11);
          timer_voice = millis();
          break;
        case 8:              //傻瓜
          timer_voice = 0;
          AUDIO.choose(12);
          timer_voice = millis();
          break;
        case 9:                     //关灯
          timer_voice = 0;
          AUDIO.choose(10);         //好的
          colorWipe(color[0]);
          timer_voice = millis();
          break;
        case 10:                  //谢谢
          timer_voice = 0;
          AUDIO.choose(13);        //不客气
          timer_voice = millis();
          break;
           case 11:                  //吹泡
          timer_voice = 0;
          digitalWrite(6,HIGH);
          delay(8000);
          digitalWrite(6,LOW);
          timer_voice = millis();
          break;
        case 0:
          timer_voice = 0;
          AUDIO.pause();
          timer_voice = millis();
          break;
      }
      while (VOICE.available()){
      VOICE.read();}
    }
  }
}


int judge(String cmd)
{
  int c;
  if (cmd == mz)c = 1;
  if (cmd == nl)c = 2;
  if (cmd == gs)c = 3;
  if (cmd == sg)c = 4;
  if (cmd == wd)c = 5;
  if (cmd == kd)c = 6;
  if (cmd == zj)c = 7;
  if (cmd == sm)c = 8;
  if (cmd == gd)c = 9;
  if (cmd == xx)c = 10;
  if(cmd == cp)c = 11;
  if (cmd == "pp") {
    c = 0;
  }
  return c;
}

void writecommand()
{
  VOICE.println("{d1}");
  delay(200);
  VOICE.println("{d1}");    //确认进入调试模式
  delay(200);
  VOICE.println("{c0}");      //清除已有指令
  delay(200);
  VOICE.println("{a0pao pao|s0pp}");  //开启语音指令
  delay(200);
  VOICE.println("{a0ni de ming zi|s0mz}"); //你的名字
  delay(200);
  VOICE.println("{a0ni de nian ling|s0nl}");  // 你的年龄
  delay(200);
  VOICE.println("{a0jiang gu shi|s0gs}");   //讲故事
  delay(200);
  VOICE.println("{a0bei shi|s0sg}"); //背诗
  delay(200);
  VOICE.println("{a0sha gua|s0sm}"); //傻瓜
  delay(200);
//  VOICE.println("{a0tian qi ru he|s0wd}"); //天气如何
//  delay(200);
  VOICE.println("{a0kai deng|s0kd}");  //开灯
  delay(200);
  VOICE.println("{a0guan deng|s0gd}");  //关灯
  delay(200);
  VOICE.println("{a0zai jian|s0zj}");  //再见
  delay(200);
  VOICE.println("{a0xie xie|s0xx}");  //谢谢
  delay(200);
  VOICE.println("{l0}");     //加载
  delay(200);
  VOICE.println("{d0}");    //退出调试
  delay(200);
}
void colorWipe(uint32_t c) {
  for (uint16_t i = 0; i < strip.numPixels(); i++)
  {
    strip.setPixelColor(i, c);
    strip.show();
  }
}
  • def.h
#include "Arduino.h"


#include <SoftwareSerial.h>
//定义蓝牙通信串口
//SoftwareSerial mySerial(4, 5); // RX, TX

//定义串口
//#define YUYIN mySerial    
#define VOICE Serial1
#define DEBUG Serial

String mz = "mz";
String nl = "nl";
String gs = "gs";
String sg = "sg";
String wd = "wd";
String kd = "kd";
String zj = "zj";
String sm = "sm";
String gd = "gd";
String xx = "xx";
String zl = "zl";
String cp = "cp";

int led_style=2;
int led_style_vol=2;
//int led_style_cache=0;

boolean led_sta=true;
//定义亮度
#define led_brightness  3
unsigned long led_time;
  • JQ6500.cpp
// 
// JQ6500 audio library for Arduino
//

#include "JQ6500.h"
#include "JQ6500_def.h"
#include <SoftwareSerial.h>

JQ6500::JQ6500(SoftwareSerial *ser) {
    common_init();
	audioSwSerial = ser;	
}

JQ6500::JQ6500(HardwareSerial *ser) {
    common_init();
	audioHwSerial = ser;
}

void JQ6500::common_init(void){
	audioSwSerial = NULL;
	audioHwSerial = NULL;
	
}

void JQ6500::begin(uint16_t baud){
	if(audioSwSerial) audioSwSerial->begin(baud);
	else			  audioHwSerial->begin(baud);
	delay(10);
}

void JQ6500::sendCommand(uint8_t cmd, uint8_t *buf, uint16_t len){
	sendBuffer[0] = STX;
	sendBuffer[1] = len+2;
	sendBuffer[2] = cmd;
	if(len > 0)
		memcpy(sendBuffer+3, buf, len);
	sendBuffer[len+3] = ETX;
	if(audioSwSerial)
		audioSwSerial->write(sendBuffer, 4+len);
	else
		audioHwSerial->write(sendBuffer, 4+len);
	delay(100);
}

void JQ6500::next(){
	
	sendCommand(CMD_NEXT, NULL, 0);
}

void JQ6500::prev(){
	
	sendCommand(CMD_PREV, NULL, 0);
}

void JQ6500::choose(uint16_t num){
	cmdBuffer[0] = num>>8;
	cmdBuffer[1] = num&0x00FF;
	sendCommand(CMD_CHOOSE, cmdBuffer, 2);
}

void JQ6500::volUp(){
	
	sendCommand(CMD_UP, NULL, 0);
}

void JQ6500::volDown(){
	
	sendCommand(CMD_DOWN, NULL, 0);
}

void JQ6500::volumn(uint8_t vol){
	cmdBuffer[0] = vol;
	sendCommand(CMD_VOL, cmdBuffer, 1);
}

void JQ6500::eq(uint8_t eq){
	cmdBuffer[0] = eq;
	sendCommand(CMD_EQ, cmdBuffer, 1);	
}

void JQ6500::setDevice(uint8_t device){
	cmdBuffer[0] = device;
	sendCommand(CMD_DEVICE, cmdBuffer, 1);
}

void JQ6500::sleep(){
	
	sendCommand(CMD_SLEEP, NULL, 0);
}

void JQ6500::reset(){
	
	sendCommand(CMD_RESET, NULL, 0);
}

void JQ6500::play(){
	
	sendCommand(CMD_PLAY, NULL, 0);
}

void JQ6500::pause(){
	
	sendCommand(CMD_PAUSE, NULL, 0);
}

void JQ6500::folder(uint8_t temp){
	cmdBuffer[0] = temp;
	sendCommand(CMD_FOLDER, cmdBuffer, 1);
}

void JQ6500::setMode(uint8_t temp){
	cmdBuffer[0] = temp;
	sendCommand(CMD_MODE, cmdBuffer, 1);
}

void JQ6500::chooseFile(uint8_t folder, uint8_t file){
	cmdBuffer[0] = folder;
	cmdBuffer[1] = file;
	sendCommand(CMD_FILE, cmdBuffer, 2);
}

void JQ6500::init(uint8_t device, uint8_t mode, uint8_t vol){
	begin(9600);
	delay(300);
	reset();
	setDevice(device);
	setMode(mode);
	volumn(vol);
}


uint16_t JQ6500::dataParse(){
	uint16_t sum = 0;
	uint8_t temp;
	if(audioSwSerial){
		while(audioSwSerial->available()){
			temp = char(audioSwSerial->read());
			if(temp>47&&temp<58){
				temp -=48;  
			}else if(temp>96&&temp<103){
				temp -=87;  
			}
			sum = sum*16+temp; 
		}
	}else{
		while(audioHwSerial->available()){
			temp = char(audioHwSerial->read());
			if(temp>47&&temp<58){
				temp -=48;  
			}else if(temp>96&&temp<103){
				temp -=87;  
			}
			sum = sum*16+temp; 
		}
	}
	return sum;	
}

uint16_t JQ6500::queryNum(uint8_t cmd){
	if(audioSwSerial)
		audioSwSerial->flush();
	else
		audioHwSerial->flush();
	sendCommand(cmd, NULL, 0);	
    delay(100);	
	return dataParse();	
}

uint16_t JQ6500::queryTF(){
	return queryNum(QUERY_TF);	
}

uint16_t JQ6500::queryTFFile(){
    return queryNum(QUERY_TF_FILE);	
}

uint16_t JQ6500::queryFlash(){
    return queryNum(QUERY_FLASH);	
}

uint16_t JQ6500::queryFlashFile(){
    return queryNum(QUERY_FLASH_FILE);	
}

uint16_t JQ6500::queryTotalTime(){	
    return queryNum(QUERY_TTIME);	
}

uint16_t JQ6500::queryPlayTime(){
    return queryNum(QUERY_PTIME);	
}

String JQ6500::queryName(){
	String nameCache = "";
	char temp;
	uint16_t i = 0;
	if(audioSwSerial)
		audioSwSerial->flush();
	else
		audioHwSerial->flush();
	sendCommand(QUERY_NAME, NULL, 0);	
    delay(100);
	if(audioSwSerial){
		while(audioSwSerial->available()){
			temp = audioSwSerial->read();
			nameCache += temp;
			if(temp == 32){
				i++;	
			}else{
				i = 0;
			}
			if(i >= 3){
				i = 0;
				nameCache.replace("   ", ".");
			}
		}
	}else{
		while(audioHwSerial->available()){
			temp = audioHwSerial->read();
			nameCache += temp;
			if(temp == 32){
				i++;	
			}else{
				i = 0;
			}
			if(i >= 3){
				i = 0;
				nameCache.replace("   ", ".");
			}
		}
	}
	return nameCache;	
}

//
// END OF FILE
//
  • JQ6500.h
// 
// JQ6500 audio library for Arduino
// VERSION: 0.1.0
//

#ifndef __JQ6500_H__
#define __JQ6500_H__

#include <Arduino.h>
#include <SoftwareSerial.h>
 
#define AUDIO_VERSION "0.1.0"

#define STX 0x7E
#define ETX 0xEF

#define CMD_NEXT    0x01
#define CMD_PREV    0x02
#define CMD_CHOOSE  0x03
#define CMD_UP      0x04
#define CMD_DOWN    0x05
#define CMD_VOL     0x06
#define CMD_EQ      0x07
#define CMD_DEVICE  0x09
#define CMD_SLEEP   0x0A
#define CMD_RESET   0x0C
#define CMD_PLAY    0x0D
#define CMD_PAUSE   0x0E
#define CMD_FOLDER  0x0F
#define CMD_MODE    0x11
#define CMD_FILE    0x12

#define QUERY_STA   	0x42
#define QUERY_VOL   	0x43
#define QUERY_EQ    	0x44
#define QUERY_MODE  	0x45
#define QUERY_VERSION 	0x46
#define QUERY_TF    	0x47
#define QUERY_DISK  	0x48
#define QUERY_FLASH 	0x49
#define QUERY_TF_FILE 	0x4B
#define QUERY_DISK_FILE 0x4C
#define QUERY_FLASH_FILE	0x4D
#define QUERY_PTIME 	0x50
#define QUERY_TTIME 	0x51
#define QUERY_NAME  	0x52
#define QUERY_FILES 	0x53


class JQ6500{
public:
	JQ6500(SoftwareSerial *ser);
	JQ6500(HardwareSerial *ser);
	
	void begin(uint16_t baud);
	void next();
	void prev();
	void choose(uint16_t num);
	void volUp();
	void volDown();
	void volumn(uint8_t vol);
	void eq(uint8_t eq);
	void setDevice(uint8_t device);
	void sleep();
	void reset();
	void play();
	void pause();
	void folder(uint8_t temp);
	void setMode(uint8_t temp);
	void chooseFile(uint8_t folder, uint8_t file);
	void init(uint8_t device, uint8_t mode, uint8_t vol);
	
	uint16_t queryTF();
	uint16_t queryTFFile();
	uint16_t queryFlash();
	uint16_t queryFlashFile();
	uint16_t queryTotalTime();

	uint16_t queryPlayTime();
	String queryName();
	
private:
	uint8_t sendBuffer[8];
	uint8_t cmdBuffer[8];

	SoftwareSerial *audioSwSerial;
	HardwareSerial *audioHwSerial;
  
	void common_init(void);
	void sendCommand(uint8_t cmd, uint8_t *buf, uint16_t len);
	uint16_t dataParse();
	uint16_t queryNum(uint8_t cmd);
};

#endif
//
// END OF FILE
//

JQ6500_def.h

// 
// AM2321 Temperature & Humidity Sensor library for Arduino
// VERSION: 0.1.0
//
// The MIT License (MIT)
//
// Copyright (c) 2013 Wang Dong
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

#ifndef __JQ6500_DEF_H__
#define __JQ6500_DEF_H__

#define EQ_NORMAL    	0x00
#define EQ_POP    		0x01
#define EQ_ROCK  		0x02
#define EQ_JAZZ      	0x03
#define EQ_CLASSIC    	0x04
#define EQ_BASE			0x05

#define DEVICE_U    	0x00
#define DEVICE_TF    	0x01
#define DEVICE_AUX  	0x02
#define DEVICE_SLEEP    0x03
#define DEVICE_FLASH    0x04

#define MODE_ALL		0x00
#define MODE_FOL		0x01
#define MODE_ONE		0x02
#define MODE_RAM		0x03
#define	MODE_ONE_STOP	0x04


#endif
//
// END OF FILE
//

Audio.h

#include "arduino.h"
#include <EEPROM.h>

#define EEPROM_write(address, p) {int i = 0; byte *pp = (byte*)&(p);for(; i < sizeof(p); i++) EEPROM.write(address+i, pp[i]);}
#define EEPROM_read(address, p)  {int i = 0; byte *pp = (byte*)&(p);for(; i < sizeof(p); i++) pp[i]=EEPROM.read(address+i);}

struct config_type
{
  int EEPROM_music_mode;
  int EEPROM_music_vol;
};

config_type config;      // 定义结构变量config,并定义config的内容

void eeprom_WRITE(int mode, int vol)
{
  config_type config;      // 定义结构变量config,并定义config的内容
  config.EEPROM_music_mode=mode;
  config.EEPROM_music_vol=vol;

  EEPROM_write(0, config);  // 变量config存储到EEPROM,地址0写入
}

config_type eeprom_READ()
{
  config_type config_readback;
  EEPROM_read(0, config_readback);
  return config_readback;
}
</cpp>
<source lang="cpp">
#include "arduino.h"

int vol,volCache;
unsigned long time_cache=0;

int keyRead()
{
  if(analogRead(A6)<10)
  {
    delay(50);
    if(analogRead(A6)<10)
      return 1;
  }
  if(analogRead(A6)>200 && analogRead(A6)<300)
  {
    delay(50);
    if(analogRead(A6)>200 && analogRead(A6)<300)
      return 2;
  }
  if(analogRead(A6)>300 && analogRead(A6)<400)
  {
    delay(50);
    if(analogRead(A6)>300 && analogRead(A6)<400)
      return 3;
  }
  return 0;
}

int keyGet()
{
   int key = 0;
   vol = keyRead();  //检测输入动作
   if(vol==0){
      if(volCache == 1){
          key = 1;
      }else if(volCache > 1){
          key = volCache + 3; 
      }else{
          key = 0; 
      } 
      time_cache=millis();
  }
  volCache = vol;
  if(millis() > time_cache + 1000){
      key = vol + 1;
      volCache = 0;
  }  
  return key;
}

视频