'------------------------------------------------------ ' Name : remote-sonar.bas ' Compiler : PicBasic Pro - MicroEngineering Labs ' Notes : Remote control with sonar avoidance. '------------------------------------------------------ ' PortA set as outputs. Pin 1 input. trisa = %00000010 ' PortB set as outputs. pin 0 input. trisb = %00000001 '------------------------------------------------------ ' initialize variables include "modedefs.bas" rx_baud CON N2400 rxmit VAR PORTB.0 enable_right VAR PORTB.1 forward_right VAR PORTB.2 reverse_right VAR PORTB.3 enable_left VAR PORTB.4 reverse_left VAR PORTB.5 forward_left VAR PORTB.6 trigger VAR PORTA.0 echo VAR PORTA.1 piezo VAR PORTA.3 control VAR BYTE temp VAR BYTE dist_raw VAR WORD dist_inch VAR WORD conv_inch CON 15 low enable_left low forward_left low reverse_left low enable_right low forward_right low reverse_right SOUND PIEZO,[115,10,50,10] start: serin rxmit,rx_baud,["Z"],control if control = "A" then gosub sr_sonar if dist_inch < 8 then start gosub forward endif if control = "B" then gosub backwards endif if control = "C" then gosub turn_left endif if control = "D" then gosub turn_right endif if control = "E" then sound piezo,[115,10,50,10] endif if control = "F" then low enable_left low forward_left low reverse_left low enable_right low forward_right low reverse_right endif goto start '------------------------------------------------------ ' movement subroutines forward: high enable_left high forward_left high enable_right high forward_right pause 500 low enable_left low forward_left low enable_right low forward_right return '------------------------------------------------------ turn_left: high enable_left high forward_left high enable_right high reverse_right return '------------------------------------------------------ backwards: high enable_left high reverse_left high enable_right high reverse_right return '------------------------------------------------------ turn_right: high enable_left high reverse_left high enable_right high forward_right return '------------------------------------------------------ sr_sonar: pulsout trigger,1 pulsin echo,1,dist_raw dist_inch = (dist_raw/conv_inch) pause 10 return end