The second and third while
and if
are not working but the first while
and if
do work. Am I doing something wrong?
#include <DFMiniMp3.h>
int sw1 = 3;
int sw2 = 4;
int sw3 = 5;
class Mp3Notify
{
public:
static void OnError(uint16_t errorCode)
{
// see DfMp3_Error for code meaning
Serial.println();
Serial.print("Com Error ");
Serial.println(errorCode);
}
static void OnPlayFinished(uint16_t globalTrack)
{
Serial.println();
Serial.print("Play finished for #");
Serial.println(globalTrack);
}
static void OnCardOnline(uint16_t code)
{
Serial.println();
Serial.print("Card online ");
Serial.println(code);
}
static void OnCardInserted(uint16_t code)
{
Serial.println();
Serial.print("Card inserted ");
Serial.println(code);
}
static void OnCardRemoved(uint16_t code)
{
Serial.println();
Serial.print("Card removed ");
Serial.println(code);
}
};
DFMiniMp3<HardwareSerial, Mp3Notify> mp3(Serial); //Create the UART connection to the module
void setup()
{
//3 push buttons with pullups
pinMode(sw1, INPUT); //Define each button as input with pullup
pinMode(sw2, INPUT);
pinMode(sw3, INPUT);
digitalWrite (sw1, HIGH); //12 Votls supply set to off state
digitalWrite (sw2, HIGH); //5 Votls supply set to off state
digitalWrite (sw3, HIGH); //3 Votls supply set to off state
Serial.begin(9600);
mp3.begin(); //Start communication with the DFplayer module
uint16_t volume = mp3.getVolume(); //Get actual volume
mp3.setVolume(30); //Set new volum (max is 30)
uint16_t count = mp3.getTotalTrackCount(); //Get the total tracks on the SD card in case we want to sue this later...
}
//just a fucntion that we use to create delays in "ms"
//without using the delay() function
void waitMilliseconds(uint16_t msWait)
{
uint32_t start = millis();
while ((millis() - start) < msWait)
{
// calling mp3.loop() periodically allows for notifications
// to be handled without interrupts
mp3.loop();
delay(1);
}
}
void loop()
{
if (digitalRead(sw1) == LOW)
{
mp3.playMp3FolderTrack(1); // Play audio track 0001
waitMilliseconds(5000); // 1s of delay
while (digitalRead(sw1) == HIGH); // do nothing until state changes
}
if (digitalRead(sw2) == LOW)
{
mp3.playMp3FolderTrack(2); // Play audio track 0002
waitMilliseconds(5000); // 1s of delay
while (digitalRead(sw2) == HIGH); // do nothing until state changes
}
if (digitalRead(sw3) == LOW)
{
mp3.playMp3FolderTrack(3); // Play audio track 0002
waitMilliseconds(5000); // 1s of delay
while (digitalRead(sw3) == HIGH); // do nothing until state changes
}
}
-
Am I doing something wrong? - if the alternative is that while/if statements do not work, then sure, you're doing something wrong. Please format your code properly and someone will help you find the mistake.Sim Son– Sim Son2020年11月07日 10:49:07 +00:00Commented Nov 7, 2020 at 10:49
-
@SimSon, from the basic concept it looks fine. I need someone to help me with this.Habib Anwari– Habib Anwari2020年11月07日 10:52:32 +00:00Commented Nov 7, 2020 at 10:52
-
Thanks for formating the code. The code just behaves differently than you expect, but those statements definitely work. What makes you believe they don't? Please add your observations and expectations into the question.Sim Son– Sim Son2020年11月07日 10:58:30 +00:00Commented Nov 7, 2020 at 10:58
-
1@ocrdu, yes that is fine. but the second "if" statement doesn't function.Habib Anwari– Habib Anwari2020年11月07日 11:14:40 +00:00Commented Nov 7, 2020 at 11:14
-
1@ocrdu, no, not working. Just the first if statement works.Habib Anwari– Habib Anwari2020年11月07日 11:26:43 +00:00Commented Nov 7, 2020 at 11:26
1 Answer 1
After pressing sw1
, the first if
works. After releasing sw1
(it's a push button), the line while (digitalRead(sw1) == HIGH);
will make the code just sit there forever without continuing with the rest of the code until you press sw1
again, which will be caught by the first if
unless you release 'sw1' inhumanly fast.
So, once you have pressed and released sw1
, the rest of the code with the next two if
s will never run.
-
your last statement looks right, but somehow it can't figure out the problem. If I held the sw1 down and press the sw2 then sw2 start working. Now only sw2 functions sw1 not working. The switching locks to sw2. but when I reset everything it goes back to sw1.Habib Anwari– Habib Anwari2020年11月07日 13:53:39 +00:00Commented Nov 7, 2020 at 13:53
-
The while loops block the execution. Maybe it will work as you intended if you remove them and make the delays a bit shorter.ocrdu– ocrdu2020年11月07日 16:13:40 +00:00Commented Nov 7, 2020 at 16:13
-
I use a DFmini player. It works well if the button is press and release. But in my case, the button will need to be held down. the problem with holding the button down is that the MP3 get played over and over. I want it to play only for once if the pin is high and or button is held down for a long time. If i remove the "while" then it will play over and over again. does not stop.Habib Anwari– Habib Anwari2020年11月07日 16:32:33 +00:00Commented Nov 7, 2020 at 16:32
-
If you use a blocking delay, no button will respond during the delay time, and only repeat after the delay time. Or, debounce the buttons (you can look that up) and use a long debounce time, basically debouncing the push button and the human too.ocrdu– ocrdu2020年11月07日 16:40:02 +00:00Commented Nov 7, 2020 at 16:40
Explore related questions
See similar questions with these tags.