#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int buzzerPin = 8; // 蜂鳴器連接的引腳
const int button1Pin = 3; // 按鈕1連接的引腳
const int button2Pin = 4; // 按鈕2連接的引腳
const int button3Pin = 5; // 按鈕3連接的引腳
const int ledPin = 13; // LED連接的引腳
// 定義音符的頻率
const int note_C = 261;
const int note_D = 294;
const int note_E = 329;
const int note_F = 349;
const int note_G = 392;
const int note_A = 440;
const int note_B = 493;
// 設置初始音符為低音Do
int currentNote = note_C;
// 跟蹤是否已經播放過高音Do
bool highDoPlayed = false;
void setup() {
pinMode(buzzerPin, OUTPUT);
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
pinMode(button3Pin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
delay(2000);
display.clearDisplay();
}
void loop() {
// 讀取按鈕的按下狀態
int buttonState1 = digitalRead(button1Pin);
int buttonState2 = digitalRead(button2Pin);
int buttonState3 = digitalRead(button3Pin);
// 檢查按鈕的組合以決定播放的音階
if (buttonState1 == LOW && buttonState2 == LOW && buttonState3 == LOW) {
tone(buzzerPin, currentNote); // 播放當前音符
displayText("Do");
digitalWrite(ledPin, HIGH); // 點亮 LED
} else if (buttonState1 == LOW && buttonState2 == LOW && buttonState3 == HIGH) {
tone(buzzerPin, note_D); // Re
currentNote = note_D; // 更新當前音符
displayText("Re");
digitalWrite(ledPin, HIGH); // 點亮 LED
} else if (buttonState1 == LOW && buttonState2 == HIGH && buttonState3 == LOW) {
tone(buzzerPin, note_E); // Mi
currentNote = note_E; // 更新當前音符
displayText("Mi");
digitalWrite(ledPin, HIGH); // 點亮 LED
} else if (buttonState1 == LOW && buttonState2 == HIGH && buttonState3 == HIGH) {
if (!highDoPlayed) {
tone(buzzerPin, note_C * 2); // 高音Do
displayText("High Do");
digitalWrite(ledPin, HIGH); // 點亮 LED
highDoPlayed = true; // 標記已經播放過高音Do
}
} else if (buttonState1 == HIGH && buttonState2 == LOW && buttonState3 == LOW) {
tone(buzzerPin, note_F); // Fa
currentNote = note_F; // 更新當前音符
displayText("Fa");
digitalWrite(ledPin, HIGH); // 點亮 LED
} else if (buttonState1 == HIGH && buttonState2 == LOW && buttonState3 == HIGH) {
tone(buzzerPin, note_G); // So
currentNote = note_G; // 更新當前音符
displayText("So");
digitalWrite(ledPin, HIGH); // 點亮 LED
} else if (buttonState1 == HIGH && buttonState2 == HIGH && buttonState3 == LOW) {
tone(buzzerPin, note_A); // La
currentNote = note_A; // 更新當前音符
displayText("La");
digitalWrite(ledPin, HIGH); // 點亮 LED
} else {
// 如果沒有按下按鈕,則停止播放音樂並且 LED 將被關閉
noTone(buzzerPin);
digitalWrite(ledPin, LOW); // 關閉 LED
display.clearDisplay();
highDoPlayed = false; // 重設標記
}
}
void displayText(String text) {
display.setTextSize(3); // 設置文字大小為3
display.setTextColor(SSD1306_WHITE); // 設置文字顏色為白色
display.clearDisplay(); // 清空顯示
int16_t x = (display.width() - text.length() * 28) / 2; // 計算文字的水平位置以置中
int16_t y = (display.height() - 32) / 2; // 計算文字的垂直位置以置中
display.setCursor(x, y); // 設置文字的位置
display.println(text); // 顯示文字
display.display(); // 更新顯示
}