Hey!
So I am very stuck! I have two buttons on a board that I am looking to push for up and down based to scroll through the tracks. I am also trying to do it so that way if I want to change the "track" before the duration is complete the code does that as well. I am having an extremely difficult time with my code and it is quite a mess! I was told a StateChangeDetection would be good. In the code now, I can play the track but it just loops and the up and down buttons do not work. I am also trying to get the OLED to display each track as well as it scrolls up or down. Any help at all is appreciated! I have been working on this or way tooooooo long!
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Soundboard.h>
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define SFX_RST 9
#define SFX_ACT 8
#define BUTTON_UP_PIN 1
#define BUTTON_DOWN_PIN 3
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_Soundboard sfx(&Serial1, &Serial, SFX_RST);
const char *trackNames[] = {
"Level1.wav", // Track 0
"Level2.wav", // Track 1
"Level3.wav", // Track 2
"Level4.wav", // Track 3
"Level5.wav", // Track 4
"Level6.wav", // Track 5
"Level7.wav", // Track 6
"Level8.wav", // Track 7
"Level9.wav", // Track 8
"Level10.wav", // Track 9
"Level11.wav", // Track 10
"Level12.wav" // Track 11
};
uint8_t currentTrack = 0;
void setup()
{
Serial.begin(9600); // Main serial for debugging
Serial1.begin(9600); // Secondary serial for Soundboard communication
sfx.reset(); // Reset the Soundboard
pinMode(BUTTON_UP_PIN, INPUT_PULLUP);
pinMode(BUTTON_DOWN_PIN, INPUT_PULLUP);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
{
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
delay(2000); // Pause for 2 seconds
display.clearDisplay();
}
void loop()
{
// Check button states and scroll through tracks
if (digitalRead(BUTTON_UP_PIN) == LOW)
{
currentTrack++;
if (currentTrack > 11)
{
currentTrack = 11;
}
sfx.stop();
}
/*
if (currentTrack < sizeof(trackNames) / sizeof(trackNames[0]) - 1) {
currentTrack++;
} else {
currentTrack = 0;
}
*/
//delay(200); // Button debounce
//}
/*if (digitalRead(BUTTON_DOWN_PIN) == LOW)
{
currentTrack--;
/*
if (currentTrack > 0) {
currentTrack--;
} else {
currentTrack = sizeof(trackNames) / sizeof(trackNames[0]) - 1;
}
*/
//delay(200); // Button debounce
//}
// Clear the display
//display.clearDisplay();
// Print the current track name on the OLED display
/*display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print("Playing");
display.print(" ");
display.println(trackNames[currentTrack]);
display.println();
display.display(); // Show initial text
display.clearDisplay();
display.println("Playing");*/
Serial.print("Playing Track: ");
Serial.println(trackNames[currentTrack]);
sfx.playTrack(1);
//while(true)
//{
//}
// Play the current track
//playTrack(currentTrack);
//delay(95); // Adjust delay time as needed
}
void playTrack(uint8_t trackName)
{
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.clearDisplay();
display.println("Playing");
sfx.playTrack(trackName);