TonyChyi

我在那一角落患过抽风

Arduino,你会唱小星星吗
2019年07月01日
 

 

啊,好久没更新一些代码什么的了,都快忘了这里是个技术博客了

#include <Arduino.h>

#define PIN 11  // Output pin

#define TONE_C 262
#define TONE_D 294
#define TONE_E 330
#define TONE_F 349
#define TONE_G 392
#define TONE_A 440  // Standard
#define TONE_B 494
#define TONE_N 0

#define BEAT_TIME 450 // ms, 120bpm
#define BEAT_SILENT 50 // ms, 120bpm
#define QUAD_SCORE BEAT_TIME+BEAT_SILENT  // 500ms
#define LEN(x) (sizeof x)/(sizeof(int))



void setup() {
    pinMode(PIN, OUTPUT);
    digitalWrite(PIN, LOW);
}

void loop() {
    delay(BEAT_TIME + BEAT_SILENT);
    line1(); line2();
    line1(); line3();
    line4();
    line1(); line3();
    delay(2000);
}

void line1() {
    // EEFG GFED CCDE
    play(TONE_E, QUAD_SCORE);
    play(TONE_E, QUAD_SCORE);
    play(TONE_F, QUAD_SCORE);
    play(TONE_G, QUAD_SCORE);

    play(TONE_G, QUAD_SCORE);
    play(TONE_F, QUAD_SCORE);
    play(TONE_E, QUAD_SCORE);
    play(TONE_D, QUAD_SCORE);

    play(TONE_C, QUAD_SCORE);
    play(TONE_C, QUAD_SCORE);
    play(TONE_D, QUAD_SCORE);
    play(TONE_E, QUAD_SCORE);
}

void line2 () {
    // E._DD0
    play(TONE_E, QUAD_SCORE + (QUAD_SCORE) / 2);
    play(TONE_D, (QUAD_SCORE) / 2);
    play(TONE_D, QUAD_SCORE);
    play(TONE_N, QUAD_SCORE);
}

void line3 () {
    // D._CC0
    play(TONE_D, QUAD_SCORE + (QUAD_SCORE) / 2);
    play(TONE_C, (QUAD_SCORE) / 2);
    play(TONE_C, QUAD_SCORE);
    play(TONE_N, QUAD_SCORE);
}

void line4() {
    // DDEC D_E_FEC D_E_FED CE.G0
    play(TONE_D, QUAD_SCORE);
    play(TONE_D, QUAD_SCORE);
    play(TONE_E, QUAD_SCORE);
    play(TONE_C, QUAD_SCORE);

    play(TONE_D, QUAD_SCORE);
    play(TONE_E, (QUAD_SCORE) / 2);
    play(TONE_F, (QUAD_SCORE) / 2);
    play(TONE_E, QUAD_SCORE);
    play(TONE_C, QUAD_SCORE);

    play(TONE_D, QUAD_SCORE);
    play(TONE_E, (QUAD_SCORE) / 2);
    play(TONE_F, (QUAD_SCORE) / 2);
    play(TONE_E, QUAD_SCORE);
    play(TONE_D, QUAD_SCORE);

    play(TONE_C, QUAD_SCORE);
    play(TONE_D, QUAD_SCORE);
    play(TONE_G / 2, QUAD_SCORE);
    play(TONE_N, QUAD_SCORE);
}

void play(int _tone,int len) {
    if (_tone > 0)
        tone(PIN, _tone);
    delay(len - BEAT_SILENT);
    noTone(PIN);
    delay(BEAT_SILENT);
}
Tags: #Arduino

 

TonyChyi © 2018 GPLv2