ArduinoGeneral

DFPlayer with Servo

userHead Account cancelled 2018-03-13 00:16:46 1367 Views1 Replies
I want my DFPlayer work while the Servo is sweeping.
but my DFPlayer is not working with my Servo.
I'm using Arduino Uno
Here's my code:
Code: Select all
#include "Arduino.h"
#include "DFRobotDFPlayerMini.h"
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);

/* Sweep
 by BARRAGAN <http://barraganstudio.com>
 This example code is in the public domain.

 modified 8 Nov 2013
 by Scott Fitzgerald
 http://www.arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
    myDFPlayer.play(1);
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
    myDFPlayer.play(1);
  }
}
2018-03-13 05:13:56 Good luck with getting any sort of help here! (The forum, especially when it deals with DFPlayers seems 'dead'.. I have several DFPlayer questions posted.. all 0 replies!)

Anyways..

Your servo needs interrupts to work...(Timer).. and the DFPlayer takes serial commands.. (also using interrupts I believe)..

In order for you to get your sketch to work the way you want it to... you will need to update things to be non-blocking..

(ie: no delays() are used ANYWHERE in your code).. and instead you use millis() to detect when something should happen. (Check out the blink without delay example sketch in the IDE)

You might try calling the DFplayer .play() command first.. since it shouldnt need to do anything after the serial command has been sent..
Even if that does happen to work.. it is still recommended to never (ever) use delays().. when a delay is happening.. nothing else in the code is working.. (you are checking sensors, you arent checking button presses...etc..etc) you dont want to eliminate that stuff.. so its better to never use delays()..

Or you can use a dedicated servo driver board... but you should not -have- to..

Good luck!
userHeadPic whispers007