I am wanting to Press a Button and both audio and 3x Servos to begin.
I have the following code so when I press a button the Servos move. I am wanting to map the servos to the music accurately. Is there a way to do this with an external Joystick and then put that code into Arduino?
My current set up is 3 Servo motors and a Button.
I will use another Arduino for the mp3 and just code so the same button starts the mp3
Heres the Code:
#include <Servo.h>
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() {
}
What do you mean by "mapping the music accurately?
Imagine a piece of music with a snare-drum roll if every single hit of the drum-stick during the roll would be counted as a beat the beats per minute would be more than 600 beats per minute = 10 beats per second.
Are you expecting the servos to move back and forth at that high speed? mechanically impossible.
Yes simply use a standard classical RC-remote control
how is the joystick-code related with the music-controlled servo-code?
How do you want to "feed-in" the music into the arduino so the arduino "knows" when to move a servo to what position ?
You will have to deliver a much more precise description of what you really want to do.
I have some assumptions what you might want to do but I refuse to write them as my assumptions could be wrong.
If this is too much effort for you try to command google or amazon-alexa to deliver a ready to use device
"OK google I am wanting to map the servos to the music accurately. Is there a way to do this with an external Joystick and then put that code into Arduino?"
or if you have amazon-alexa
"alexa I am wanting to map the servos to the music accurately. Is there a way to do this with an external Joystick and then put that code into Arduino?"
It doesn’t take much imagination to hazard a guess about what you are trying to do.
But I agree w/ @StefanL38, it would be a potential waste of time to bother too much helping you just yet.
If you have a joystick and two servos, see if you can cobble together a sketch that lets you move one servo and back and forth with the left and right axis of the joystick, and move the other servo back and forth the up and down axis of the joystick.
Forget about any other function and detail, Get that sketch working. You’ll need to do that anyway, so it won’t be any time misspent.
The rest of the functionality of your project seems possible if I am guessing correctly, but to worry about it now is premature.
Two servos, one joystick, one sketch you can write mostly by folllowing patterns you see in examples code for joysticks and servos.
Show us that sketch working or ask for help getting it to do. Then we can move on to this idea of yours what needs more explanation and planning before you write a single line of code to accomplish it.
See the Servo->Knob example for how to control a servo with an analog input. Write a sketch to read three analog inputs, position the three servos, and write the three values to Serial Monitor. Then you can use the knobs (or a joystick and a knob, or 1.5 joysticks) to position your three servos. Write down the positions for use in your sketch.
The mp3 will be a soundtrack of 3 voices. The servos are moving as mouths up and down. I am wanting the sound to be in time with the voices so each servo moves to a different voice.
I want to press a button so the mp3 file will start and so will the servo sequence. I can control the servo with an external joystick but I am wanting the movements of the servo to be recorded into code so I can play the code back and the servo will run on its own.
I am struggling to write the servo sequence as I am a beginner with code.
To be frank: Your project does not sound like one for a beginner in coding ...
Are you looking for someone to do the job for you OR are you interested in programming on your own? That makes a big difference!
When you are heading for the second option it is much more likely that you get support in the forum as it will consume much more "live time" to remotely develop a project for somebody else.
I understand that this is solved:
How to read a joystick ... What data do you get?
How to control one servo with the data from the joystick
If so I recommend that you go on with
Think how many data you need to control one servo over the complete time the mp3 is playing
Check whether this can be stored on-board or not for three servos (need for an SD Card reader?)
Learn how to control servos without using delay() but the millis() function
Learn about the use of arrays and structs (e.g. to store position and time for each servo)
Once this is done, you can write a sketch that starts to record time & position per servo in an array or probably a SDcard for replay.
to keep the servo-movements really in sync with the MP3 playback the information how and when the servo shall move could be stored in the right or left channel of the MP3-music as different frequencies.
There is PC-software for adding light-actions and such things but I don't remember the name of it.
Would using a PC be an option? or does it have to be tiny small and only a microcontroller is usable?
Second best solution is to record the movements including the timing.
But indeed if you are a beginner about coding it will need quite some time to learn all this
Have you gotten any further with a joystick, or one and a half joysticks, than @serenseren?
The advice is the same. We have now several times suggested to @serenseren that the roll must be slowed a bit.
It will be a challenge, made easier if you have the basic hardware working, plus like @serenseren you might learn quite a bit just getting that much functioning.
And @StefanL38 introduces a new idea from an old trick, encoding directions for external circuitry within the audio (or completely taking over one channel) that could be recognized and used to move the servos. That would make keeping the sound and the servos synchronized automatic.
It might be that if the program was simply replaying a recorded sequence of servo moves that the two would become unrelated in the way you wish to maintain.
Thanks Stefan. This sounds like a good Solution. How would i begin with encoding directions for external circuitry within the audio or something like this to move the Servos?
How many different positions do you need as a maximum?
I have not done something like this personally. The left or right channel of the MP3-file would contain a different number of pulses. A "special pulse-combination to indicate "start" and then counting the number of pulses
So if there is silence and only the pulses at maximum volume an operational amplifier (single-chip OpAmp with some resistors) working as a comparator with hysteresis should work.
I want to emphasise: I haven't build such a circuit myself for this kind of application audio-signal-pulses.
I have used it for a foto-transistor detecting the reflected light of a small laser where the light reflects from very different surfaces depending on what rotational element is creating the pulses.
This is an extra-question about general electronics you could post a question on this in the general electronics subforum
Since you’ll have to sooner or later, why not begin by getting one joystick to control two servos?
Then you’ll be able to gather some experience and data you are going to need, so it will be then to decide
how that data might be recorded
how it will be stored, be it in sound files or tables in your code or whatever and
how it will be played back and stay in sync
Baby steps. All of which you must take, you might get some gratification and motivation by getting something simple working right away, even if just a manual “instrument” you play along with music.
google for ideas about how sound can be processed to develop data. There are simple tricks like advancing to the next in an automated slide show, all the way up to decoding a data stream that could provide good fidelity when reproducing your live performance.
I like both approaches; working on the basic thing I’ve now challenged you to get working might reveal different approaches or definitively rule out one or both.