Moving Servos to voice

I am creating 3 fish that sing with 6 motors (3 x mouths and 3x eyes)

I am wanting to Press a Button and both audio and Servos begin a sequence.

I have the following code so when I press a button the Servos move to a random Servo code I found. I am wanting the Servo’s to move to the mp3 accurately so they look like they are talking. Is there a way to control the servos with an external joystick and the movements be live recorded into arduino code so I can run the code back in Arduino?

Heres the Code:

Servo myservo1; 
Servo myservo2;
Servo myservo3; // create servo object to control a servo 
                // twelve servo objects can be created on most boards

int pos =0;    // variable to store the servo position 

void setup() {

 { 
  
  myservo1.attach(9);
  myservo2.attach(6);
  myservo3.attach(5); // attaches the servo on pin 9 to the servo object 
} 
 { 
  myservo1.write(180);             
    delay(2000); ////stops motor for 8 seconds 
  for(pos = 180; pos>=0; pos-=1)// goes from 180 degrees to 0 degrees
  myservo2.write(180);             
    delay(2000); ////stops motor for 8 seconds 
  for(pos = 180; pos>=0; pos-=1)// goes from 180 degrees to 0 degrees
    myservo3.write(180);             
    delay(2000); ////stops motor for 8 seconds 
  for(pos = 180; pos>=0; pos-=1)// goes from 180 degrees to 0 degrees
  {                                
    myservo1.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(5); // waits 15ms for the servo to reach the position 
    myservo2.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(10); // waits 15ms for the servo to reach the position 
    myservo3.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(5); // waits 15ms for the servo to reach the position 

  } 

    myservo1.write(0);              
    delay(4000); //stops motor for 4 seconds   
  for(pos = 0; pos <= 180; pos +=1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo1.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);      // waits 15ms for the servo to reach the position

  } 

} 
}

void loop() {

}

Hello @serenseren!

I’m assuming you are using regular hobby servos, if that’s the case then they probably don’t have position feedback so you can’t “live record” the movements. However, as you want to use a potentiometer to control the servos you could simply use that to “record the positions”.

So you simply need to use a code that uses a potentiometer to control the position for the servos, for example:

What that does is read the “joystick value” and map/scale that to a servo position, this way you can place the servo to the position you want and either write the values down or modify the code to save the values to an array when you press a certain key (for example).

1 Like

Thanks Geraldine, do you know how I would modify the code to save the values to an array?

You could add a button to make the process easier. If you use the code from the link I shared you would need to add the button pin, read the button state, and save the potentiometer value when the button is pressed.

The code would be similar to this

#include <Servo.h>
#define SERVO_PIN 9
#define GROUND_JOY_PIN A3            //joystick ground pin will connect to Arduino analog pin A3
#define VOUT_JOY_PIN A2              //joystick +5 V pin will connect to Arduino analog pin A2
#define XJOY_PIN A1                  //X axis reading from joystick will go into analog pin A1
#define BUTTON_PIN 2                 //number of the pushbutton pin

int savedValues[50];
int i = 0;
Servo myservo;
 
void setup()
{
 Serial.begin(9600);
 pinMode(VOUT_JOY_PIN, OUTPUT) ;    //pin A3 shall be used as output
 pinMode(GROUND_JOY_PIN, OUTPUT) ;  //pin A2 shall be used as output
 digitalWrite(VOUT_JOY_PIN, HIGH) ; //set pin A3 to high (+5V)
 digitalWrite(GROUND_JOY_PIN,LOW) ; //set pin A3 to low (ground)
 pinMode(BUTTON_PIN, INPUT);        // initialize the pushbutton pin as an input:
 myservo.attach(9);
}
 
void loop()
{
   delay(200);     
   int joystickXVal = analogRead(XJOY_PIN) ;  //read joystick input on pin A1
   int scaled_val = (joystickXVal+520)/10;
   Serial.println("Servo value = ");
   Serial.print(scaled_val);                  //print scaled value
   Serial.println() ;
   myservo.write(scaled_val);                 //write the calculated value to the servo  
   if(digitalRead(buttonPin) == HIGH) { 
      Serial.println("Saved value = ");       //print saved value
      Serial.print(scaled_val);              
      savedValues[i] = scaled_val;
      i++;
   }   
}
``