Two SRF05 ultrasonic sensor

#1
Hi, I've gotten one SRF05 sensor working and giving an LED output, however, I can't seem to work out a code which allows two SRF05 to input and each one turning on a single LED. Any help, please? I am using two SRF05 ultrasonic sensor and an 18M2 Protoboard.

Here is my code for one sensor:


symbol trig1 = B.3 'Define output pin for Trigger pulse
symbol echo1 = C.6 'Define input pin for Echo pulse
symbol range1 = w1 '16 bit word variable for range


rightled:
high 2
pause 1
goto main


main:
low 0
low 1
low 2
pulsout trig,2 'produces 20uS trigger pulse (must be minimum of 10uS)
pulsin echo,1,range 'measures the range in 10uS steps
pause 10 'SRF004 mandatory 10mS recharge period after ranging completes
'now convert to cm (divide by 6.2) or inches (divide by 14.9)
'as picaxe cannot use 6.2, multiply by 10 then divide by 62 instead
let range = range*10/62 'multiply by 10 then divide by 62
if range>40 then main
if w1>0 then leftled
debug range
goto main
 

Attachments

hippy

Technical Support
Staff member
#2
For a single ultrasound sensor the code would normally be something like -

Code:
Symbol TRIG  = B.2
Symbol ECHO  = C.5
Symbol range = w1

Main:
  PulsOut TRIG,2
  PulsIn  ECHO,1,range
  range = range * 10 / 62
  ; Do something with range here
  Pause 20
  Goto Main
For two sensors you would just do everything twice over. Something like -

Code:
Symbol TRIG1  = B.3
Symbol ECHO1  = C.6
Symbol range1 = w1

Symbol TRIG2  = B.2
Symbol ECHO2  = C.5
Symbol range2 = w2

Main:
  PulsOut TRIG1,2
  PulsIn  ECHO1,1,range1
  range1 = range1 * 10 / 62
  ; Do something with range1 here
  PulsOut TRIG2,2
  PulsIn  ECHO2,1,range2
  range2 = range2 * 10 / 62
  ; Do something with range2 here
  Pause 20
  Goto Main
 
#3
For a single ultrasound sensor the code would normally be something like -

Code:
Symbol TRIG  = B.2
Symbol ECHO  = C.5
Symbol range = w1

Main:
  PulsOut TRIG,2
  PulsIn  ECHO,1,range
  range = range * 10 / 62
  ; Do something with range here
  Pause 20
  Goto Main
For two sensors you would just do everything twice over. Something like -

Code:
Symbol TRIG1  = B.3
Symbol ECHO1  = C.6
Symbol range1 = w1

Symbol TRIG2  = B.2
Symbol ECHO2  = C.5
Symbol range2 = w2

Main:
  PulsOut TRIG1,2
  PulsIn  ECHO1,1,range1
  range1 = range1 * 10 / 62
  ; Do something with range1 here
  PulsOut TRIG2,2
  PulsIn  ECHO2,1,range2
  range2 = range2 * 10 / 62
  ; Do something with range2 here
  Pause 20
  Goto Main
Thank you for helping me out here however I have used the code you have given me and the two sensors are still not turning individual LED on.
Here is what I have done
Code:
Symbol TRIG1  = B.3
Symbol ECHO1  = C.6
Symbol range1 = w1

Symbol TRIG2  = B.4
Symbol ECHO2  = C.5
Symbol range2 = w2

Main:
  low 0
  low 1
  low 2
  PulsOut TRIG1,2
  PulsIn  ECHO1,1,range1
  range1 = range1 * 10 / 62
  if w1>0 then leftled
  PulsOut TRIG2,2
  PulsIn  ECHO2,1,range2
  range2 = range2 * 10 / 62
  if w2>0 then rightled
  Pause 20
  Goto Main
  
leftled:
high 1
goto main
  
rightled:
high 2
goto main
 

erco

Senior Member
#4
Signals from multiple ultrasonic sensors may interfere with each other, even if pointed in different directions. Also there is a power spike when an outgoing ultrasonic pulse is generated, which could cause supply problems if they are used simultaneously. Might work best to alternate sensors.
 

hippy

Technical Support
Staff member
#5
When you "if w1>0 then leftled" that 'leftled:' routine then goes back to main so the other sensor is never read. You could try something like ...

Code:
Main:
  low B.0
  low B.1
  low B.2
  PulsOut TRIG1,2
  PulsIn  ECHO1,1,range1
  range1 = range1 * 10 / 62
  if range1 > 0 then
    high B.1
  end if
  PulsOut TRIG2,2
  PulsIn  ECHO2,1,range2
  range2 = range2 * 10 / 62
  if range2 > 0 then
    high B.2
  end if
  Pause 20
  Goto Main
 

AllyCat

Senior Member
#6
Hi,
Here is what I have done ......
Note also that your original program flow is effectively:
Code:
leftled:
  high 1
goto main
Main:
  low 0
  low 1
So the LED would flash only very briefly (a few ms).

A PAUSE would help, but a better solution is to use an IF .. THEN ... ELSE ... ENDIF structure to drive the LED(s) either High or Low.

Cheers, Alan.
 
Last edited:

Aries

New Member
#7
Or you could make the leftled:, rightled: sections into subroutines, replace "goto xxxled" by "gosub xxxled" and replace "goto main" with "return". You can then expand the subroutines if required without interfering with the clarity of the flow in the main program.
 
Top