#include <FS.h>
#include <SPIFFS.h>
#include <MIDI.h>
#include <TM1637Display.h>
// Pin GPIO untuk masing-masing klakson
const int hornPins[6] = { 5, 18, 19, 21, 22, 23 };
// Pin GPIO untuk tombol
const int playButtonPin = 4;
const int nextButtonPin = 12;
const int prevButtonPin = 13;
// Pin untuk TM1637
const int CLK = 14;
const int DIO = 27;
TM1637Display display(CLK, DIO);
// Setup MIDI
MIDI_CREATE_DEFAULT_INSTANCE();
// Variabel untuk mengelola file MIDI
File midiFile;
const int totalSongs = 30;
String midiFiles[totalSongs];
int currentSongIndex = 0;
bool isPlaying = false;
// Fungsi untuk menyalakan/mematikan klakson
void controlHorn(byte channel, byte note, byte velocity) {
// Asumsikan not angka 1-6 sesuai dengan MIDI note 60-65
int hornIndex = note - 60;
if (hornIndex >= 0 && hornIndex < 6) {
if (velocity > 0) {
digitalWrite(hornPins[hornIndex], HIGH); // Nyalakan klakson
} else {
digitalWrite(hornPins[hornIndex], LOW); // Matikan klakson
}
}
}
void setup() {
Serial.begin(115200);
// Inisialisasi pin klakson sebagai output
for (int i = 0; i < 6; i++) {
pinMode(hornPins[i], OUTPUT);
digitalWrite(hornPins[i], LOW);
}
// Inisialisasi pin tombol sebagai input
pinMode(playButtonPin, INPUT_PULLUP);
pinMode(nextButtonPin, INPUT_PULLUP);
pinMode(prevButtonPin, INPUT_PULLUP);
// Inisialisasi TM1637 display
display.setBrightness(0x0f);
display.showNumberDec(currentSongIndex + 1, true); // Menampilkan nomor lagu (mulai dari 1)
// Inisialisasi SPIFFS
if (!SPIFFS.begin(true)) {
Serial.println("An error has occurred while mounting SPIFFS");
return;
}
// Inisialisasi MIDI
MIDI.begin(MIDI_CHANNEL_OMNI);
MIDI.setHandleNoteOn(controlHorn);
MIDI.setHandleNoteOff(controlHorn);
// Mengisi array nama file MIDI
for (int i = 0; i < totalSongs; i++) {
midiFiles[i] = "/song" + String(i + 1) + ".mid";
}
}
void loop() {
// Baca tombol play
if (digitalRead(playButtonPin) == LOW) {
delay(200); // Debouncing
if (!isPlaying) {
playSong(currentSongIndex);
isPlaying = true;
} else {
stopSong();
isPlaying = false;
}
}
// Baca tombol next
if (digitalRead(nextButtonPin) == LOW) {
delay(200); // Debouncing
currentSongIndex = (currentSongIndex + 1) % totalSongs;
display.showNumberDec(currentSongIndex + 1, true); // Menampilkan nomor lagu (mulai dari 1)
if (isPlaying) {
playSong(currentSongIndex);
}
}
// Baca tombol prev
if (digitalRead(prevButtonPin) == LOW) {
delay(200); // Debouncing
currentSongIndex = (currentSongIndex - 1 + totalSongs) % totalSongs;
display.showNumberDec(currentSongIndex + 1, true); // Menampilkan nomor lagu (mulai dari 1)
if (isPlaying) {
playSong(currentSongIndex);
}
}
// Baca file MIDI
if (midiFile && midiFile.available()) {
byte data = midiFile.read();
MIDI.read(data);
}
}
void playSong(int index) {
if (midiFile) {
midiFile.close();
}
midiFile = SPIFFS.open(midiFiles[index], "r");
if (!midiFile) {
Serial.println("Failed to open file");
return;
}
}
void stopSong() {
if (midiFile) {
midiFile.close();
}
for (int i = 0; i < 6; i++) {
digitalWrite(hornPins[i], LOW);
}
}