极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9928|回复: 1

microduino core+ + oled 制作串口控制音乐盒

[复制链接]
发表于 2013-12-12 13:33:18 | 显示全部楼层 |阅读模式
本帖最后由 井底添蛙 于 2013-12-12 13:39 编辑

闲来无聊,用microduino core+和oled屏制作了一个音乐盒,无奈没找到按键,就用电脑串口来选歌了
硬件搭建如下图:

外观很简陋{:soso_e113:} ,只是用来玩玩,没人会要这么昂贵的音乐盒{:soso_e144:}
下面是拍的视频:

优酷发布太慢了

视频来了


优酷转码转的太恶心了
[pre lang="arduino" line="1" file="musicbox.c"]

#define musicpin 9


#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
SSD1306 display(OLED_RESET);
int i=0;

#define L1 262
#define L2 294
#define L3 330
#define L4 349
#define L5 392
#define L6 440
#define L7 494

#define D0 0
#define D1 523
#define D2 587
#define D3 659
#define D4 698
#define D5 784
#define D6 880
#define D7 988

#define H1 1064
#define H2 1175
#define H3 1318
#define H4 1397
#define H5 1586
#define H6 1760
#define H7 1967

int song2[] = {
  D6,D7,H1,D7,H1,H3,D7,
  D3,D6,D5,D6,H1,D5,
  D4,D3,D4,D3,D4,H1,D3,D0,
  H1,H1,H1,D7,D5,D5,D7,D7,D0,

  D6,D6,H1,D7,H1,H3,D7,
  D3,D3,D6,D5,D6,H1,D5,
  D3,D4,H1,D7,H1,H2,H3,H1,
  D0,H1,D7,D6,D7,D6,D6,D0,

  H1,H2,H3,H2,H3,H5,H2,
  D5,D5,H1,D7,H1,H3,H3,H3,
  D6,D7,H1,D7,H1,H2,H1,D5,D5,
  H4,H3,H2,H1,H3,H3,D0,

  H3,D0,H3,H6,H5,H3,H2,H1,
  D0,H2,H1,H2,H5,H3,
  H3,H6,H5,H3,H2,H1,
  D0,H2,H1,H2,D7,D6,
  D6,D7,D6,  
};
char noteDurations2[] = {
  5,5,2,4,2,2,1,
  2,2,4,2,2,1,
  5,5,2,4,2,2,2,4,
  4,6,6,2,4,2,2,2,2,//30

  5,5,2,4,2,2,1,
  5,5,2,4,2,2,1,
  2,2,4,2,2,2,4,1,
  4,5,5,4,2,2,1,2,//30

  5,5,2,4,2,2,1,
  5,5,2,4,2,2,1,1,
  5,5,2,5,5,2,2,4,1,
  2,2,2,2,1,1,2,//31

  1,2,2,1,1,2,4,1,//29
  4,2,4,2,2,1,
  2,1,1,2,4,1,
  4,2,4,2,2,1,
  2,2,1,  
};

int song1[] = {
  392,392,440,392,523,493,
  392,392,440,392,587,523,
  392,392,784,659,523,493,440,
  698,698,659,523,587,523
};
int noteDurations1[] = {
  6,6,3,3,3,2,
  6,6,3,3,3,2,
  6,6,3,3,3,3,2,
  6,6,3,3,3,2  
};

int song[] = {
  523,523,784,784,880,880,784,
  698,698,659,659,587,587,523,
  784,784,698,698,659,659,587,
  784,784,698,698,659,659,587,
  523,523,784,784,880,880,784,
  698,698,659,659,587,587,523,
};
int noteDurations[] = {
  4,4,4,4,4,4,2,
  4,4,4,4,4,4,2,
  4,4,4,4,4,4,2,
  4,4,4,4,4,4,2,
  4,4,4,4,4,4,2,
  4,4,4,4,4,4,2,
};


void cityofsky()
{
  for (int thisNote = 0; thisNote <120; thisNote++)
  {
    int noteDuration = 1000/noteDurations2[thisNote];// 计算每个节拍的时间,以一个节拍一秒为例,四分之一拍就是1000/4毫秒,八分之一拍就是1000/8毫秒
    tone(musicpin, song2[thisNote],noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30; // 每个音符间的停顿间隔,以该音符的130%为佳
    delay(pauseBetweenNotes);
    noTone(musicpin);
  }
};

void littlestar()
{
  for (int thisNote = 0; thisNote <42; thisNote++)
  {
    int noteDuration = 1000/noteDurations[thisNote];// 计算每个节拍的时间,以一个节拍一秒为例,四分之一拍就是1000/4毫秒,八分之一拍就是1000/8毫秒
    tone(musicpin, song[thisNote],noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30; // 每个音符间的停顿间隔,以该音符的130%为佳
    delay(pauseBetweenNotes);
    noTone(musicpin);
  }
};

void birthday()
{
  for (int thisNote = 0; thisNote <25; thisNote++)
  {
    int noteDuration = 1000/noteDurations1[thisNote];// 计算每个节拍的时间,以一个节拍一秒为例,四分之一拍就是1000/4毫秒,八分之一拍就是1000/8毫秒
    tone(musicpin, song1[thisNote],noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30; // 每个音符间的停顿间隔,以该音符的130%为佳
    delay(pauseBetweenNotes);
    noTone(musicpin);
  }
};


void setup()
{
  Serial.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3c);
  display.display();
  delay(2000);
  display.clearDisplay();
  pinMode(musicpin, OUTPUT);   

}
void loop()
{
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println(" Music box ");
  display.drawLine(0,18,128,18, WHITE);
  display.display();

  i= int(Serial.read());

  if(i==49)
  {
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(10,24);
    display.println("City of sky");
    display.display();
    cityofsky();
    display.clearDisplay();
  }
  
  if(i==50)
  {
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(10,24);
    display.println("Little Star");
    display.display();
    littlestar();
    display.clearDisplay();
  }
  
  if(i==51)
  {
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(10,24);
    display.println("Birthday");
    display.display();
    birthday();
    display.clearDisplay();
  }
}
[/code]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2013-12-12 19:15:42 | 显示全部楼层
{:soso_e179:}顶一下!你貌似很无聊啊!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 06:36 , Processed in 0.044190 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表