Deprecated: Assigning the return value of new by reference is deprecated in /home/users/0/deca.jp-gomi/web/blog/class/View.php on line 25

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/0/deca.jp-gomi/web/blog/class/View.php on line 30

Warning: Cannot modify header information - headers already sent by (output started at /home/users/0/deca.jp-gomi/web/blog/class/View.php:25) in /home/users/0/deca.jp-gomi/web/blog/class/View.php on line 81
鉄道の調べ ブログ支線::(Japanino) JukeBox プログラムを公開!

(Japanino) JukeBox プログラムを公開!

学研 大人の科学 vol.27 の8ビットマイコン Japanino(Arduino互換)のプログラムを公開します。

japanino2.jpgjapanino1.jpg

2曲をスイッチの切り替えで交互に演奏するプログラムです。
LED も小節ごとに順に点灯します。
# インデントは保存されていませんので、見にくくなっています。

------------------------------------------------------------------
/*
Juke Box
2曲の歌をスイッチを入れる度に交互に演奏する
Copyright (C) GOMI Hiroshi 2010 All right reserved.
*/
#include "pitches.h"

int tonePin = 14;
int inputPin = 6;
int ledPin[] = {7, 8, 9, 10, 11, 12, 13}; int ledp = 0; int oledp = 0;

int tempo = 100;
int no = 1;

// 海
int tempo1 = 12;
int length1 = 26;
int score1[] = {
NOTE_B4, 4, NOTE_A4, 4, NOTE_G4, 4, NOTE_E4, 2, NOTE_A4, 2, NOTE_G4, 4, NOTE_E4, 4,
NOTE_D4, 2, NOTE_D4, 2, NOTE_G4, 4, NOTE_G4, 4, NOTE_A4, 8, 0, 4,
NOTE_B4, 4, NOTE_B4, 4, NOTE_D5, 4, NOTE_B4, 2, NOTE_B4, 2, NOTE_A4, 4, NOTE_G4, 4,
NOTE_E4, 2, NOTE_E4, 2, NOTE_D4, 4, NOTE_A4, 4, NOTE_G4, 8, 0, 4 };

// 夕やけこやけ
int tempo2 = 8;
int length2 = 55;
int score2[] = {
NOTE_G4, 2, NOTE_G4, 2, NOTE_G4, 2, NOTE_A4, 2, NOTE_G4, 2, NOTE_G4, 2, NOTE_G4, 2, NOTE_E4, 2,
NOTE_C4, 2, NOTE_C4, 2, NOTE_D4, 2, NOTE_E4, 2, NOTE_D4, 6, 0, 2,
NOTE_E4, 4, NOTE_E4, 2, NOTE_G4, 2, NOTE_A4, 2, NOTE_C5, 2, NOTE_C5, 2, NOTE_A4, 2,
NOTE_G4, 2, NOTE_G4, 2, NOTE_A4, 2, NOTE_G4, 2, NOTE_C5, 6, 0, 2,
NOTE_C5, 3, NOTE_D5, 1, NOTE_C5, 2, NOTE_A4, 2, NOTE_C5, 2, NOTE_C5, 2, NOTE_G4, 2, NOTE_G4, 2,
NOTE_A4, 2, NOTE_G4, 2, NOTE_A4, 2, NOTE_G4, 2, NOTE_E4, 6, 0, 2,
NOTE_G4, 2, NOTE_E4, 2, NOTE_D4, 2, NOTE_C4, 2, NOTE_D4, 2, NOTE_D4, 2, NOTE_G4, 2, NOTE_D4, 2,
NOTE_E4, 2, NOTE_G4, 2, NOTE_A4, 2, NOTE_G4, 2, NOTE_C5, 6, 0, 2 };

void setup() {
for (int led = 0; led < 7; led++) {
pinMode(ledPin[led], OUTPUT);
digitalWrite(ledPin[led], LOW); // set the LED off
}
pinMode(inputPin, INPUT);
digitalWrite(inputPin, HIGH);
}

void loop() {
// 入力スイッチ(inputPin)が ON になるまで待つ
for (;;) {
if (digitalRead(inputPin) == LOW) break;
delay(10);
}
// スイッチを入れると次の曲を演奏する
if (no == 1) {
play(tempo1, length1, score1);
no = 2;
} else {
play(tempo2, length2, score2);
no = 1;
}
// LED を消灯して1秒待つ
digitalWrite(ledPin[ledp], LOW); // set the LED off
digitalWrite(ledPin[oledp], LOW); // set the LED off
delay(1000);
ledp = 0;
oledp = 0;
}

/*
演奏(ループ)
入力(inputPin)が OFF になるまで無限ループで演奏する
演奏中は小節ごとにLEDが順に点灯する
@param tempox テンポ(2/4 のときは 8, 3/4 のときは 12, 4/4 のときは 16)
@param kength 楽譜サイズ
@param score[] 楽譜データ {音程、長さ}*
*/
void play(int tempox, int length, int score[]) {
for (;;) {
int current = 0;
for (int note = 0; note < length; note++) {
int duration = tempo * score[note * 2 + 1];
current += score[note * 2 + 1];
if (current >= tempox) {
digitalWrite(ledPin[ledp], HIGH); // set the LED on
current = 0;
ledp++;
if (ledp >= 7) ledp = 0;
} else {
digitalWrite(ledPin[oledp], LOW); // set the LED off
oledp = ledp;
}
tone(tonePin, score[note * 2], duration);
int pause = duration * 1.30;
delay(pause);
if (digitalRead(inputPin) == HIGH) return;
}
}
}
Calendar
<< April 2024 >>
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
282930
search this site.
ranking


amazon
(当ブログのお勧め!)
(当サイトからも写真提供)
tags
archives
recent comment
recent trackback
others
admin
amazon



(当サイトからも写真提供)







from 2010/7/4