Here's code for the 18f25k22 to control an object avoiding robot. It was converted from picaxe basic, https://youtu.be/DZVkTLNGYs8 It uses timer 1 interrupt to run the servo.
;ultrasonic on servo robot
#chip18F25K22,16#configOSC=INTIO67;DimServo_PosasWORDDimTIME_BASEaliasCCPR1H,CCPR1LASWORD'// Programmable "Compare" ValueCCP1CON = b'00001011' '//ConfigCCP1forCompare:SpclEventtrigger;TIME_BASE=20000'// Make period timer = 20ms (2000us);On Interrupt CCP1 Call Servo_Pulse ;every 20ms;InitTimer1 OSC,PS1_4 '//Configuretimer1withPrescaleof1:4ClearTimer1'//Make Sure timer is cleared to 0StartTimer 1 '//Startthetimer;#defineServo_PinPORTb.7'//ServosignalOnPIN23#defineecho=portB.1;ultrasonicsensortrigpin.echopinjoinedtotrigby1.8kresistor#definelmotordira=portA.0;h-bridgemotorpins#definelmotordirb=portA.1#definermotordira=portA.2#definermotordirb=portA.3dirlmotordiraout:dirlmotordirbout:dirrmotordiraout:dirrmotordirboutdirservo_pinout[word]saferange=420radardir=1Servo_POS=150;---- point radar aheadwait5000ms;fortmp=1to40gosubrotate_radarwait100msnext;main:;----------------------- START ------------------gosubmotors_forwardgosubrotate_radar;;---- set saferange for different directionsifradarpos>=130andradarpos<=170thensaferange=430;frontelsesaferange=470;leftandrightendif;;--- test for obstaclesifrange>saferangethengotomainendif;---- ahead is clear;;aheadblockedgosubbrakeifradarpos>170then;--- blocked leftgototurnrightelseifradarpos<130then;--- blocked rightgototurnleftendif;;---- ahead blocked so random test turn left or righttmp=Randomiftmp<128thenservo_pos=100;pointradarfarrightgosubpingifrange>saferangethen;---- right is cleargototurnrightelsegototurnleftendifelseservo_pos=200;---- point radar far leftgosubpingifrange>saferangethen;---- left is cleargototurnleftelsegototurnrightendifendif;......................................................turnright:saferange=470:servo_pos=150;---- point radar ahead;do:gosubscanrotateright:loopuntilrange>saferangeservo_pos=170;---- point radar more left;do:gosubscanrotateright:loopuntilrange>saferangeservo_pos=215;---- point radar full left;do:gosubscanrotateright:loopuntilrange>saferange;---- spin clockwise until no object to full left;radardir=1:servo_pos=126;---- point radar rightgotomain;......................................................turnleft:saferange=470:servo_pos=150;----point radar ahead;do:gosubscanrotateleft:loopuntilrange>saferange;---- spin anti-clockwise until no object aheadservo_pos=110;---- point radar right;do:gosubscanrotateleft:loopuntilrange>saferange;---- spin anti-clockwise until no object to more rightservo_pos=85;---- point radar full right;do:gosubscanrotateleft:loopuntilrange>saferange;---- spin anti-clockwise until no object to full right;radardir=0:servo_pos=174;---- point radar leftgotomain;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~subServo_PulsePulseOutServo_Pin,Servo_Posusendsubscanrotateleft:gosubpinggosubrotate_anticlockwisewait7msgosubmotors_offreturnscanrotateright:gosubpinggosubrotate_clockwisewait7msgosubmotors_offreturnbrake:gosubmotors_reversewait10msgosubmotors_offreturnrotate_clockwise:setlmotordiraon;leftmotorforwardsetlmotordiraoffsetrmotordiraoff;rightmotorbackwardsetrmotordiraonreturnrotate_anticlockwise:setlmotordiraoff;leftmotorbackwardsetlmotordiraonsetrmotordiraon;rightmotorforwardsetrmotordiraoffreturnmotors_forward:setlmotordiraon;leftmotorforwardsetlmotordiraoffsetrmotordiraon;rightmotorforwardsetrmotordiraoffreturnmotors_off:setlmotordiraoff;leftmotoroffsetlmotordiraoffsetrmotordiraoff;rightmotoroffsetrmotordiraoffreturnmotors_reverse:setlmotordiraoff;leftmotorbackwardsetlmotordiraonsetrmotordiraoff;rightmotorbackwardsetrmotordiraonreturnrotate_radar:gosubping;ifradardir=1thenservo_pos=radarpos+8ifservo_pos=222thenradardir=0endifelseservo_pos=radarpos-8ifservo_pos=78thenradardir=1endifendifreturnping:retry:direchooutsetechoondirechoinpulseinecho,range,1mswait10msifrange=0thengotoretryendif;----out of rangereturn
Last edit: Anobium 2017-02-15
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Picaxe basic,sorry. I'll post later. It's a "learn GCB" project, Commands have different names to picaxe. The robot code works better than some I've seen. GBC speed is not needed as there are 10ms pauses in the code to allow the US sensor to settle.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Here's code for the 18f25k22 to control an object avoiding robot. It was converted from picaxe basic, https://youtu.be/DZVkTLNGYs8 It uses timer 1 interrupt to run the servo.
;ultrasonic on servo robot
Last edit: Anobium 2017-02-15
Great job. Is the video Great Cow BASIC or Picaxe?
Picaxe basic,sorry. I'll post later. It's a "learn GCB" project, Commands have different names to picaxe. The robot code works better than some I've seen. GBC speed is not needed as there are 10ms pauses in the code to allow the US sensor to settle.