' ========================================================================= ' ' File....... Random_Servos.bs1 ' Purpose.... Randomly turn 3 servos ' Author..... Robert Hole (based on the work of Vern Graner) ' Web..... www.halloween.cindybob.com ' Started.... 20 Mar 2006 ' Updated.... 22 Mar 2006 ' ' {$STAMP BS1} ' {$PBASIC 1.0} ' ' ========================================================================= ' -----[ Program Description ]--------------------------------------------- ' Pan 3 servos left and right a random amount. How often a particular servo ' will be in motion is a function of the chance number. A higher number means ' less chance that a servo will be in motion. ' -----[ Revision History ]------------------------------------------------ ' -----[ I/O Definitions ]------------------------------------------------- SYMBOL Servo1 = 0 ' Servo1 Pin # SYMBOL Servo2 = 1 ' Servo2 Pin # SYMBOL Servo3 = 2 ' Servo3 Pin # ' -----[ Constants ]------------------------------------------------------- SYMBOL Chance = 199 ' Set the chance of any servo being in motion. ' The higher the number the last chance. ' -----[ Variables ]------------------------------------------------------- SYMBOL lottery = W0 ' random number SYMBOL rUpdate = W5 ' random number SYMBOL update = B8 ' update or not varialbe SYMBOL pos1 = B2 ' servo1 postion SYMBOL dest1 = B3 ' servo1 destination SYMBOL pos2 = B4 ' servo2 postion SYMBOL dest2 = B5 ' servo2 destination SYMBOL pos3 = B6 ' servo3 postion SYMBOL dest3 = B7 ' servo3 destination ' -----[ Initialization ]-------------------------------------------------- Reset: DIRS = %00000111 ' Change if the servos are not on pins 0,1,2 lottery = 1031 ' Seed the random number rUpdate = 2302 ' Seed the random number dest1=120 ' Center the servos. These pos1=120 ' numbers may need to changed dest2=150 ' depending on the servos used pos2=150 ' and any deviation caused when dest3=160 ' mounting the servos. pos3=160 'Center the servos FOR update=100 TO dest1 PULSOUT Servo1, update PAUSE 20 NEXT FOR update=100 TO dest2 PULSOUT Servo2, update PAUSE 20 NEXT FOR update=100 TO dest3 PULSOUT Servo3, update PAUSE 20 NEXT ' -----[ Program Code ]---------------------------------------------------- Main: PAUSE 10 ' Speed, more pause = slower ' ========= ' Servo 1 ' ========= Check_Dest1: RANDOM lottery IF pos1 <> dest1 THEN Update_Pos1 ' If not at dest then update the servo position? RANDOM rUpdate ' At dest so randomly decide if the servo should update=rUpdate // chance ' go again. IF update<>1 THEN Check_Dest2: ' No match so maintain current position dest1 = lottery // 250 ' Match so get a new dest. dest1 = dest1 MIN 70 ' Confine to minimum and maximum limits. These dest1 = dest1 MAX 180 ' limits may need to be changed to suit your ' situation and taste. Experiment with different numbers. Update_Pos1: IF pos1 < dest1 THEN Go_CW1 ' Decide if the servo should move CW or CCW pos1 = pos1 - 2 ' Move servo CCW Go_CW1: pos1 = pos1 + 1 ' Move servo CW PULSOUT Servo1, pos1 ' Move the servo ' ========= ' Servo 2 ' ========= Check_Dest2: RANDOM lottery IF pos2 <> dest2 THEN Update_Pos2 RANDOM rUpdate update=rUpdate // chance IF update<>2 THEN Check_Dest3: dest2 = lottery // 250 dest2 = dest2 MIN 90 dest2 = dest2 MAX 200 Update_Pos2: IF pos2 < dest2 THEN Go_CW2 pos2 = pos2 - 2 Go_CW2: pos2 = pos2 + 1 PULSOUT Servo2, pos2 ' ========= ' Servo 3 ' ========= Check_Dest3: RANDOM lottery IF pos3 <> dest3 THEN Update_Pos3 RANDOM rUpdate update=rUpdate // chance IF update<>3 THEN Main: dest3 = lottery // 250 dest3 = dest3 MIN 90 dest3 = dest3 MAX 200 Update_Pos3: IF pos3 < dest3 THEN Go_CW3 pos3 = pos3 - 2 Go_CW3: pos3 = pos3 + 1 PULSOUT Servo3, pos3 GOTO Main ' Start again END ' -----[ Subroutines ]-----------------------------------------------------