I am trying to use an Arduino mega as a clock. When the time hits specific values, I want to either: have my dfplayer mini play an mp3 file, or actuate a servo. Right now, I am using the DFROBOT LCD Kepyad Shield for the LCD screen, plugged into the Arduino Mega. I have a 5V power supply board powering the two 5g servos, and I am using the Arguino Mega to power both the LCD, the dfplayer mini, and a small speaker. The code works for the most part: the mp3 file plays at the correct time, and the servos actuate at the correct time. However--sometimes, the servos will twitch/actuate sporadically, especially when the dfplayer mini is commanded and operating. I tried using a separate power supply for each servo, but no change/success. The servos are fairly small, and combined are pulling well under the max for the power supply board.
My initial suspicion is that perhaps I either 1) have a code issue where the servo is constantly reading off a PWM pin, and somehow in the code the arduino is using the same pin or channel for the dfplayer, or 2) I have the wiring messed up and there is a short somewhere.
I did also try (within each if loop for the servo) the sequence of: attaching the servo, writing to a position, and then detaching the servo...I thought the "detach" would fix the potential issue of a dueling pwm channel; However, when I did that, the servo did not respond/actuate at all.
Below is the code I'm using (pieced together based on various arduino forums): I would appreciate any assistance!
/*This software is provided in “AS IS” condition,NO WARRANTIES in any form apply to this software.
picmicrolab.com
***************************************************************************************************************
Digital Clock with 1602 LCD Shield Board for Arduino
SELECT - 3.0855 V - Adj Hours
LEFT - 1.9778 V - Adj Minutes
DOWN - 1.2446 V
UP - 0.4918 V
RIGHT - 0 V
*/
// include the library code:
#include <LiquidCrystal.h>
#include "DFRobotDFPlayerMini.h"
#include "SoftwareSerial.h"
#include <Servo.h>
// #include <VarSpeedServo.h>
Servo Servo1; //rear landing gear
Servo Servo2; //front landing gear
// Servo Servo3; //bottom flap
// Use pins 11 and 12 to communicate with DFPlayer Mini
static const uint8_t PIN_MP3_TX = 11; // Connects to module's RX
static const uint8_t PIN_MP3_RX = 12; // Connects to module's TX
SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);
// Create the Player object
DFRobotDFPlayerMini player;
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 8, en = 9, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//----Time Var----------
int TimeHours10s = 0;
int TimeHours1s = 0;
int TimeMinutes10s = 0;
int TimeMinutes1s = 0;
int TimeSeconds10s = 0;
int TimeSeconds1s = 0;
//----------------------
//----Alarm Var---------
int AlarmHours10s = 0;
int AlarmHours1s = 0;
int AlarmMinutes10s = 0;
int AlarmMinutes1s = 0;
int AlarmSeconds10s = 0;
int AlarmSeconds1s = 0;
//----------------------
// ----------------------int steps for servo----------------------
int pos = 0; // current servo position
int targetPos = 90; // target servo position
int stepSize = 1; // increment for each step
unsigned long lastMoveTime = 0;
int delayBetweenSteps = 10; // milliseconds
void setup()
{
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Init USB serial port for debugging
// Serial.begin(9600);
// Init serial port for DFPlayer Mini
softwareSerial.begin(9600);
player.begin(softwareSerial);
delay(1500);
Servo1.attach(22);
Servo1.write(0);
delay(1500);
Servo2.attach(45);
Servo2.write(0);
delay(1500);
}
void loop()
{
CalculateTime();
ShowTime();
//ShowAlarm();
delay(1000);
//-------------------------------------Adj Time Value--------------
while(analogRead(A0)* 4.88 < 4500)
{
CalculateTime();
ShowTime();
if(analogRead(A0)* 4.88 > 10 && analogRead(A0)* 4.88 < 293) // right button,
{
TimeMinutes1s++;
delay(500);
CalculateTime();
}
if(analogRead(A0)* 4.88 > 300 && analogRead(A0)* 4.88 < 1465) // up button, inc hours
{
TimeHours1s++;
delay(500);
CalculateTime();
}
}
}
void ShowTime()
{
//--------------------------------------------
// Turn on the display
lcd.display();
lcd.setCursor(0, 0); // top left
lcd.print("Time ");//
if(TimeHours10s == 0)
lcd.print(" ");//
else
lcd.print(TimeHours10s);//
lcd.print(TimeHours1s);//
lcd.print(":");//
lcd.print(TimeMinutes10s);//
lcd.print(TimeMinutes1s);//
lcd.print(":");//
lcd.print(TimeSeconds10s);//
lcd.print(TimeSeconds1s);//
lcd.setCursor(0, 0); // top left
}
void CalculateTime()
{
TimeSeconds1s++;
if(TimeSeconds1s == 10)
{
TimeSeconds1s = 0;
TimeSeconds10s++;
}
if(TimeSeconds10s == 6)
{
TimeSeconds10s = 0;
TimeSeconds1s = 0;
TimeMinutes1s++;
}
if(TimeMinutes1s == 10)
{
TimeMinutes1s = 0;
TimeSeconds10s = 0;
TimeSeconds1s = 0;
TimeMinutes10s++;
}
if(TimeMinutes10s == 6)
{
TimeMinutes10s = 0;
TimeMinutes1s = 0;
TimeSeconds10s = 0;
TimeSeconds1s = 0;
TimeHours1s++;
}
if(TimeHours1s == 10)
{
TimeHours1s=0;
TimeMinutes10s = 0;
TimeMinutes1s = 0;
TimeSeconds10s = 0;
TimeSeconds1s = 0;
TimeHours10s++;
}
if(TimeHours10s == 2 && TimeHours1s == 4)
{
TimeHours10s=0;
TimeHours1s=0;
TimeMinutes10s = 0;
TimeMinutes1s = 0;
TimeSeconds10s = 0;
TimeSeconds1s = 0;
}
if (TimeMinutes10s == 0 && TimeMinutes1s == 0 && TimeSeconds10s == 0 && TimeSeconds1s == 0) // top of hour: takeoff, gear down
{
player.volume(20);
player.play(1);
}
if (TimeMinutes10s == 0 && TimeMinutes1s == 7 && TimeSeconds10s == 0 && TimeSeconds1s == 0) // halfway to 15: gear up
{
Servo1.write(90);
Servo2.write(90);
}
if (TimeMinutes10s == 1 && TimeMinutes1s == 5 && TimeSeconds10s == 0 && TimeSeconds1s == 0 ) //10k ft, at 15min
{
player.volume(25);
player.play(2);
}
if (TimeMinutes10s == 3 && TimeMinutes1s == 0 && TimeSeconds10s == 0 && TimeSeconds1s == 0 ) // 30mins: 10k ft descending
{
player.volume(25);
player.play(3);
}
if (TimeMinutes10s == 3 && TimeMinutes1s == 7 && TimeSeconds10s == 0 && TimeSeconds1s == 0) // halfway to 45, gear down
{
Servo1.write(0);
Servo2.write(0);
}
if (TimeMinutes10s == 4 && TimeMinutes1s == 5 && TimeSeconds10s == 0 && TimeSeconds1s == 0 )
{
player.volume(25);
player.play(4);
}
}