井底添蛙 发表于 2013-12-12 13:33:18

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

本帖最后由 井底添蛙 于 2013-12-12 13:39 编辑

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

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

优酷发布太慢了

视频来了
http://v.youku.com/v_show/id_XNjQ3MTg3MzE2.html

优酷转码转的太恶心了


#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;// 计算每个节拍的时间,以一个节拍一秒为例,四分之一拍就是1000/4毫秒,八分之一拍就是1000/8毫秒
    tone(musicpin, song2,noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30; // 每个音符间的停顿间隔,以该音符的130%为佳
    delay(pauseBetweenNotes);
    noTone(musicpin);
}
};

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

void birthday()
{
for (int thisNote = 0; thisNote <25; thisNote++)
{
    int noteDuration = 1000/noteDurations1;// 计算每个节拍的时间,以一个节拍一秒为例,四分之一拍就是1000/4毫秒,八分之一拍就是1000/8毫秒
    tone(musicpin, song1,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();
}
}

huang1234 发表于 2013-12-12 19:15:42

{:soso_e179:}顶一下!你貌似很无聊啊!
页: [1]
查看完整版本: microduino core+ + oled 制作串口控制音乐盒