#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Paramètres de l'écran OLED
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Broches des boutons
const int playPauseButtonPin = 12;
const int nextButtonPin = 14;
const int prevButtonPin = 27;
// Variables pour le lecteur MP3 simulé
bool isPlaying = false;
int trackIndex = 0;
unsigned long trackStartTime = 0; // Heure de début de la lecture de la piste
unsigned long elapsedTime = 0; // Temps écoulé en millisecondes
const char* tracks[] = {"Piste 1", "Piste 2", "Piste 3", "Piste 4"};
const int numTracks = sizeof(tracks) / sizeof(tracks[0]);
void setup() {
// Initialisation de l'écran OLED
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("Erreur lors de l'initialisation de l'écran OLED");
while (true);
}
display.clearDisplay();
// Initialisation des boutons
pinMode(playPauseButtonPin, INPUT_PULLUP);
pinMode(nextButtonPin, INPUT_PULLUP);
pinMode(prevButtonPin, INPUT_PULLUP);
// Initialisation du moniteur série
Serial.begin(115200);
// Affichage initial
updateDisplay();
}
void loop() {
// Vérification des boutons avec anti-rebond simple
if (digitalRead(playPauseButtonPin) == LOW) {
togglePlayPause();
delay(200); // Délai pour anti-rebond
}
if (digitalRead(nextButtonPin) == LOW) {
nextTrack();
delay(200); // Délai pour anti-rebond
}
if (digitalRead(prevButtonPin) == LOW) {
prevTrack();
delay(200); // Délai pour anti-rebond
}
// Mise à jour de l'affichage du temps de lecture toutes les secondes
if (isPlaying) {
updateDisplay();
delay(1000);
}
}
void updateDisplay() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
// Afficher le nom de la piste
display.setCursor(0, 0);
display.println("Lecteur MP3");
display.setCursor(0, 20);
display.print("Piste: ");
display.println(tracks[trackIndex]);
// Afficher l'état de lecture
display.setCursor(0, 40);
display.print("Etat: ");
display.println(isPlaying ? "Lecture" : "Pause");
// Calcul du temps écoulé
unsigned long currentTime = millis();
if (isPlaying) {
elapsedTime = currentTime - trackStartTime;
}
int elapsedSeconds = (elapsedTime / 1000) % 60;
int elapsedMinutes = (elapsedTime / 1000) / 60;
// Afficher le temps de lecture au format MM:SS
display.setCursor(0, 55);
display.print("Temps: ");
if (elapsedMinutes < 10) display.print("0");
display.print(elapsedMinutes);
display.print(":");
if (elapsedSeconds < 10) display.print("0");
display.print(elapsedSeconds);
display.display();
}
void nextTrack() {
trackIndex = (trackIndex + 1) % numTracks;
resetTrackTime();
updateDisplay();
}
void prevTrack() {
trackIndex = (trackIndex - 1 + numTracks) % numTracks;
resetTrackTime();
updateDisplay();
}
void togglePlayPause() {
if (isPlaying) {
// Mise en pause
isPlaying = false;
} else {
// Lecture
isPlaying = true;
trackStartTime = millis() - elapsedTime; // Reprendre là où on s'était arrêté
}
updateDisplay();
}
void resetTrackTime() {
elapsedTime = 0;
trackStartTime = millis();
}