I'm trying to have the DFplayer play a sound while the servo moves from 180 to 40 degrees, and then pause the DFplayer while the servo moves back from 40 to 180 degrees. I'm avoiding using delay in the loop, but it does not make a difference for the brief stutter I'm experiencing. I used Wokwi to test and there is a jitter in servo movement right around the Short Pause (see code).
Am I missing something here?
Here's the code
#include <Arduino.h>
#define ENABLE_EASE_QUADRATIC
#include "ServoEasing.hpp"
ServoEasing Servo1;
#include <DFPlayerMini_Fast.h>
DFPlayerMini_Fast player;
#define START_DEGREE_VALUE 90
#include <SoftwareSerial.h>
SoftwareSerial softSerial(/*rx =*/4, /*tx =*/5);
#define mySerial softSerial
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
delay(4000); // To be able to connect Serial monitor after reset or power up and before first print out. Do not wait for an attached Serial Monitor!
// Just to know which program is running on my Arduino
#if !defined(PRINT_FOR_SERIAL_PLOTTER)
Serial.println(F("START " __FILE__ " from " __DATE__ "\r\nUsing library version " VERSION_SERVO_EASING));
#endif
softSerial.begin(9600);
player.begin(softSerial, true);
player.volume(20);
Servo1.attach(9);
Serial.println(F("Set easing type to QUADRATIC_IN_OUT for Servo1"));
Serial.println(F(""));
Servo1.setEasingType(EASE_QUADRATIC_IN_OUT);
// Move servo to good start position
setSpeedForAllServos(50);
Servo1.easeTo(180);
Serial.println(F("Getting Ready"));
Serial.println(F(""));
delay(4000);
}
void loop() {
uint16_t tSpeed = (random(80 , 100));
setSpeedForAllServos(tSpeed);
player.playNext();
Servo1.easeTo(40);
//Short Pause
int16_t shortPause = random(25, 400 + 1);
unsigned long SPause = millis();
while(millis() - SPause <= shortPause)
{
;
}
player.pause();
int16_t outSpeed = tSpeed - random(10, 30 +1);
setSpeedForAllServos(outSpeed);
Servo1.easeTo(180);
//Long Pause
uint16_t LongPause = random(1500, 2000 + 1);
unsigned long LPause = millis();
while(millis() - LPause < LongPause)
{
;
}
}