|
|
(未显示同一用户的3个中间版本) |
第105行: |
第105行: |
| [[File:Musicstep6.jpg||600px|center]] | | [[File:Musicstep6.jpg||600px|center]] |
| 至此音乐盒拼装完成 | | 至此音乐盒拼装完成 |
| + | ==刷卡使用== |
| + | 在一张空白的纸上使用黑色马克笔分别在不同边缘按照如图方式画下黑白相间的粗条纹 |
| + | [[File:刷卡.jpg||600px|center|thumb]] |
| + | |
| + | 之后将条纹从刷卡处刷过即可播放音乐。不同条纹数量即播放不同的音乐 |
| | | |
| ==注意事项== | | ==注意事项== |
第123行: |
第128行: |
| 在uiSetup中更改阈值设定 | | 在uiSetup中更改阈值设定 |
| ==程序说明== | | ==程序说明== |
− | | + | 主程序部分 |
− | CardPlayer.ino
| |
| <source lang="cpp"> | | <source lang="cpp"> |
− | #include "audio.h"
| |
− | #include "JQ6500.h"
| |
− | #include "JQ6500_def.h"
| |
− | #include <SoftwareSerial.h>
| |
− | #include <EEPROM.h>
| |
− | //定义蓝牙通信串口
| |
− | SoftwareSerial mySerial(2, 3); // RX, TX
| |
− | #define VOICE mySerial
| |
− | JQ6500 AUDIO(&VOICE); //声明audio类
| |
− |
| |
− |
| |
− | #define PIN_key A0 //灰度传感器输入引脚
| |
− |
| |
− | #include <Adafruit_NeoPixel.h>
| |
− | #define PIN 6 //Lantern灯引脚
| |
− | Adafruit_NeoPixel strip = Adafruit_NeoPixel(1, PIN, NEO_GRB + NEO_KHZ800);
| |
− |
| |
− | #define music_num_MIN 1 //歌曲最小数,固定
| |
− | #define music_num_MAX 10 //歌曲最大数,可改
| |
− |
| |
− | int music_vol=26; //初始音量0~30
| |
− |
| |
− | //---------------------------------
| |
− | boolean music_status = false; //歌曲播放状态
| |
− | int music_num = 1; //歌曲序号
| |
− |
| |
− | void setup()
| |
− | {
| |
− | Serial.begin(9600);
| |
− |
| |
− | strip.begin(); //初始化LED
| |
− | strip.show(); // Initialize all pixels to 'off'
| |
− |
| |
− | colorWipe(strip.Color(0, 255, 200), 10); //初始化颜色 R,G,B
| |
− |
| |
− | AUDIO.init(DEVICE_TF,MODE_FOL,music_vol); //初始化mp3模块
| |
− | AUDIO.pause(); //暂停播放
| |
− | }
| |
− |
| |
− | unsigned long time1,time2;
| |
− |
| |
− | #define paper_val_MIN 100 //黑白传感器触发小值
| |
− | #define paper_val_MAX 600 //黑白传感器触发大值
| |
− | boolean paper_sta=false;
| |
− | int paper_num=0; //纸张格数
| |
− |
| |
− | int LED_vol_sta=0; //LED呼吸灯反转
| |
− | int LED_vol=0; //LED呼吸灯值
| |
− |
| |
− |
| |
− | boolean key=HIGH; //按键
| |
− | boolean key_cache=LOW; //检测按键松开缓存
| |
− |
| |
| boolean vokey(boolean _vokey) | | boolean vokey(boolean _vokey) |
| { | | { |
第275行: |
第226行: |
| } | | } |
| </source> | | </source> |
− | Audio.h
| |
− | <source lang="cpp">
| |
− | #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;
| |
− | }
| |
− | </source>
| |
− | 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">
| |
− | //
| |
− | // 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
| |
− | //
| |
− | </source>
| |
− | JQ6500_def
| |
− | <source lang="cpp">
| |
− | //
| |
− | // 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
| |
− | //
| |
− | </source>
| |
| ==视频== | | ==视频== |