DFPlayer mini play next track with active switch,

got this code from member sonny22(thanks), to almost work. I build this for my slot car drag racing track, so when the car stages and blocking the sensor one, track one is playing. When car is ready to launch and passes over switch two, track two is playing. At this moment switch two starts to play track two when I first activate and then deactivate sensor two. Any suggestions how to approach a solution?

type or paste code here#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#define ACTIVATED LOW

int buttonNext = 5;
int buttonPause = 4;
int buttonPrevious = 3;
bool switch1Pressed = false;
bool switch2Pressed = false;

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;

void printDetail(uint8_t type, int value);

void setup() {

    pinMode(buttonPause, INPUT);
    digitalWrite(buttonPause, HIGH);
    pinMode(buttonNext, INPUT);
    digitalWrite(buttonNext, HIGH);
    pinMode(buttonPrevious, INPUT);
    digitalWrite(buttonPrevious, HIGH);

    //checking if the dfplay is connected and ready to play
    mySoftwareSerial.begin(9600);
    Serial.begin(115200);

    Serial.println();
    Serial.println(F("DFRobot DFPlayer Mini Demo"));
    Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));

    if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with player.
        Serial.println(F("Unable to begin:"));
        Serial.println(F("1.Please recheck the connection!"));
        Serial.println(F("2.Please insert the SD card!"));

        while (true);
    }
    Serial.println(F("DFPlayer Mini online."));

    delay(500);
}

// routine to play a specific song
void playTrackX(byte x) {

    mySoftwareSerial.write((byte)0x7E);
    mySoftwareSerial.write((byte)0xFF);
    mySoftwareSerial.write((byte)0x06);
    mySoftwareSerial.write((byte)0x03);
    mySoftwareSerial.write((byte)0x00);
    mySoftwareSerial.write((byte)0x00);
    mySoftwareSerial.write((byte)x);
    mySoftwareSerial.write((byte)0xEF);
}

void loop() {

    // button Next is pressed play song 1
    if (digitalRead(buttonNext) == ACTIVATED) {
        // Om switch 1 precis blev nedtryckt, starta spår 1
        if (!switch1Pressed) {
            playTrackX(1);
        }
        // Uppdatera switch 1s status
        switch1Pressed = true;
    }
    else {
        // Om switch 1 släpps, sluta spåret 1
        if (switch1Pressed) {
            // Lägg till stoppfunktion för spår 1 här
            // Exempel: myDFPlayer.stop();
        }
        // Återställ switch 1s status
        switch1Pressed = false;
    }

    // button Previous is pressed play song 2
    if (digitalRead(buttonPrevious) == ACTIVATED) {
        // Om switch 2 precis blev nedtryckt, starta spår 2
        if (!switch2Pressed) {
            playTrackX(2);
        }
        // Uppdatera switch 2s status
        switch2Pressed = true;
    }
    else {
        // Om switch 2 släpps, sluta spåret 2
        if (switch2Pressed) {
            // Lägg till stoppfunktion för spår 2 här
            // Exempel: myDFPlayer.stop();
        }
        // Återställ switch 2s status
        switch2Pressed = false;
    }

    // button Pause is pressed play song 3
    if (digitalRead(buttonPause) == ACTIVATED) {
        playTrackX(3);
    }
}

You don't want to know IF your buttons are pressed, you want to know WHEN your buttons are pressed (or switches, in this case). Look at the State Change Detection example in the IDE (File->examples->02.Digital->State Change Detection) to learn how.

You should also consider making your sketch like a state machine and keep track of which state you are in (WAITING, STAGED, STARTED, etc.) so you can then properly react to any switches given the current state.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.