Hi, I have been working on a model skeleton head. It has a moving jaw that responds to an Alexa voice command. An analog signal representing the voice appears on A5; the level is set such that a sound below <100 results in no movement; however, above this value, the angle of servo 1 moves accordingly in sync with the voice heard in a small speaker mounted into the skull. While voice commands move the jaw of the skull, the skull rotates at an angle between 0 ° and 70 °. When voice commands cease (<100), both the jaw and skull’s rotations stop, which is fine. Instead of stopping at any position, I'm trying to return the skull’s rotation to 0 °. in my case, facing forward. I’ve posted my sketch, which works fine, but I'm having difficulty with line 61, where servo 2, the skull rotation servo, needs to return to 0 ° when everything is silent.
Hope this explanation explains my query. Code included.
#include <Servo.h>
Servo servo2; // Create servo object to control the second servo
int pos1 = 0; // Variable to store Servo 1 position
int pos2 = 70; // Variable to store Servo 2 position 70deg
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
const long interval1 = 15; // Interval to update Servo 1 (milliseconds)
const long interval2 = 40; // Interval to update Servo 2 (milliseconds) // 10
int direction1 = 1; // Direction for Servo 1 movement
int direction2 = -1; // Direction for Servo 2 movement -1;
int leftLED = 10; // left side skull eye led
int rightLED = 11; // right side skull eye led
// ++++++++++ jaw actuator servo control +++++++++++++
Servo servo_jaw;
const int soundSensor = A5;
int servoPin = 4; // jaw pwm control signal
int soundValue; // anolog digital signal voltage
int angle;
int mutePin = 3; // mute signal to mute amplifier module
// ***********************************************
void setup() {
servo_jaw.attach(servoPin);
pinMode(mutePin, OUTPUT);
pinMode(leftLED, OUTPUT);
pinMode(rightLED, OUTPUT);
servo2.attach(5); // rotate skull servo 2 to digital pin 5
servo2.write(pos2); // Set initial position for servo 2
digitalWrite(mutePin, HIGH); // mute amplifier
delay(6000); // allow circuits time to settle
}
void loop() {
soundValue = analogRead(soundSensor);
delay(10);
angle = map(soundValue, 0, 1023, 20, 120); //jaw open/close
servo_jaw.write(angle); // set jaw open and close gap
delay(20);
if (soundValue < 100) // set minimum sound level
{
digitalWrite(mutePin, LOW);
digitalWrite(leftLED, LOW);
digitalWrite(rightLED, LOW);
} else {
digitalWrite(mutePin, HIGH);
digitalWrite(leftLED, HIGH);
digitalWrite(rightLED, HIGH);
//****************************************Control Servo 2 head rotate
unsigned long currentMillis = millis();
if (currentMillis - previousMillis2 >= interval2) {
previousMillis2 = currentMillis;
//digitalWrite(mutePin, HIGH);
digitalWrite(leftLED, HIGH);
digitalWrite(rightLED, HIGH);
pos2 += direction2;
if (pos2 <= 0 || pos2 >= 70) { // set angle
direction2 *= -1; // Reverse direction at limits
}
servo2.write(pos2); // skull rotates between 0 and 70 deg return skull to position 0
}
if (soundValue < 100) // if sound level drops below <100
direction2 *= -1; // return skull angle to 0 deg
{
digitalWrite, (pos2 <= 0);
}
}
} // END
Hi, thanks for your reply. It does compile and runs fine in the model. I have a limited knowledge of programming, and often takes me many hours of work using code examples in my programs.
Now doubt certain areas can be improved upon and any advice is of course appreciated,
I’m not an experienced programmer by any means, but I use code snippets from other members here from time to time and generally get by, members help has always been appreciated. I just needed some guidance from line 61 just to get the servo2 to return too it’s 0 deg angle when no commands are received. Any help offered in this respect is very much appreciated. Please note I have disabilities which limit my work and replies, but will take on board the comments and try and figure it out myself. Kind regards Jevray.
You need to tell us, what "model" does this run fine in?
Then, please consider the comments within this, and try it. It is not your solution, but rather an attempt to show you what you've done wrong. Your solution will take more work.
#include <Servo.h>
Servo servo2; // Create servo object to control the second servo
int pos1 = 0; // Variable to store Servo 1 position
int pos2 = 70; // Variable to store Servo 2 position 70deg
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
const long interval1 = 15; // Interval to update Servo 1 (milliseconds)
const long interval2 = 40; // Interval to update Servo 2 (milliseconds) // 10
int direction1 = 1; // Direction for Servo 1 movement
int direction2 = -1; // Direction for Servo 2 movement -1;
int leftLED = 10; // left side skull eye led
int rightLED = 11; // right side skull eye led
// ++++++++++ jaw actuator servo control +++++++++++++
Servo servo_jaw;
const int soundSensor = A5;
int servoPin = 4; // jaw pwm control signal
int soundValue; // anolog digital signal voltage
int angle;
int mutePin = 3; // mute signal to mute amplifier module
// ***********************************************
void setup() {
servo_jaw.attach(servoPin);
pinMode(mutePin, OUTPUT);
pinMode(leftLED, OUTPUT);
pinMode(rightLED, OUTPUT);
servo2.attach(5); // rotate skull servo 2 to digital pin 5
servo2.write(pos2); // Set initial position for servo 2
digitalWrite(mutePin, HIGH); // mute amplifier
delay(6000); // allow circuits time to settle
}
void loop() {
soundValue = analogRead(soundSensor);
delay(10);
angle = map(soundValue, 0, 1023, 20, 120); //jaw open/close
servo_jaw.write(angle); // set jaw open and close gap
delay(20);
if (soundValue < 100) // set minimum sound level
{ //this executes if soundValue is < 100
digitalWrite(mutePin, LOW);
digitalWrite(leftLED, LOW);
digitalWrite(rightLED, LOW);
} else {//from here to the end all executes when soundValue is >= 100; you can
//determine that by hovering over the { - the matching } will be hilighted
digitalWrite(mutePin, HIGH);
digitalWrite(leftLED, HIGH);
digitalWrite(rightLED, HIGH);
//****************************************Control Servo 2 head rotate
unsigned long currentMillis = millis();
if (currentMillis - previousMillis2 >= interval2) {
previousMillis2 = currentMillis;
//digitalWrite(mutePin, HIGH);
digitalWrite(leftLED, HIGH);
digitalWrite(rightLED, HIGH);
pos2 += direction2;
if (pos2 <= 0 || pos2 >= 70) { // set angle
direction2 *= -1; // Reverse direction at limits
}
servo2.write(pos2); // skull rotates between 0 and 70 deg return skull to position 0
}
/* commented out, because it will never ever execute; we're still within the else!!
if (soundValue < 100) // if sound level drops below <100
direction2 *= -1; // return skull angle to 0 deg
{
digitalWrite, (pos2 <= 0);//and thisline is nonsense - look again at the syntax
//for digitalWrite - needs both a pin and a value
}
*/
}
} // END
Go back to the very beginning and learn how to command the servo to move to a particular angle. The Servo Sweep example would be a fine place to start.
#include <Servo.h>
int servoPin = 9;
Servo servo;
int angle = 0; // servo position in degrees
void setup()
{
servo.attach(servoPin);
}
void loop()
{
// scan from 0 to 180 degrees
for(angle = 0; angle < 180; angle++)
{
servo.write(angle);
delay(15);
}
// now scan back from 180 to 0 degrees
for(angle = 180; angle > 0; angle--)
{
servo.write(angle);
delay(15);
}
}
I can see the "head" servo does a continuous sweep, back and forth, when "noise" is above 100. I can also see the "jaw" servo does a "sweep" motion according to the input level of A5 (as A5 increases, so does the angle on "jaw" servo, and vice-versa).
You have two if (soundValue < 100) conditions... and your code is not using the "first" condition to return the "head" servo to 0. Remove the second condition on the bottom of the sketch and put servo2.write(0); in the first condition at the top of the sketch. It will be a one-command servo move, but it will return the "head" to 0 degrees.
@Jevray could use the MoToServo class of my MobaTools Library instead of the simple servo.h. The transition should be fairly simple, because the method names are nearly the same. But the library provides slow servo movement by itself, so the sketch should get more simple. And the one-command servo move to '0' when (soundValue < 100) will move the servo slowly to '0'.
After you "get it working" you will want to work on moving the "head" slowly back toward 0. My "working" code makes the "head" servo move to zero in one, fast, movement.
Thanks for your reply, I did initially try various code examples which worked, the one I’m using for the skull head rotations includes the use of millisecond so not to block the jaw functions. Your example is simple enough for me to try out. Regards Jevray