#include <Arduino.h>
#include <TM1637Display.h> // Sertakan pustaka TM1637
// Tentukan pin GPIO untuk tombol dan komponen
#define DO_PIN 15
#define RE_PIN 2
#define MI_PIN 4
#define FA_PIN 16 // RX2
#define SO_PIN 17 // TX2
#define LA_PIN 5
#define SI_PIN 18
#define DO2_PIN 19
#define BUZZER_PIN 13
#define RECORD_BUTTON_PIN 14
#define LED_PIN 21
#define PLAY_BUTTON_PIN 12
#define TM1637_CLK 26 // Pin CLK untuk TM1637
#define TM1637_DIO 27 // Pin DIO untuk TM1637
int recordedNotes[100]; // Array untuk menyimpan catatan yang direkam
int noteCount = 0; // Jumlah catatan yang direkam
bool isRecording = false;
// Buat objek TM1637Display
TM1637Display display(TM1637_CLK, TM1637_DIO);
unsigned long playStartTime = 0; // Waktu mulai pemutaran
unsigned long playDuration = 0; // Durasi pemutaran dalam milidetik
void setup() {
pinMode(DO_PIN, INPUT_PULLUP);
pinMode(RE_PIN, INPUT_PULLUP);
pinMode(MI_PIN, INPUT_PULLUP);
pinMode(FA_PIN, INPUT_PULLUP);
pinMode(SO_PIN, INPUT_PULLUP);
pinMode(LA_PIN, INPUT_PULLUP);
pinMode(SI_PIN, INPUT_PULLUP);
pinMode(DO2_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(RECORD_BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
pinMode(PLAY_BUTTON_PIN, INPUT_PULLUP);
// Inisialisasi tampilan TM1637
display.setBrightness(0x0A); // Atur kecerahan tampilan (sesuaikan sesuai kebutuhan)
display.showNumberDec(0, false); // Tampilkan "0" awal
}
void loop() {
// Periksa apakah tombol rekam ditekan
if (digitalRead(RECORD_BUTTON_PIN) == LOW) {
isRecording = !isRecording;
digitalWrite(LED_PIN, isRecording ? HIGH : LOW);
delay(500); // Debounce
}
if (isRecording) {
recordNotes();
} else {
if (digitalRead(PLAY_BUTTON_PIN) == LOW) {
playStartTime = millis(); // Catat waktu mulai
playNotes();
playDuration = millis() - playStartTime; // Hitung durasi
displayDuration(playDuration);
}
}
}
void displayDuration(unsigned long duration) {
int seconds = duration / 1000;
display.showNumberDecEx(seconds, 0b01000000, true);
delay(1000); // Tunda untuk menampilkan durasi sejenak (sesuaikan sesuai kebutuhan)
display.showNumberDec(0, false); // Hapus tampilan
}
void playNotes() {
if (digitalRead(PLAY_BUTTON_PIN) == LOW) {
for (int i = 0; i < noteCount; i++) {
int note = recordedNotes[i];
playNote(note);
}
}
}
void recordNotes() {
if (digitalRead(DO_PIN) == LOW) {
recordAndPlayNote(261.63); // DO
}
if (digitalRead(RE_PIN) == LOW) {
recordAndPlayNote(293.66); // RE
}
if (digitalRead(MI_PIN) == LOW) {
recordAndPlayNote(329.63); // MI
}
if (digitalRead(FA_PIN) == LOW) {
recordAndPlayNote(349.23); // FA
}
if (digitalRead(SO_PIN) == LOW) {
recordAndPlayNote(392.00); // SO
}
if (digitalRead(LA_PIN) == LOW) {
recordAndPlayNote(440.00); // LA
}
if (digitalRead(SI_PIN) == LOW) {
recordAndPlayNote(493.88); // SI
}
if (digitalRead(DO2_PIN) == LOW) {
recordAndPlayNote(523.25); // DO (Oktav 2)
}
}
void recordAndPlayNote(float frequency) {
recordedNotes[noteCount] = frequency;
noteCount++;
playNote(frequency);
delay(300); // Sesuaikan durasi sesuai kebutuhan
}
void playNote(float frequency) {
if (frequency > 0) {
tone(BUZZER_PIN, frequency);
delay(500); // Sesuaikan durasi sesuai kebutuhan
noTone(BUZZER_PIN);
}
}