语音情景灯/zh

来自Microduino Wikipedia
502748957@qq.com讨论 | 贡献2015年11月30日 (一) 05:37的版本 程序说明
跳转至: 导航搜索

概述

  • 项目名称: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 "Micromenu_user.h"
#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include <AM2321.h>
//EEPROM---------------------
#include <EEPROM.h>

//用户自定义部分------------------------
#include <SoftwareSerial.h>


void setup() 
{
  SETUP_begin();     //该函数定义于 Micromenu_user.h
  //用户自定义部分========================
}


void loop() 
{
  {
    LOOP_priority_1ST(); //该函数定义于 Micromenu_user.h
  }
}

Micromenu_user.h

#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();    //语音模块配置
 DEBUG.println("SetUP");
}
//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;

视频