**New Code** This Works
//#include <ESP32Servo.h>
#include <Servo.h>
//#include <WiFi.h>
//#include <HardwareSerial.h>
#include <SoftwareSerial.h>
//#include <Stepper.h>
#include "AccelStepper.h"
#include <DFRobotDFPlayerMini.h>
//#include <MobaTools.h>
int serial1 = 10;
int serial2 = 11;
//HardwareSerial mySerial(2); // Use UART1 (Serial1 on ESP32)
SoftwareSerial mySerial(serial1, serial2); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
# define Start_Byte 0x7E
# define Version_Byte 0xFF
# define Command_Length 0x06
# define End_Byte 0xEF
# define Acknowledge 0x00
//Servos Eyes
Servo ServoEyes;
Servo ServoNeckRotate;
int volume = 30;
int poseye = 40;
int posneckupdown = 40;
const int volumeMute = 32;
const int volumeUP = 33;
const int volumeDOWN = 4;
int servoPinneckRotate = 9;
int servoPineyes = 12;
int buttonState = 0;
int lastButtonState = HIGH;
bool isMuted = false;
int savedVolume = 15;
bool stepperRunning;
//StepMotor
const int stepsPerRevolution = 2038;
#define HALFSTEP 4
// Motor pin definitions:
#define motorPin1 5 // IN1 on the ULN2003 driver
#define motorPin2 6 // IN2 on the ULN2003 driver
#define motorPin3 7 // IN3 on the ULN2003 driver
#define motorPin4 8 // IN4 on the ULN2003 driver
#define MotorInterfaceType 4
int endPoint = 1024;
//AccelStepper stepper(HALFSTEP, 5, 7, 6, 8);
AccelStepper stepper(MotorInterfaceType, motorPin1, motorPin3, motorPin2, motorPin4);
#define home_switch 4 // Pin 9 connected to Home Switch (MicroSwitch)
int move_finished = 1; // Used to check if move is completed
long initial_homing = 1;
const int homeSwitchPin = 4; // Connect your limit switch or hall sensor here
void setup() {
Serial.begin(115200); // Initializes Serial Monitor for debugging (USB output to PC)
mySerial.begin(9600); // RX, TX (DFPlayer communication)
//pinMode(limitSwitchPin, INPUT_PULLUP); // Internal pull-up resistor
//myStepperdown.setSpeed(10);
// DFPlayer Startup
Serial.println("Initializing DFPlayer...");
// Tries to initialize DFPlayer and prints error if failed
if (!myDFPlayer.begin(mySerial)) {
Serial.println("DFPlayer not detected."); // Shown if no communication with DFPlayer
while (1); // Stops further execution
}
Serial.println("DFPlayer connected!");
myDFPlayer.volume(30); // Set volume (0 to 30)
Serial.println("DFPlayer volume 30");
myDFPlayer.play(2); // Play first track (0001.mp3)
Serial.println("DFPlayer Intro");
myDFPlayer.loop(2);
//ServoEyes.setPeriodHertz(50); // standard 50 hz servo
ServoEyes.attach(servoPineyes, 500, 2400); // attaches the servo on pin 18 to the servo object
//ServoNeckRotate.setPeriodHertz(50); // standard 50 hz servo
ServoNeckRotate.attach(servoPinneckRotate, 500, 2400); // attaches the servo on pin 18 to the servo object
ServoEyes.write(40);
ServoNeckRotate.write(75);
// using default min/max of 1000us and 2000us
// different servos may require different min/max settings
// for an accurate 0 to 180 sweep
pinMode(volumeMute, INPUT);
pinMode(volumeUP, INPUT);
pinMode(volumeDOWN, INPUT);
digitalWrite(volumeMute, HIGH);
digitalWrite(volumeUP, HIGH);
digitalWrite(volumeDOWN, HIGH);
stepperhome();
int stepperRunning = true;
//stepper.moveTo(8000);
//myDFPlayer.play(1); // Play first track (0001.mp3)
Serial.println("DFPlayer Langnek Verhaal");
myDFPlayer.loop(1);
}
void stepperhome (){
ServoEyes.write(40);
Serial.println("Homing in progress... (Langnek)");
Serial.println("");
pinMode(home_switch, INPUT_PULLUP);
// Set Max Speed and Acceleration of each Steppers at startup for homing
//stepper.setMaxSpeed(700); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper.setAcceleration(250); // Set Acceleration of Stepper
//stepper.setSpeed(0);
// Start Homing procedure of Stepper Motor at startup
while (digitalRead(home_switch)) { // Make the Stepper move CCW until the switch is activated
stepper.setMaxSpeed(700.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper.setAcceleration(250.0);
//stepper.setSpeed(0);
stepper.moveTo(initial_homing); // Set the position to move to
initial_homing--; // Decrease by 1 for next move if needed
stepper.run(); // Start moving the stepper
stepper.runSpeed();
delay(0);
}
stepper.setCurrentPosition(0); // Set the current position as zero for now
stepper.setMaxSpeed(700.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper.setAcceleration(100.0); // Set Acceleration of Stepper
initial_homing = 1;
stepper.setCurrentPosition(0);
Serial.println("Homing Langenek Completed");
Serial.println("");
stepper.setMaxSpeed(700.0); // Set Max Speed of Stepper (Faster for regular movements)
stepper.setAcceleration(250.0);
//stepper.moveTo(200); // Set the position to move to
//initial_homing--; // Decrease by 1 for next move if needed
//stepper.run(); // Start moving the stepper
//delay(5);
}
int servoPosition = 40; // the current angle of the servo - starting at 90.
int servoSlowInterval = 80; // millisecs between servo moves
int servoFastInterval = 80;
int servoInterval = servoSlowInterval; // initial millisecs between servo moves
int servoDegrees = 2;
unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop()
unsigned long previousServoMillis = 0; // the time when the servo was last moved
const int servoMinDegrees = 20; // the limits to servo movement
const int servoMaxDegrees = 60;
void servoSweep() {
// this is similar to the servo sweep example except that it uses millis() rather than delay()
// nothing happens unless the interval has expired
// the value of currentMillis was set in loop()
if (currentMillis - previousServoMillis >= servoInterval) {
// its time for another move
previousServoMillis += servoInterval;
servoPosition = servoPosition + servoDegrees; // servoDegrees might be negative
if (servoPosition <= servoMinDegrees) {
// when the servo gets to its minimum position change the interval to change the speed
if (servoInterval == servoSlowInterval) {
servoInterval = servoFastInterval;
}
else {
servoInterval = servoSlowInterval;
}
}
if ((servoPosition >= servoMaxDegrees) || (servoPosition <= servoMinDegrees)) {
// if the servo is at either extreme change the sign of the degrees to make it move the other way
servoDegrees = - servoDegrees; // reverse direction
// and update the position to ensure it is within range
servoPosition = servoPosition + servoDegrees;
}
// make the servo move to the next position
ServoEyes.write(servoPosition);
// and record the time when the move happened
}
}
//New Code
class Sweeper
{
//Class Member Varialbes
//These are initialized at startup
//ServoEyes servo; // the servo
int pos =40; // current servo position
int posneck =40; // current servo position
// These maintain the current state
int increment; // increment to move for each interval
int updateInterval; // interval between updates
unsigned long lastUpdate; // last update of position
// Constructor - creates a Flasher
// and initializes the member variables and state
public:
Sweeper(int interval)
{
updateInterval = interval;
increment = 1;
}
void Attach(int pin)
{
ServoEyes.attach(servoPineyes);
}
void Detach()
{
ServoEyes.detach();
}
void Update()
{
if((millis() - lastUpdate) > updateInterval)
{
lastUpdate = millis();
pos += increment;
ServoEyes.write(pos);
//Serial.println(pos);
if ((pos >= 60) || (pos <= 20)) // end of sweep
{
// reverse direction
increment = -increment;
}
}
}
};
class Sweeperneck
{
//Class Member Varialbes
//These are initialized at startup
//ServoEyes servo; // the servo
int pos =40; // current servo position
int posneck =70; // current servo position
// These maintain the current state
int increment; // increment to move for each interval
int updateInterval; // interval between updates
unsigned long lastUpdate; // last update of position
// Constructor - creates a Flasher
// and initializes the member variables and state
public:
Sweeperneck(int interval)
{
updateInterval = interval;
increment = 1;
}
void Attach(int pin)
{
ServoNeckRotate.attach(servoPinneckRotate);
}
void Detach()
{
ServoNeckRotate.detach();
}
void Update()
{
if((millis() - lastUpdate) > updateInterval)
{
lastUpdate = millis();
posneck += increment;
ServoNeckRotate.write(posneck);
//ServoNeckRotate.write(70);
//Serial.println(pos);
if ((posneck >= 110) || (posneck <= 30)) // end of sweep
{
// reverse direction
increment = -increment;
}
}
}
};
Sweeper sweeper1(50);
Sweeperneck sweeper1neck(50);
//stepper.setCurrentPosition = 0;
const unsigned long MsecPeriod = 15;
unsigned long msec0;
int pos = 40; // variable to store the servo position
int dir = 1;
//Loop Works
void loop()
{
{
stepper.setMaxSpeed(700); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper.setAcceleration(250);
stepper.moveTo(230000);
Serial.println("Up Langenek");
Serial.println("Eyes Left Right");
Serial.println("Neck Left Right");
while(stepper.distanceToGo() > 0) {
stepper.run();
sweeper1.Update();
sweeper1neck.Update();
}
stepper.setMaxSpeed(700); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper.setAcceleration(250);
stepper.moveTo(0);
Serial.println("Down Langenek");
Serial.println("Eyes Left Right");
Serial.println("Neck Left Right");
while(stepper.distanceToGo() != 0) {
stepper.run();
sweeper1.Update();
sweeper1neck.Update();
}}
}
void volumeINC()
{
volume = volume+1;
if(volume==31)
{
volume=30;
}
exe_cmd(0x06, 0, volume);
delay(500);
}
void volumeDEC()
{
volume = volume-1;
if(volume==-1)
{
volume=0;
}
exe_cmd(0x06, 0, volume);
delay(500);
}
void exe_cmd(byte CMD, byte Par1, byte Par2)
{
word checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge, Par1, Par2, highByte(checksum), lowByte(checksum), End_Byte};
for (byte x=0; x<10; x++)
{
mySerial.write(Command_line[x]);
}
}
void mp3story() {
myDFPlayer.volume(30); // Set volume (0 to 30)
Serial.println("DFPlayer volume 30");
myDFPlayer.play(2); // Play first track (0001.mp3)
Serial.println("DFPlayer Langnek");
myDFPlayer.loop(1);
}
void serverstory() {
}