Go Down

Topic: MP3 player multitasking problem (Read 292 times) previous topic - next topic

nordmende_parsifal

Dec 11, 2018, 09:41 pm Last Edit: Dec 11, 2018, 09:59 pm by nordmende_parsifal
Hi,

we're building an MP3 player using Arduino, using a rotary encoder to skip back and forth through tracks and a potentiometer to regulate the volume. Our issue is that we can't seem to get both of those inputs to be recognized by the board at the same time. If we use only the rotary encoder, it works fine. But if we add the potentiometer, then it seems to be prioritized and we can't skip back and forth through the tracks anymore while the volume adjustments work. We also plan to use force sensitive resistors to choose between file folders later in the projects, so we really need the multitasking of the program to work.

Here is our current code:
Code: [Select]
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

int val;
int encoder0PinA = 3;
int encoder0PinB = 4;
int encoder0PinBUSY = 2;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int encoder0PinAState = 0;
int encoder0PinALaststate = 0;
int encoder0PinBState = 0;
int encoder0PinBLaststate = 0;
int n = LOW;
const int volumePin = A0;
int volumeState = 0;

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);

void setup() {
  pinMode (encoder0PinA, INPUT);
  pinMode (encoder0PinB, INPUT);
  pinMode (encoder0PinBUSY, INPUT);
  Serial.begin (9600);
  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 mp3.
    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);
    {
      delay(0);
    }
  }
  Serial.println(F("DFPlayer Mini online."));
 
  myDFPlayer.volume(15);  //Set volume value. From 0 to 30
  myDFPlayer.play(1);  //Play the first mp3
  Serial.println ("playing track 1");

  encoder0PinALaststate = digitalRead(encoder0PinA);
  delay(1000);
 
}

void loop() {
 
  encoder0PinAState = digitalRead(encoder0PinA); // Reads the "current" state of the encoder0PinA
                                                 // If the previous and the current state of the encoder0PinA are different, that means a Pulse has occured
  encoder0PinBState = digitalRead(encoder0PinB);

  const int volumePin = A0;
  byte volumeState = 254;

  int read = analogRead (volumePin);
  byte state = map (read, 0, 1023, 20, 5);

  if (state < volumeState - 1 || state > volumeState + 1) {
     volumeState = state;
     myDFPlayer.volume(volumeState);
     Serial.print (volumePin);
     Serial.print ("volume");
     Serial.println (volumeState);
     Serial.println(myDFPlayer.readVolume());
    }

  if (digitalRead(encoder0PinBUSY) == HIGH) {
     Serial.println ("Track is over");
     myDFPlayer.next();
     delay(50);
     myDFPlayer.next();
     Serial.println ("Skipping to next track");
     delay(100);
  }

   if (encoder0PinAState != encoder0PinALaststate) {
     if (digitalRead(encoder0PinB) != encoder0PinAState) { // If the encoder0PinB state is different to the encoder0PinA state, that means the encoder is rotating clockwise
       myDFPlayer.next();
       delay(50);
       myDFPlayer.next();
       Serial.println ("next");
     } else {
       myDFPlayer.previous();
       delay(50);
       myDFPlayer.previous();
       Serial.println ("prev");
     }

    Serial.println (encoder0Pos);
    Serial.println ("/");
    Serial.flush();
    delay(1000);
 
  }
  encoder0PinBState = encoder0PinBLaststate;
  encoder0PinALast = n;

}


I hope someone can help us out :)

MarkT

You need to provide links to datasheets for all the hardware you are using, we can't magically
devine what you have!!

Code: [Select]

  pinMode (encoder0PinA, INPUT);
  pinMode (encoder0PinB, INPUT);
  pinMode (encoder0PinBUSY, INPUT);

What's this BUSY input then?

Code: [Select]

  Serial.begin (9600);
  mySoftwareSerial.begin(9600);
  //Serial.begin(115200);

Why comment out the 115200 baud rate - you problem is likely the long delay in
processing volume changes caused by printing loads of characters extremely slowly using 9600
baud - reduce those prints a lot, go back to 115200 baud for debug messages.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up