Here i want to break while loop after 5 minutes,
As i am using button to pause my df player and again pushing the same button gets play.
Using while true statement
But i want to break with a 5 minute timer and also if button pressed , any one option which ever is first.
But inside while loop no timer works.
Any one can help.
Your topic was MOVED to its current forum category as it is more suitable than the original
Please post your full sketch, using code tags when you do
You can post code by using this method that adds the code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps
- press Ctrl-T for autoformatting your code
- do a rightclick with the mouse and choose "copy for forum"
- paste clipboard into write-window of a posting
best regards Stefan
For extra information and examples look at Using millis() for timing. A beginners guide and Several things at the same time
Post the code you have and we can help.
@gaganranchi
Would the following hints be helpful? You have to write the missing codes.
void setup()
{
Serial.begin(9600);
pinMode(2, INPUT_PULLUP); //connect button with DPin-2
unsigned long prMillis = millis();
do
{
//write here codes to complete the program
}
while (millis() - prMillis < 5000); //5-sec loop time
Serial.print("Elapsed time: ");
Serial.print(millis()/1000); Serial.println(" sec");
}
void loop()
{
Serial.println("Out of while loop of setup() function.");
delay(1000);
}
Looks a lot like delay(5000):
Hi gagan,
if you post your complete sketch then users here can explain how this is coded based on your sketch.
You can post code by using this method that adds the code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps
- press Ctrl-T for autoformatting your code
- do a rightclick with the mouse and choose "copy for forum"
- paste clipboard into write-window of a posting
best regards Stefan
Look again - the proposed code was
do {
//write here codes to complete the program
}
while (millis() - prMillis < 5000); //5-sec loop time
Ok, I’ll wear that.
Looks like delay(); but, not blocking like delay()? The codes under do clause (for example: checking if button is pressed) are still executed while the content of millisCounter is being monitored.
#include <DFPlayer_Mini_Mp3.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
SoftwareSerial DFplayer(2, 3);
# define Start_Byte 0x7E
# define Version_Byte 0xFF
# define Command_Length 0x06
# define End_Byte 0xEF
# define Acknowledge 0x00
# define ACTIVATED LOW
int song_num;
int volume = 15;
int IsPlaying = 9;
const int volumeUP = A1;
const int volumeDOWN = A2;
int Pause = 7;
int Play = 8;
const int led = 6;
bool busy = 0;
void setup() {
// put your setup code here, to run once:
pinMode(IsPlaying, INPUT_PULLUP);
pinMode(Pause, INPUT_PULLUP);
pinMode(Play, INPUT_PULLUP);
pinMode(volumeUP, INPUT);
pinMode(volumeDOWN, INPUT);
digitalWrite(volumeUP, HIGH);
digitalWrite(volumeDOWN, HIGH);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
lcd.backlight();
lcd.begin(16, 2);
DFplayer.begin(9600);
exe_cmd(0x3F, 0, 0);
lcd.setCursor(0, 0);
lcd.print("HAVE A NICE DAY");
lcd.setCursor(0, 1);
lcd.print("MUSIC PLAYER*");
delay(2000);
}
void loop() {
Status();
if (digitalRead(Play) == LOW) {
delay(250);
exe_cmd(0x03, 0, 5);
}
if (digitalRead(Pause) == LOW) {
delay(250);
while (true) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MP3 PALYER");
lcd.setCursor(0, 1);
lcd.print(" MUSIC PAUSED");
exe_cmd(0x0E, 0, 0); //pause COMMAND
digitalWrite(led, HIGH); //flash lights on indicate pause
lcd.noDisplay();
delay(200);
lcd.display();
delay(700);
if (digitalRead(Pause) == LOW) {
delay(350);
exe_cmd(0x0D, 0, 0); //resume play
digitalWrite(led, LOW); //flash lights off
lcd.clear();
break;
}
}
}
if (digitalRead(volumeUP) == LOW) {
delay(250);
while (true) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print ("VOLUME ++");
lcd.setCursor(10, 0);
lcd.print(volume);
lcd.setCursor(0, 1);
lcd.print("MUSIC PLAYING");
volup();
return;
}
}
if (digitalRead(volumeDOWN) == LOW) {
delay(250);
while (true) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print ("VOLUME --");
lcd.setCursor(10, 0);
lcd.print(volume);
lcd.setCursor(0, 1);
lcd.print("MUSIC PLAYING");
voldn();
return;
}
}
if (busy == 1)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MP3 PALYER");
lcd.setCursor(0, 1);
lcd.print("MUSIC PLAYING");
delay(500);
}
if (busy == 0)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MP3 PALYER");
lcd.setCursor(0, 1);
lcd.print("MUSIC STOPED");
delay(250);
}
}
void exe_cmd(byte CMD, byte Par1, byte Par2) {
word checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
byte Command_line[10] = {Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge, Par1, Par2, highByte(checksum), lowByte(checksum), End_Byte};
for (byte x = 0; x < 10; x++)
{
DFplayer.write(Command_line[x]);
}
}
void volup() {
volume = volume + 1;
if (volume == 21) {
volume = 20;
}
exe_cmd(0x06, 0, volume);
delay(250);
}
void voldn() {
volume = volume - 1;
if (volume == 9) {
volume = 10;
}
exe_cmd(0x06, 0, volume);
delay(250);
}
void Status() {
if (digitalRead(IsPlaying) == LOW) {
busy = 0;
}
if (digitalRead(IsPlaying) == HIGH) {
busy = 1;
}
}
here is my complete code, i just want if pause the player and i forgot to resume this should automatically start playing after 5 minutes of paused, or i press play key before 5 minutes it start playing. this will make a sense that many time we pause the player and forget it and it is waiting and wasting power.
I write in my own words what I think you want to have:
if player is paused start playing again after 5 minutes
as a signal "hey I was paused switch me really off"
or if player is paused and you press a button start playing again.
first button-press pause
second button-press play
next button-press pause
next button-press play.....
everytime the player is paused start the 5-minute countdown new
Is this correct?
best regards Stefan
yes only using a single push button
you have soooooo many delay() calls in there that the user interface will suck...
I would suggest to rewrite this as a state machine
I second that.
If you write a description of the
pure
functionality
I will support you to re-write the code as a state-machine.
pure functionality means
by all means and in all cases
avoid code-snippets
describe everything in normal words
describe the functionality like your grandma which knows nothing about programming would describe what she can see watching another person using your player
it would start with something like
if power is switched on display shows a welcome message
if he presses the ???-Button the music starts to play (insert a self-explaining name for the ???)
etc.
best regards Stefan
These while(true){ .... return;} patterns are no different than .... return;
I'd start by dumping all the whiles and most of the delays(), and use the digitalRead(IsPlaying) as a state variable to toggle the sense of the "play" button like this:
if (digitalRead(Play) == LOW) {
if (digitalRead(isPlaying) == LOW) // .. DF Busy playing then start playing
{
exe_cmd(0x0E, 0, 0); //pause COMMAND
} else { // it's not Busy, so start playing
exe_cmd(0x03, 0, 5); //play COMMAND
}
}
Hi,
i am making a mp3 player using df mini mp3 sd card player,
its a simple player with play key, pause key, volume up key, volume down key, one led output indicating player is paused, one lcd i2c displays welcome note and when playing will display song title, as i am facing only one issue is every mp3 player remains pause until we press the key again, and this how if we got engaged in any work, we forget to resume and the player is running and draining the battery.
i have given the part of code. as i am planning to attach more function with this player, but i have stuck in this resume key, so first of all want to clear this, after this i will complete the code for my player.