Possible ultrasonic programs - will they work?

mham123

New Member
I am quite new to PICAXE and PICAXE programming, and not very experienced in programming the chips. I am trying to write a program to directly control a servo's position using an ultrasonic sensor - as you move your hand closer to the sensor, the servo rotates one way, and as you move it away, it rotates the other way.

So, could anyone tell me:
1. Would either of these actually work?
2. Which would would, in your opinion, be the best?

Code:
symbol angle = b2
		 init: servo 1, 150
		 symbol servop = b3
		 symbol echo = 6
		 symbol trig = 3
		 symbol range = w1
		 symbol range_true = w2
		 main:
		
label_8B:	gosub SCAN
		if b0< 100 then label_69
		goto label_8B

label_69:	gosub SCAN
		pause 10
		gosub SCAN2
		if b0>b10 then label_D2
label_ED:    let range_diff = range_true - range_true2
		 let range_diff = range_diff * 3 / 2
   	       let angle = angle - range_diff
		 servo, 1, angle
		goto main

label_D2:	 let range_diff = range_true2 - range_true
		 let range_diff = range_diff * 3 / 2
		 let angle = angle + range_diff
		 servo, 1, angle
		goto main

SCAN:	
		 pulsout trig,2
		 pulsin echo,1,range
		 pause 10
		 let range_true = range * 10 / 58
		 pause 50
		return
		
SCAN2:	
		 pulsout trig,2
		 pulsin echo,1,range
		 pause 10
		 let range_true2 = range * 10 / 58
		 pause 50
		return
Code:
main:
		 symbol angle = b2
		 init: servo 1, 150
		 symbol servop = b3
		 symbol echo = 6
		 symbol trig = 3
		 symbol range = w1
		 symbol range_true = w2
label_98:	gosub 
		if range_true < 100 then label_6B
		goto label_98

label_6B:	gosub SCAN
		let angle = range * 3 / 2
		 servopos 1, b2
		goto label_98

SCAN:	
		 pulsout trig,2
		 pulsin echo,1,range
		 pause 10
		 let range_true = range * 10 / 58
		 pause 50
		return
Thanks for any help.
 

inglewoodpete

Senior Member
First, welcome to the forum.

As you you say, you are new to programming PICAXEs. When learning the idiosyncrasies of the PICAXE (or any new system), start small. Write and test a very basic routine. In your case, write 2 test routines: 1 based on learning how to control a servo and another to drive your ultrasonic hardware. Use "Debug" or "SerTxd" commands (tempoarily) to output the register values at suitable points, so that you can determine what is happening inside the PICAXE.

Some tips on coding:
* The 'symbol' statements are best moved to the top of the file so that they don't clutter the executable code.
* Add comments to your code so that others can get an idea of what a block of code or individual command is trying to do.
* Have you done a syntax check on your code? I can see at least 1 error.

"an ultrasonic sensor" is not very clear to other users who are trying to help. Can you give details Eg make, model?
 
Last edited:
Top