第8堂:療癒情緒,機器人音樂盒

本堂課主要探討讓機器人發出音樂,
以及元宇宙時事討論。


硬體接線,今天把開關接到D2,蜂鳴器接到D9


無源蜂鳴器


按鍵輸入


製作音樂盒
開新檔,自己建立標頭檔 notes.h

#define C0  18
#define CS0 17
#define D0  18
#define DS0 19
#define E0  21
#define F0  22
#define FS0 23
#define G0  25
#define GS0 26
#define A0  28
#define AS0 29
#define B0  31
#define C1  33
#define CS1 35
#define D1  37
#define DS1 39
#define B0  31
#define C1  33
#define CS1 35
#define D1  37
#define DS1 39
#define E1  41
#define F1  44
#define FS1 46
#define G1  49
#define GS1 52
#define A1  55
#define AS1 58
#define B1  62
#define C2  65
#define CS2 69
#define D2  73
#define DS2 78
#define E2  82
#define F2  87
#define FS2 93
#define G2  98
#define GS2 104
#define A2  110
#define AS2 117
#define B2  123
#define C3  131
#define CS3 139
#define D3  147
#define DS3 156
#define E3  165
#define F3  175
#define FS3 185
#define G3  196
#define GS3 208
#define A3  220
#define AS3 233
#define B3  247
#define C4  262
#define CS4 277
#define D4  294
#define DS4 311
#define E4  330
#define F4  349
#define FS4 370
#define G4  392
#define GS4 415
#define A4  440
#define AS4 466
#define B4  494
#define C5  523
#define CS5 554
#define D5  587
#define DS5 622
#define E5  659
#define F5  698
#define FS5 740
#define G5  784
#define GS5 831
#define A5  880
#define AS5 932
#define B5  988
#define C6  1047
#define CS6 1109
#define D6  1175
#define DS6 1245
#define E6  1319
#define F6  1397
#define FS6 1480
#define G6  1568
#define GS6 1661
#define A6  1760
#define AS6 1865
#define B6  1976
#define C7  2093
#define CS7 2217
#define D7  2349
#define DS7 2489
#define E7  2637
#define F7  2794
#define FS7 2960
#define G7  3136
#define GS7 3322
#define A7  3520
#define AS7 3729
#define B7  3951
#define C8  4186
#define CS8 4435
#define D8  4699
#define DS8 4978
#define E8  5274
#define F8  5588
#define FS8 5920
#define G8  6272
#define GS8 6645
#define A8  7040
#define AS8 7459
#define B8  7902

接著主程式放入小星星

#include "notes.h"

int buzzerPin=9;

int note[]={C5, C5, G5, G5, A5, A5, G5, 0, F5, F5, E5, E5, D5, D5, C5, 0,
            G5, G5, F5, F5, E5, E5, D5, 0, G5, G5, F5, F5, E5, E5, D5, 0,
            C5, C5, G5, G5, A5, A5, G5, 0, F5, F5, E5, E5, D5, D5, C5, 0};
         
int duration[]={4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
                4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
                4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4};

void setup() {
 pinMode(buzzerPin, OUTPUT);
 }

void loop() {
  littleStar(buzzerPin, sizeof(note)/sizeof(int));
  }

void littleStar(int pin, int count) {
  for (int i=0; i<count; i++) {
    int d=1000/duration[i];
    tone(pin, note[i], d);
    int p=d * 1.3;
    delay(p);
    noTone(pin);
    }
  delay(2000);
  }

挑戰1

按鍵後撥放小星星


挑戰2

放2首歌


時事探討 : 元宇宙

資訊界日新月異,站在潮流的風口上,才能抓準趨勢

您可能也會喜歡…

發佈留言