designing an Automatic pop-up target using a geared DC motor

Thread Starter

MLD

Joined Dec 7, 2012
76
Im OK with using 10 channels. The Random operation on the 10 channels are working just fine. THANKS
 

Thread Starter

MLD

Joined Dec 7, 2012
76
Here is an image of the electronic project box for the Random circuit board. Target automatically randomly pops up when shot down.


RJ
 

Attachments

Thread Starter

MLD

Joined Dec 7, 2012
76
OK great. Can you write me a code to drive a RC servo in random mode, just like the code you wrote before but to drive an RC servo instead? A switch will need to be used to let the PICAXE know when the target is down and start the cycle.

RJ
 

thatoneguy

Joined Feb 19, 2009
6,359
If you only wanted the servo in two positions, you would generate a random number, test if greater than 0.5, if true rotate servo to position A, else rotate to position B.
 

thatoneguy

Joined Feb 19, 2009
6,359
Here is code, I only added one servo on B.4

You will need to play with the numbers (75 and 225) to get your servo moving in the desired places.

You can change the conditions for other servos on other random outcomes as well, there are a ton of random algorithm behavior algorithms around the net, this one is a simple "If random number is big, move, otherwise, don't move"

The only period the servo is not receiving PWM is when the random generator assigns a zero to that bit. For this reason, I put the push up and return in their own if loop, so if the PWM is disrupted, it is only while the servo is in resting position.

Changes to code are in red and bold. I think this was the latest version of the code for your project. If not, you can add the modifications to any version. Read the Picaxe manual #2 for information on the servo, servopos and if-endif commands.

Rich (BB code):
#picaxe20M2

; More info at:http://forum.allaboutcircuits.com/showthread.php?t=77919
; 
;  This code may be re-used, modified, and shared by anybody, as long as  the information and credits above are not altered or removed.
; Please post improved code ideas and updates to a thread in the electronics chat area of allaboutcircuits.com
; Have fun, remember to wear eye and hearing protection when shooting!



;declarations
dirsB=$FF  ; All port B pins to outputs (20M2 pins 11-18)
dirsC=$0F  ; first 4 ports of port C as outputs (20M2 pins 7-10)
pinsB = %00000000 ; Reset all port B Pins to 0
pinsC=$00        ; Reset all port C outputs to 0

;variables
symbol delaytime = w0  ;delaytime variable  random requires word variables
symbol outs = b10     ; outputs
symbol adjust = b11    ; Potentiometer connected to C.7 (20M2 pin 3) to adjust delays

;initialization
servo B.4,75 ; initialise servo on B.4
delaytime = $1F         ;seed pseudo-random generator
;main loop
main:

readadc c.7,adjust    ; Pot connected between V+ and Gnd, wiper connected to C.7 (20M2 pin 3).  Used for adjusting random timing.
adjust = adjust * 100   ; make 0-254 number 0-25400 

random delaytime      ; get pseudo-random number between 0 and 65535
delaytime = delaytime % 7000  ; Mod 7000 to get number between 0 and 7000
;delaytime = delaytime + 3000  ;add 3000 for 3-10second delaytime (depreciated by user potentiometer addition)
delaytime = delaytime + adjust ; add user chosen delaytime to delay.

pause delaytime          ;pause for 0-26,100 milliseconds (0-26 seconds, user set potentiometer + 0-7 seconds)

random delaytime        ; get new random number for output states
outs = delaytime/256   ;use first 8 bits of delaytime word for random port b outputs
pinsB= outs           ;set portB pins high or low psuedo-random (20M2 pins 11-18)
if delaytime > 32768 then
  servopos B.4,225 ; move servo to up position
  pause 300 ; wait for servo to finish pushing target up
  servopos B.4,75  ; return servo to idle position
  pause 300 ;  wait for servo to finish moving
endif

random delaytime         ; get new random number for output states
outs = delaytime/512    ;use last 4 bits of delaytime word for random port c outputs
pinsC= outs           ;set portC pins high or low psuedo-random (20M2 pins 7-10)

pause 500              ;wait ½ second to ensure all chosen targets to reset are up
pinsB = $06              ;disable outputs  other than Servo on B.4 after the ½ second delay so cylinders aren't pushing on target
pinsC = $00              ;disable outputs after the ½ second delay so cylinders aren't pushing on target

goto main               ; keep doing it until power is removed.
 
Last edited:

Thread Starter

MLD

Joined Dec 7, 2012
76
Thanks "thatoneguy" I will build it and test it. Keep in mind i know nothing about programming. Im just familiar with hardware. I can build circuits following a schematic diagram and troubleshoot them but don't know anything about programming. I already know how to wire up the RC servo with the PICAXE. I just need help with the source code for the PICAXE

RJ
 

Thread Starter

MLD

Joined Dec 7, 2012
76
I put the code on the PICAXE 20M2 and servo does not move 0 to 90 degrees just stays at a hault. Is there an input on the picaxe that has to go logic low or high to trigger the RC servo?
 

thatoneguy

Joined Feb 19, 2009
6,359
Try making a new program, and running Only the servo in that program.

There may be an issue with setting the output port to 1 or 0 while running the servo command on it (Servos require continual PWM to hold position).

There is sample code for servo in Manual 2 under "servo" and "Servopos" that you can copy/paste into a new program to download and try.

Sometimes, the servo range is only from 150 to 190 (168 center) for a fast ball bearing metal geared servo I used, so you'll need to play with the numbers a bit.
 

Thread Starter

MLD

Joined Dec 7, 2012
76
Here is a much simpler code with an input to start the cycle, randomly as well. The servo is moving too fast, how can I slow it down to 5ms

-----------------------------------
Rich (BB code):
init:    servo 4,75    ; initialise servo
main:    
    do
        random w0
    loop until w0 <= 10000 and w0 >= 3000
    pause w0
    servopos b.4,75    ; move servo to one end
    pause 2000    ; wait 2 seconds
    servopos b.4,150    ; move servo to middle
    do : loop until pinc.1 = 1
    goto main    ; loop back to start
 
Last edited by a moderator:

thatoneguy

Joined Feb 19, 2009
6,359
To slow down servo movement, you can do something like:

Rich (BB code):
for b5=75 to 150
servopos B.4, b5  'Set servo position to next step 75, 76, 77... to 150
pause 10            ' Wait a little bit
next b5             ' loop again through increment of servo and delay
Change the delay value, or do

Rich (BB code):
for b5=75 to 150 step 5
servopos B.4, b5
pause 10
next b5
To move the servo in larger increments each time through the loop.

See Attached for command reference if that doesn't make sense. You can change b5 to any byte you like (as long as the number to be stored is < 255). Just be sure to change b5 to b<whatever> everywhere in the program, or define it at the top so it can be easily changed (see my program listing above).
 

Attachments

thatoneguy

Joined Feb 19, 2009
6,359
Is it moving to one position to another quickly and stopping, or literally spinning fast?

If it is actually spinning, the servo you have may be a continual rotation type where you can vary the speed and direction, or a broken standard R/C Servo. It is a 3 wire R/C type servo with red, white (or yellow) and black wires coming to a JST header, correct?
 

Thread Starter

MLD

Joined Dec 7, 2012
76
its moving at normal servo speed. Both RC servo im using for these experiments have brown for neg, red for pos. and orange for signal. I have 2 servos a Savox digital servo and a Eflite micro servo. I have tested some codes and some codes don't work with the Savox digital RC servo but do always work with the Eflite micro RC servo. For troubleshooting problems i use the E-flite micro RC servo.

I need to slow down the speed. Below is an example of a code with the servo turning at the speed I want. Again I don't know how to put these codes together, any mistake on the code will give me a syntax error. If you provide me with the completed code will be helpful, (I DO NOT UNDERSTAND basic picaxe programming) as much as you explain it to me its foreign to me the code for Picaxe, I just don't understand it. For all my project im mainly using the PICAXE 20M2 chip.


below is a code with the servo moving at a slower speed but with this code it just rotates back and forth. There is no input switch to give command and there is no random start in this code below. Here it is:

Rich (BB code):
'*** Program constants

symbol servo_delay = 5            'Delay to slow down servo motion (5ms default)

init:
    'set servos to known position
    servo 1,75
    servo 2,75
    servo 4,75
    pause servo_delay
move_forward:
    for b1 = 75 to 150
        servopos 1,b1
        pause servo_delay
    next b1
    for b1 = 75 to 150
        servopos 4,b1
        servopos 2,b1
        pause servo_delay
    next b1
    for b1 = 150 to 75 step -1
        servopos 1,b1
        pause servo_delay
    next b1
    for b1 = 150 to 75 step -1
        servopos 4,b1
        servopos 2,b1
        pause servo_delay
    next b1
    goto move_forward
 
Last edited by a moderator:

thatoneguy

Joined Feb 19, 2009
6,359
If that works, you only need to move your for-next loop inside the if then statement to replace my servo statement with that block of statements.


So you would add two servo for-next loops, one to move servo to top, one to return it, but you can get rid of the extra delay, as the delay is built into your for loop.

pseudo code to mix:

if random>32768
for ....
<your servo code here>
next

for ... <your servo return code here>
next
endif

It's a copy and paste solution. The more you play with it, the more it will make sense, seriously. You learn by tracking down syntax errors and correcting logical errors. You don't learn much by watching written code run, other than the basic structure of language, which you know.
 

thatoneguy

Joined Feb 19, 2009
6,359
Not much time, here's your servo code added into the random loop.

You can remove the pin assignments and have the servos move on different random numbers instead for a complete servo solution.

Rich (BB code):
#picaxe20M2

; More info at:http://forum.allaboutcircuits.com/showthread.php?t=77919
; 
;  This code may be re-used, modified, and shared by anybody, as long as  the information and credits above are not altered or removed.
; Please post improved code ideas and updates to a thread in the electronics chat area of allaboutcircuits.com
; Have fun, remember to wear eye and hearing protection when shooting!



;declarations
dirsB=$FF  ; All port B pins to outputs (20M2 pins 11-18)
dirsC=$0F  ; first 4 ports of port C as outputs (20M2 pins 7-10)
pinsB = %00000000 ; Reset all port B Pins to 0
pinsC=$00        ; Reset all port C outputs to 0

;variables
symbol delaytime = w0  ;delaytime variable  random requires word variables
symbol outs = b10     ; outputs
symbol adjust = b11    ; Potentiometer connected to C.7 (20M2 pin 3) to adjust delays
symbol servo_delay = 5

;initialization
'set servos to known position
servo 1,75
servo 2,75
servo 4,75
pause servo_delay
delaytime = $1F         ;seed pseudo-random generator
;main loop
main:

readadc c.7,adjust    ; Pot connected between V+ and Gnd, wiper connected to C.7 (20M2 pin 3).  Used for adjusting random timing.
adjust = adjust * 100   ; make 0-254 number 0-25400 

random delaytime      ; get pseudo-random number between 0 and 65535
delaytime = delaytime % 7000  ; Mod 7000 to get number between 0 and 7000
;delaytime = delaytime + 3000  ;add 3000 for 3-10second delaytime (depreciated by user potentiometer addition)
delaytime = delaytime + adjust ; add user chosen delaytime to delay.

pause delaytime          ;pause for 0-26,100 milliseconds (0-26 seconds, user set potentiometer + 0-7 seconds)

random delaytime        ; get new random number for output states
outs = delaytime/256   ;use first 8 bits of delaytime word for random port b outputs
pinsB= outs           ;set portB pins high or low psuedo-random (20M2 pins 11-18)
if delaytime > 32768 then
    for b1 = 75 to 150
        servopos 1,b1
        pause servo_delay
    next b1
    for b1 = 75 to 150
        servopos 4,b1
        servopos 2,b1
        pause servo_delay
    next b1
    for b1 = 150 to 75 step -1
        servopos 1,b1
        pause servo_delay
    next b1
    for b1 = 150 to 75 step -1
        servopos 4,b1
        servopos 2,b1
        pause servo_delay
    next b1



'  servopos B.4,225 ; move servo to up position
'  pause 300 ; wait for servo to finish pushing target up
'  servopos B.4,75  ; return servo to idle position
'  pause 300 ;  wait for servo to finish moving
endif

random delaytime         ; get new random number for output states
outs = delaytime/512    ;use last 4 bits of delaytime word for random port c outputs
pinsC= outs           ;set portC pins high or low psuedo-random (20M2 pins 7-10)

pause 500              ;wait ½ second to ensure all chosen targets to reset are up
pinsB = $06              ;disable outputs  other than Servo on B.4 after the ½ second delay so cylinders aren't pushing on target
pinsC = $00              ;disable outputs after the ½ second delay so cylinders aren't pushing on target

goto main               ; keep doing it until power is removed.
 
Last edited:

Thread Starter

MLD

Joined Dec 7, 2012
76
"thatoneguy" I tried the source code above and it doesn't work. The source code for what I want it to do should be short. This is too much code for what I want it to do.


This is the simplest code i seen so far below, all i basically need is to slow down the servos movement which is not in this code below

init: servo 4,75 ; initialise servo
main:
do
random w0
loop until w0 <= 10000 and w0 >= 3000
pause w0
servopos b.4,75 ; move servo to one end
pause 2000 ; wait 2 seconds
servopos b.4,150 ; move servo to middle
do : loop until pinc.1 = 1
goto main ; loop back to start
 
Top