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

high 0
                                                ‘Bring RB0 high to light LED
                                                        1
                                                  ‘Wait    second
                                       pause 250
                                                         4
                                       low 0
                                                        1
                                       pause 250 ‘Bring RB0 low to turn off LED
                                                  ‘Wait    second
                                                         4
                                       goto start  ‘Check switch again
                                       The program for the PICBASIC Pro compiler is as follows:
                                       ‘REM PICBASIC Compiler Pro
                                       ‘Rem test switch low
                                       input portb.4  ‘Set pin RB4 to read switch
                                       start:
                                       if portb.4   0 then blink  ‘If switch is low, then blink LED
                                       goto start  ‘If not, check again
                                       blink:   ‘Blink LED routine
                                       high 0   ‘Bring RB0 high to light LED
                                                        1
                                       pause 250  ‘Wait    second
                                                         4
                                       low 0   ‘Bring RB0 low to turn off LED
                                                        1
                                       pause 250  ‘Wait    second
                                                         4
                                       goto start  ‘Check switch again
                                       The schematic for the read-switch-low circuit is shown in Fig. 6.21.
                                       The switch is connected to an I/O pin labeled RB4. The LED is      121
                                       connected to RB0 through a 470-ohm current-limiting resistor.
                               Reading switches—logic high
                                       These programs and schematic are the complement to the previous
                                       examples. Look back to Fig. 6.20, example B. The switch labeled B
                                       keeps the I/O pin at a logic low level. When the switch is closed, the
                                       I/O pin is brought to a logic high level.




















                                         6.21 Schematic for read-switch-low circuit

                                                       Team LRN                                      Intelligence
   137   138   139   140   141   142   143   144   145   146   147