Page 248 - Robots Androids and Animatrons : 12 Incredible Projects You Can Build
P. 248

‘Limit delay to 250 ms
                                       if ti > 250 then hold1
                                                      ‘Check switch status
                                       peek porta,b0
                                       if bit0 = 0 then loop1
                                                               ‘Still increasing delay?
                                       goto check  ‘If not, jump to main switch status
                                       loop2:   ‘Decrease delay
                                       poke portb,0  ‘Turn off transistors
                                       ti = ti   5   ‘Decrease delay by 5 ms
                                       pause 50  ‘Pause a moment
                                       if ti < 20 then hold2  ‘Limit delay to 20 ms
                                       peek porta,b0  ‘Check switch status
                                       if bit1 = 0 then loop2  ‘Still decreasing delay?
                                       goto check  ‘If not, jump to main switch status
                                       hold1:   ‘Limit upper delay
                                       ti = 245  ‘to 250 ms
                                       goto loop1  ‘Go back
                                       hold2:   ‘Limit lower delay
                                       ti = 25   ‘to 25 ms
                                       goto loop2  ‘Go back
                                       hold3:   ‘Stop stepper motor
                                       poke portb,0  ‘Turn off transistor
                                       peek porta, b0  ‘Check switches
                                       if bit2 = 0 then hold3  ‘Keep motor off?                           227
                                       goto check  ‘If not, go to main switch status
                                       check
                                       The schematic for this program is shown in Fig. 10.13. In the photo-
                                       graph of the circuit (see Fig. 10.14), the four switches are difficult
                                       to make out. There are the four bare wire strips behind the PIC
                                       microcontroller.  The  top  sides  of  the  bare  wire  strips  are  con-
                                       nected  to   5V  through  10K-ohm  resistors.  A  wire  from  each
                                       switch is connected to the appropriate pin on port A. A single wire
                                       is connected to ground and is used to close any of the switches by
                                       touching the bare wire strip.
                                       Half stepping
                                       Half stepping the motor will effectively double the resolution. In
                                       this instance it requires 400 pulses to complete one rotation. Table
                                       10.4 shows the switching logic needed in a program. When you
                                       reach the end of the table, the sequence repeats starting back at
                                       the top of the table.
                                       The ti delay variable
                                       The ti variable used in each PICBASIC program controls a delay
                                       pause whose purpose is to slow down the output sequence to port
                                       B. Without the pause, the sequence may run too fast for the stepper
                                       motor to respond, causing the stepper motor to malfunction.
                                                       Team LRN                                  Mobile platforms
   243   244   245   246   247   248   249   250   251   252   253