;PicOne Starter Pack 2.bas '========================= ' 27/1/2020 ; updated for Pic One ; wheel counters #rem EXAMPLES OF CODE LEDs ==== High 4 ; Right LED on Low 4 ; Right LED off High 6 ; centre LED on Low 6 ; centre LED off High 7 ; Left LED on Low 7 ; Left LED off Wall sensors ============ Readadc 0,b0 ; left wall sensor Readadc 1,b1 ; front wall sensor Readadc 2,b2 ; right sensor *sensor gets number between 0 and 255 depending on its distance from object sensed. Line sensor =========== Readadc 3,b3 ; line (...) sensor Reverse right motor ============== high 1 ; reverses right motor relay. It can only reverse the right motor meaning you can only spin right. If you do high 1 then the right motor will go backwards at the speed you programmed. If you do low 1 then it puts the right wheel back to going forward. Read wall Sensors: ================== high portc 5 ; turns on Infra red illumination readadc 0,b0 readadc 1,b1 readadc 2,b2 low portc 5 ; turns off Infra red illumination Motors: ======= low portc 1 ;right motor off high portc 2 ;left motor on pwmout 1,255,1023 ; right_motor full speed pwmout 1,255,512 ; right_motor half speed pwmout 1,255,0 ; right_motor stop pwmout 2,255,1023 ; left full speed pwmout 2,255,512 ; left half speed pwmout 2,255,0 ; left stop pwmout 2,255,left_motor Push buttons: ============= if pin6=0 then ; if start button pressed do while pin6 =1 : loop ; wait for start button Wheel Sensors: do while pin0 =1 : loop ; wait for left wheel counter do while pin3 =1 : loop ; wait for right wheel counter ; TV remote irin c.7,b0 ; wait for TV remote key END OF EXAMPLES #endrem ' 'DIRECTIVES #picaxe 28X1 #no_table 'No data download ' 'SYMBOLS FOR INPUTS/OUTPUTS symbol startbutton = pin6 'start button symbol rmotor = 1 symbol lmotor = 2 symbol LED1 = 7 ; left LED symbol LED2 = 6 ; centre LED symbol LED3 = 4 ; right LED symbol Lwheel = pin0 ' left wheel state symbol Rwheel = pin3 ' right wheel state symbol TVR = 7 ; c.7 TV remote ; symbols must use Channel numbers for readadc symbol Fsensor = 3 ; A.3 'SYMBOLS FOR Useful numbers symbol stopspeed = 0 ; PWM output to stop the motors symbol centre = 128 ; Line sensor centre reading symbol Kp =10 ; amount of Derivative. symbol leftish = 80 symbol rightish = 160 'DECLARE SYMBOLS FOR VARIABLES symbol leftvalue = b0 ; left wall sensor symbol frontvalue = b1 ; front wall sensor symbol rightvalue = b2 ; right wall sensor symbol leftspeed = W4 symbol rightspeed = W5 symbol value = W6 symbol oldvalue = W7 symbol correction = W8 symbol corrected = W9 ; Sensor reading after Derivative applied symbol left_count = b24 symbol right_count = b25 symbol Lwheelstate = b26 symbol Rwheelstate = b27 symbol lastbutton = B26 symbol thisbutton = B27 '************************************************************************ ' INITIALISATION '************************************************************************ gosub setup ; loop to await start button, displaying sensor do gosub wallsensors if leftvalue > 43 then high LED1 ; left else low LED1 endif if b1 > 5 then high LED2 ; middle else low LED2 endif if b2 > 5 then high LED3 ; right else low LED3 endif debug ; debug takes about 500 ms! loop while startbutton = 1 ; loop while button not pressed gosub ronan ; loop counting wheel sensors top: Lwheelstate = 0 Rwheelstate = 0 read 0,word leftspeed ; = 200 read 2,word rightspeed ;= 200 read 4,word W0 left_count = W0 ; set with Vol + right_count = 0 do ; *******************Your program**************************** gosub Count_Right gosub Count_Left if left_count >200 then rightspeed = 0 leftspeed = 0 endif ; *********************************************** gosub checkspeeds ; check speed values gosub setmotors ; output speeds to motors loop ;====================SUBROUTINES=============== checkspeeds: if leftspeed > 32767 then ; check for underflow leftspeed = 0 endif leftspeed = leftspeed max 1023 ; limit max positive number if rightspeed > 32767 then ; check for underflow rightspeed = 0 endif rightspeed = rightspeed max 1023 ; limit max positive number return ;================= setmotors: Pwmout 1 ,255,rightspeed Pwmout 2 ,255,leftspeed return ;================= setup: setfreq m8 'run faster ' stop motors Pwmout 1 ,255,0 Pwmout 2 ,255,0 sertxd("PicOne Starter Pack 2 counts displ.bas.txt",13,10) ' send message to Terminal window return ;============== Count_Left: if pin0 = 1 then high 7 if Lwheelstate =1 then return endif Lwheelstate =1 else low 7 if Lwheelstate =0 then return endif Lwheelstate =0 inc left_count endif return ;============== Count_Right: if pin3 = 1 then high 4 if Rwheelstate =1 then return endif Rwheelstate =1 else low 4 if Rwheelstate =0 then return endif Rwheelstate =0 inc right_count endif return ;===================== ronan: if pinc.7=0 then return 'return if TV receiver powered down end if high LED1 ; startbutton - 0 on entry pause 1000 ; check for long press if startbutton = 1 then ; if button released in time return ; then return endif high LED2 w1 = 0 do irin TVR, thisbutton ' Wham! high LED3 pause 1000 low LED3 select thisbutton case <= 9 if lastbutton > 9 then w1 = 0 endif if thisbutton = 9 then thisbutton=0 else thisbutton = thisbutton+1 'on simulation, remove this line. on remote, include this line. endif w1 = w1 * 10 + thisbutton 'multiplies by 10 first case = 21 'if power button is pressed return ; from ronan case = 16 'CHA+ write 0,word w1 read 0, word w2 case = 17 'CHA- write 2,word w1 read 2, word w3 case = 18 'VOL+ write 4,word w1 read 4, word w4 case = 19 'VOL- write 6,word w1 read 6, word w5 case = 20 'MUTE write 8,word w1 read 8, word w6 case = 101 ' OK , reads all values and debugs. read 0, word w0 read 2, word w1 read 4, word w2 read 6, word w3 read 8, word w4 debug thisbutton = 101 endselect lastbutton = thisbutton loop wallsensors: high portc 5 'IR leds on readadc 0,leftvalue 'read left wall sensor readadc 1,frontvalue 'read front wall sensor readadc 2,rightvalue 'read right wall sensor low portc 5 'IR leds off return ; end of program