ArduinoTroubleshooting

DFPlayer Mini Makes my Servo Move at Arduino Startup???

userHead Account cancelled 2021-11-19 06:19:37 670 Views0 Replies
Hello Everyone,

I have the DFPlayer Mini and am trying to use my Arduino Mega to play a few sounds and make a servo move with the use of a button. Everything works just the way I want it to, however whenever I plugin the Arduino the servo moves 60 degrees one direction and back again. The servo was not doing this until I added the DFPlayer code.

Any assistance would be greatly appreciated.
Code: Select all
#include <Servo.h>
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>

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

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

// Use pins 2 and 3 to communicate with DFPlayer Mini
static const uint8_t PIN_MP3_TX = 19; // Connects to module's RX 
static const uint8_t PIN_MP3_RX = 18; // Connects to module's TX 
SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);

// Create the Player object
DFRobotDFPlayerMini player;

void setup() {
  pinMode(8,INPUT);
  Myservo.attach(9);  // attaches the servo on pin 9 to the servo object

  // Init USB serial port for debugging
  Serial.begin(9600);
  // Init serial port for DFPlayer Mini
  softwareSerial.begin(9600);

  // Start communication with DFPlayer Mini
  (player.begin(softwareSerial));
  
    // Set volume to maximum (0 to 30).
    player.volume(25);
    // Play the first MP3 file on the SD card
    player.play(1);
}

void loop()
{
  if(digitalRead(8)==LOW){
    player.play(2);
    Myservo.write(30); //ending position of arm to the back of bee, higher the number futher back toward the bee it will go. 11 - 30 only.
    delay(2000);
    Myservo.write(10); //ending position of the arm to the front of bee, higher the number futher back toward the bee it will go. 29 to 10 only.
    delay(2000);
    Myservo.write(30);
    delay(2000);
  }
  else
    
Myservo.write(0);
  
}