学研 大人の科学 vol.27 の8ビットマイコン Japanino(Arduino互換)のプログラムを公開します。
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 >> Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
- search this site.
- amazon
-
(当ブログのお勧め!)
(当サイトからも写真提供)
- tags
-
- 更新情報 (282)
- アクセス記録 (33)
- 神奈川 (31)
- どうぶつしょうぎ (25)
- 千葉 (24)
- 埼玉 (23)
- 鉄活動 (22)
- 群馬 (20)
- 東京 (18)
- 栃木 (14)
- 愛知 (13)
- 静岡 (10)
- 北海道 (9)
- 鉄道の調べの記録 (8)
- 大阪 (8)
- 宮城 (8)
- 山梨 (7)
- 三重 (7)
- 福島 (6)
- 岐阜 (6)
- 島根 (6)
- ナノブロック (6)
- 岡山 (5)
- 茨城 (5)
- 鉄活動東海 (5)
- 和歌山 (5)
- lisp (5)
- コラム (4)
- 地震 (4)
- 鉄玩 (4)
- 化物語 (4)
- 広島 (4)
- コンピュータ (4)
- プログラミング (4)
- 福岡 (4)
- 長野 (4)
- 新潟 (4)
- 山形 (4)
- 機関車 (4)
- 書籍 (4)
- 書泉グランデ (3)
- 貨物列車 (3)
- 小説 (3)
- 長崎 (3)
- Android (3)
- 海外 (2)
- 鉄予定 (2)
- アルゴロジック (2)
- 挨拶 (2)
- ウェブサイト (2)
- Japanino (2)
- 鳥取 (2)
- 石川 (2)
- 時刻表 (2)
- 鉄オフ (2)
- 滋賀 (1)
- 京都 (1)
- 福井 (1)
- IoT (1)
- 香港 (1)
- 韓国 (1)
- 岩手 (1)
- セキュリティ (1)
- ウォーキング (1)
- 駅名標 (1)
- 鉄コレ (1)
- 駅名標.JP (1)
- 飲み会 (1)
- 旅先料理の調べ (1)
- Twitter (1)
- はやぶさ (1)
- 過去 (1)
- mixi (1)
- RAILWAYS (1)
- archives
-
- 202010 (1)
- 201905 (3)
- 201701 (1)
- 201609 (1)
- 201606 (1)
- 201603 (4)
- 201508 (2)
- 201507 (1)
- 201505 (1)
- 201504 (1)
- 201503 (1)
- 201412 (3)
- 201406 (2)
- 201312 (1)
- 201309 (1)
- 201303 (1)
- 201302 (1)
- 201212 (2)
- 201210 (1)
- 201208 (4)
- 201204 (3)
- 201203 (4)
- 201202 (3)
- 201201 (4)
- 201112 (2)
- 201111 (2)
- 201109 (1)
- 201108 (8)
- 201105 (1)
- 201104 (6)
- 201103 (17)
- 201102 (8)
- 201101 (2)
- 201012 (5)
- 201011 (4)
- 201010 (12)
- 201009 (17)
- 201008 (13)
- 201007 (9)
- 201006 (13)
- 201005 (19)
- 201004 (20)
- 201003 (14)
- 201002 (15)
- 201001 (21)
- 200912 (22)
- 200911 (27)
- 200910 (19)
- 200909 (16)
- 200908 (18)
- 200907 (15)
- 200906 (6)
- 200905 (18)
- 200904 (7)
- recent trackback
- amazon
-
(当サイトからも写真提供)