Help with Servos

Frank91608

New Member
I'm looking for some guidance on how to recreate a model that has servos, sound and LEDs working in sync. I'm specifically trying to recreate a sequence similar to the one that happens at 1:43 on this video:
.

I used picaxe in the past to do similar things but not this complicated. Where I keep getting confused is how to make things happen simultaneously.

Any help would be greatly appreciated.
 

lbenson

Senior Member
Where I keep getting confused is how to make things happen simultaneously.
This is basically pan and tilt with sound and flashes thrown in. You can probably search the forum, but 3-character items like "pan" won't be searched for. But you can use google: pan tilt site:https://picaxeforum.co.uk

From 13+ years ago with an 08M, there's this: https://picaxeforum.co.uk/threads/pan-and-tilt-control-of-webcam-with-picaxe-08m.9425/ (sorry the images have been lost, but the code is still there)

And this: https://picaxeforum.co.uk/threads/rivercam-with-picaxe-pan-tilt-control.13705/
 

AllyCat

Senior Member
Hi,
...how to recreate a model that has servos, sound and LEDs working in sync. ... Where I keep getting confused is how to make things happen simultaneously.
Hi,

Most (M2) PICaxe instructions execute in about 1 ms, so a simple list of a few instructions should execute almost "simultaneously". For more complex programs you can run 4 times faster using SETFREQ M16 , which is still compatible with the SERVO commands, or X2 chips default to slightly higher frequencies anyway. Thus two consecutive commands such as SERVO.... followed by LED HIGH should appear "simultaneous".

But it's complicated because there are two other different types of command, those that set up a "background" process and those that are "blocking". Typical Background commands such as PWMOUT and SERVO are "good" because the process will continue without any further instructions, but Blocking commands such as PAUSE and TUNE/PLAY are potentially "bad" because they may prevent other actions being initiated in synchronism. Thus you may need to put SERVO or LED ON/OFF commands before any "sound" command, and break any "Tunes" into separated "Notes".

Conversely, if your "Sounds" are from an MP3 player, then there may be a delay before the sound/file actually plays, so you may need to command a file to Play and then wait (with a Pause) for an experimentally-measured time before commanding a "Synchronised" action. Again you may need to split Sound files into individual short "sound effects" to synchronise other events, or you may need to read a "Busy" flag from the player to determine when a sound has finished.

There is another problem with "Servos", that they may move much too fast for a "realistic" motion. Therefore you may need a program loop which moves the Servo by a small amount every 20 - 100 ms. Other commands then might be included in that loop to "trigger" different events when the Servo reaches a particular position.

To summarise: Synchonising "Sounds" will probably be the most difficult, but it's not possible to give any more general advice without knowing the specific type of sound generation which is being used.

Cheers, Alan.
 

mikeyBoo

Senior Member
hi Frank91608,
The I2C PCA9685 boards as sold by Adafruit may be used to control up to 16 lights and servos in any combination for running programmed sequences. I use one of them for running lighting effects on my yak.

Adafruit 16-Channel 12-bit PWM/Servo Driver - I2C interface - PCA9685 https://www.adafruit.com/product/815

See the Picaxe app Picaxe Kayak Control System.bas. Look at the procs (aka subroutines) beginning with LED. (e.g. LED.outputs_Set LED.intensity_Set etc.) and this may give you some ideas.

Picaxe Kayak Control Project https://picaxeforum.co.uk/threads/picaxe-kayak-control-project.28063/

To control sound, an mp3 player is the simplest route using the Picaxe hserout command (e.g. using pin C.0 on a Picaxe-20M2)

Aideepen YX5300 UART Control Serial MP3 Music Player Module for Arduino/AVR/ARM/PIC $9 Prime
https://www.amazon.com/Aideepen-YX5300-Control-Serial-Arduino/dp/B01JCI23JG/ref=sr_1_4?dchild=1&keywords=Serial+MP3+player&qid=1612973862&sr=8-4

see proc mp3.command_Send in Picaxe app KYK_MSI1 Software for Picaxe.bas

Chamberlain Motion Receivers CWA2000 to Picaxe via KYK_MSI1 (Rev A)
https://picaxeforum.co.uk/threads/chamberlain-motion-receivers-cwa2000-to-picaxe-via-kyk_msi1-rev-a.32439/

hope this helps & good luck with your project!
 

Jeff Haas

Senior Member
If you decide to go with the YX5300 MP3 player, I have used those a lot and have code I can dig up. They work great and act as a co-processor...you tell the board to play the MP3 file and then go back to whatever you were doing.
 

AllyCat

Senior Member
Hi,

Or there's the complete, dedicated PICAXE-14 Audio Player Kit (needs soldering) which has also one Servo connector and two LED outputs. But the LCD, LDR and Button (switch) pins could be re-purposed for additional Servos or LEDs, etc., if required. A few more ideas on how it might be used or adapted are HERE.

Cheers, Alan.
 

Frank91608

New Member
Wow! This is a wealth of info. I really appreciate the guidance. It's going to take me a bit to absorb it all but I'm sure to have more questions.
 

AllyCat

Senior Member
Hi,

I see that your (only) previous posts (13 years ago ! ) concerned a "Sound Board" (the link is now broken) so you may have a "Favourite" Sound Player already. However, I've played the "Video" in post #1 above several times now and have made a few observations:

There appear to be 3 independent Servo "channels" required, for "Pan" , "Tilt" (or Azimuth / Elevation Angles) and the gun barrel(s)' "Recoil" (which might use two servos from a single control pulse). Similarly, there are two LEDs (one in each barrel?) which could use the same control signal, but you might want to use one pin for each to give a higher drive current. Note that PICaxe pins can deliver a higher current when used "Active Low" (i.e. Pulling the pin LOW), than the normal Active High mode. I'm not clear if the LEDs are required to "Dim" (decay) after an initial "Flash", but if so, that would require a PWM output and a more "continuous" program control (like the Servos). Also, the Video sound appears to include both Music and "Sound Effects" at the same time, so maybe you need two separate Sound Players?

However, if you are looking for advice on a "New Build" project, then the Audio Player Kit that I linked above does have three groups of header pins (each GND, V+ and a B.x port) which could be used for 3 Servos on pins B.1, B.3 and B.4. You would then need to modify the (14M2) board to connect (for example) pin C.4 (instead of B.4) as the "TX" pin to the player on the (alternative) connector group at the edge of the board. There are already pins/pads (B.2 and B.5) allocated for two LEDs, and a few spare pins for other input(s) or output(s) if required on port C.

Cheers, Alan.
 

Frank91608

New Member
Alan,

Yes that was a long time ago. I can't remember which sound board I was using then. For this project I picked up the 14M2 project board and this sound board: https://www.sparkfun.com/products/14006

I have three servos to control the movement - one for horizontal movement, one for vertical and then one for the recoil. My plan is to have three separate sound clips to match each servo move. The 2 LEDS will just flash on an off without dimming. I'll take some pics of the build and upload them.

Frank
 

AllyCat

Senior Member
Hi Frank,

Thanks, that Sound Board does look quite easy to use (and no need to find a "spare" SD memory-card) but it does appear to be(coming) "Retired" and I didn't find any code examples on this forum. Also beware that the control pins are NOT "5 volt Tolerant"; but you can run the PICaxe on a 3.3 volt rail (from a regulator) and it's generally good practice to use a separate 5 - 6 volt supply rail for the Servos anyway (ensuring that their Earth/Ground rails are linked).

The "Video" model above looks quite large and might be driven by "Continuous Rotation" Servos (particularly for the "Pan") with "end-stop" switch(es) to limit/calibrate the rotation. That's potentially easier to code because the program can simply set the motor speed and leave it to run "unattended" for several seconds. A conventional servo will normally sweep its full arc in a fraction of a second, so you probably would need a program loop of the form:
Code:
   servo b.2 , 100       ; Or wherever the servo was last "parked"
for  b2 = 101 to 200
   pause 100             ; Or as appropriate for the required speed (in increments of 20 ms)
   servopos b.2 , b2
;   if b2 = 150 then     ; Could trigger other events
;      Anything  else that might need to be done whilst the servo sweeps
;   endif
next
Cheers, Alan.
 

AllyCat

Senior Member
Hi,

Looking earlier in the video (1:38), it clearly uses a CR Servo, or more probably a DC motor of some type, which may give smoother Azimuth movement and the possibility of well over 180 degrees of rotation, if required. Recently BTW, I "non-destructively" (i.e. reversibly) converted a (9g) Servo to Continuous Rotation, which I will "write-up" some time soon if anybody is interested.

Cheers, Alan.
 
Top