I thought as much that you left the jaw servo out for that reason.
I have added the PING sensor and it looks like that is working.
If I go close to the sensor the servos start moving.
The onlything is and that was also befor adding the ping sensor the sensors move realy slow. I gues that is because of the value of "ulCurrInterval". so if I like I can speed dat up a bit.
For what I would like to program.
I would like to have a playlist of songs that are on the DFPlayer to play one by one. If someone would come (to) near I like to play a sound from a second playlist.
There is a function on the DFPlayer that is called advertise. If I call that than the song is pauzed and the advertisment is played.
DFPlayer Mini Mp3 Player - DFRobot Wiki this is a some info on the player.
I wil attach the code as well. for if you would like a look.
#include "Arduino.h"
#include <Servo.h>
#define NUM_SERVOS 3
//************************FOR PING SENSOR***************************
const byte trgPin = 7; // pin we're sending the trigger/ping signal on
const byte echoPin = 8; // pin we're reading back the echo on
const long motionDist = 15; // distance that triggers a sound (15 for testing)
//const long motionDist=35; // distance that triggers a sound (29-40 for real life)
long distance = 0; // distance to be measured/recorded by ping sensor
//************************FOR SERVOS***************************
// This is for the Jaw//
unsigned long JawSlowInterval = 80;
unsigned long JawFastInterval = 10;
unsigned long JawCurrInterval = 80;
int JawDegrees = 1;
unsigned long JawtimeServo = 0;
int JawCurrPosition = 40;
int JawMinPosition = 10;
int JawMaxPosition = 65;
// create a servo object
Servo
ServoJaw,
ServoShake,
ServoNod,
ServoTilt;
typedef struct structServo
{
Servo *ptrServo;
unsigned long ulSlowInterval;
unsigned long ulFastInterval;
unsigned long ulCurrInterval;
int nDegrees;
unsigned long timeServo;
int nCurrPosition;
int nMinPosition;
int nMaxPosition;
}structServo;
structServo ServoData[NUM_SERVOS] =
{
{
.ptrServo = (Servo *)&ServoTilt,
.ulSlowInterval = 80,
.ulFastInterval = 10,
.ulCurrInterval = 80,
.nDegrees = 1,
.timeServo = 0,
.nCurrPosition = 55,
.nMinPosition = 10,
.nMaxPosition = 100
},
{
.ptrServo = (Servo *)&ServoNod,
.ulSlowInterval = 80,
.ulFastInterval = 10,
.ulCurrInterval = 80,
.nDegrees = 1,
.timeServo = 0,
.nCurrPosition = 105,
.nMinPosition = 70,
.nMaxPosition = 140
},
{
.ptrServo = (Servo *)&ServoShake,
.ulSlowInterval = 80,
.ulFastInterval = 10,
.ulCurrInterval = 80,
.nDegrees = 1,
.timeServo = 0,
.nCurrPosition = 75,
.nMinPosition = 10,
.nMaxPosition = 130
},
};
//************************FOR AUDIO***************************
int audio_value = 0; //stores the calculated audio value
long lastMsg = 0; //stores millis
long sleepWindow = 300000; //if 5 minutes go by with no signal, then put the servos to bed
volatile boolean servosEnabled = false; //check if servos are enabled
volatile boolean ledsOn = true; //for turning on the leds of the eyes
volatile unsigned long currentTime = 0;
volatile unsigned long lastLEDtime = 0;
unsigned long resetWait = 120000; //servos sleep if not changed within this time frame (120 secs)
//set the first parameter in the following functions to a number between 0 and 180.
//int ServoJawMin = (map(92, 0, 180, 544, 2400)); //???
//int ServoJawMax = (map(72, 0, 180, 544, 2400)); //???
void setup( void )
{
// Set Up TRIGGER SENSOR
pinMode(trgPin, OUTPUT);
digitalWrite(trgPin, LOW);
pinMode(echoPin, OUTPUT); // just to make sure
digitalWrite(echoPin, LOW); // we clear any previous settings
pinMode(echoPin, INPUT); // and then use it as INPUT
Serial.begin(115200);
Serial.println("initializing...");
// Set up DFPlayer
//InitAudio();
attach_servos();
}//setup
void loop()
{
distance = getDist();
delay(2);
Serial.print("distance = ");
Serial.println(distance);
if (distance <= 5)
{
Headservos( millis() );
}
}
void attach_servos()
{
// attach the pin to the servo object
ServoJaw.attach(2);
ServoShake.attach(3);
ServoTilt.attach(5);
ServoNod.attach(6);
//servosEnabled = true;
//Serial.print ("servo's are enabled");
}
void detach_servos()
{
// detach the servo objects
ServoJaw.detach();
ServoShake.detach();
ServoTilt.detach();
ServoNod.detach();
//servosEnabled = false;
//Serial.print ("servo's are disabled");
}
void action( unsigned long Jawmil)
{
/*
if (Jawmil - JawtimeServo >= JawFastInterval)
{
JawtimeServo = Jawmil;
JawCurrPosition += JawDegrees;
if (JawCurrPosition >= audio_value || JawCurrPosition <= audio_value)
{
JawCurrPosition = - JawDegrees;
}
ServoJaw.write(JawCurrPosition);
}
*/
}
void Headservos( unsigned long tNow )
{
for( int srvo=0; srvo<NUM_SERVOS; srvo++ )
{
if( tNow - ServoData[srvo].timeServo >= ServoData[srvo].ulCurrInterval )
{
ServoData[srvo].timeServo = tNow;
ServoData[srvo].nCurrPosition += ServoData[srvo].nDegrees;
if( ServoData[srvo].nCurrPosition >= ServoData[srvo].nMaxPosition ||
ServoData[srvo].nCurrPosition <= ServoData[srvo].nMinPosition )
{
ServoData[srvo].nDegrees *= -1;
}
ServoData[srvo].ptrServo->write( ServoData[srvo].nCurrPosition );
}//if
}//for
//ServoJaw.write(audio_value);
}//Headservos
long getDist()
{
long duration, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trgPin, HIGH); // start the outgoing ping
delayMicroseconds(10); // do the ping for 10uS
digitalWrite(trgPin, LOW); // stop doing the ping
duration = pulseIn(echoPin, HIGH); // grab the delay of return echo
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
return (cm);
}
// Original code from the ping sensor library
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}