Jump to content
Electronics-Lab.com Community

Dfplayer with Arduino pushbutton and led fading


abhishek gyani

Recommended Posts

https://drive.google.com/file/d/1-7O8QLI2ADPy9g09HWJkCtybQSHkCSO9/view?usp=drivesdk

 

 

Need to combines these two programs so that when i press pushbutton, the dfplayer will play music and also the led starts fading,

 

Im new to Arduino coding and can understand basic and regular programs, but unable to add led fading code into the execute_cmd function, can anyone help me?

Link to comment
Share on other sites


I am not sure what you need but here is code that fades a LED up and down. It does not block your code by using Delay(), You can paste it into your Loop{} or call it as a function. You may wish to play with the int period value and the +/-5 value. Do note which i/o pins you can use with this PWM code.

//use pins 3, 5, 6, 9, 10, and 11 on the UNO
const int ledPin = 3; //whichevery pin you choose Do not forget the resistor!
int period =   10; //how fast to fade the LED:100 = 1/10 sec. etc
unsigned long time_now = 0; //have not read the time yet
int brightness = 0;  //start with LED off
boolean IncreaseBrightness = true; //start with increasing brightness

 void setup() 
 {
 
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600); //you may have this; used here for testing
 }

void loop()
 {
  // non-blocking delay 
  if(millis() > time_now + period)  //get current time and compare
  {
     time_now = millis(); //for next time
    //fade LED up or down
    if(IncreaseBrightness) 
    {
      analogWrite(ledPin, brightness +=5 );
      if(brightness >= 255) IncreaseBrightness = false; //max ouput to LED = 255
	  }
    else   //must be decreasing brightness
    {
       analogWrite(ledPin, brightness -=5 );
	     if(brightness <= 0 ) IncreaseBrightness = true; //min ouput to LED = 0
    }
   }
    //some code...testing displays values.....
    Serial.print("brightness "); Serial.println(brightness);
 }  

 

 

Link to comment
Share on other sites

What does not work? The LED fade or the mp3  player?

What do you wish the push button to do?

A: Push the button once and the LED fades on and off until you push it again.

B: The LED fades on and off while you hold the button down. Releasing it the fading LED stops.

 

Link to comment
Share on other sites

  • 2 months later...


Hello!

Combining the functionalities of playing music on a DFPlayer and controlling an LED to fade in and out with an Arduino can be quite straightforward. Since you're already familiar with basic Arduino coding, you're off to a great start. Here's a basic outline to integrate LED fading into your existing setup:

Setup the LED Pin: First, ensure you have an LED connected to a PWM (Pulse Width Modulation) capable pin on your Arduino. PWM pins allow for analog output, which is needed for fading effects.

Include Necessary Variables: In your code, define variables for the LED pin and for controlling the fade effect (e.g., int ledPin = 9; // PWM pin and int brightness = 0;).

Modify the execute_cmd Function: In your execute_cmd function, where you control the DFPlayer, include a loop to control the LED's brightness. Use analogWrite(ledPin, brightness) to set the LED's brightness. For fading, you can increment or decrement the brightness variable and use delay() to control the speed of the fade.

Fading Loop: A simple fading loop looks like this:

for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
}

// fade out from max to min in increments of 5 points:
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
}

Integrate with Pushbutton Logic: If you're controlling the DFPlayer with a pushbutton, you can integrate this fading loop into the same conditional block where you start playing music.

Remember, it's important to manage your delays and loops carefully. Having long delays or blocking loops in your code can make your program unresponsive to other inputs.

This is a basic outline to get you started. Depending on your exact requirements and setup, you might need to adjust the code. Feel free to share your current code if you need more specific guidance.

Happy coding!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
  • Create New...