Inconsistent readings from a HC-SR40

Mustachio_01.bas (2721Bytes)

Stumped again! 

I am building a new little guy and I am getting consistently bad readings now that I have added more code to the program. Let me explain....

I double checked that the hardware is ok. 

I tested the different elements seperately: Speaker, motors, sensor, etc. The sensor was tested using the little piece of code I keep around, inspired from the YDMII.

symbol trig = 4 

symbol echo = 5 

symbol range = w6 

Symbol beep = 6

 

puls:

high  trig

pause 10

low   trig

pulsin echo,1,range 

 

debug

if range < 250 then

sound beep,(range,10) low beep

 

endif

goto puls

 

It works pretty much flawlessly and lets me use the sensor and speaker as a theramin-like instrument. The problem comes when I put even the simplest code in. I instantly geta steady stream of either "0" or sometimes "1" as a value from the sensor. 

I tried commenting out the less important parts of the code one after the other until I a left with the bare minimum and yet I am still left with a robot who constantly feels like there's something in front. I attached my current code as I may have left a critical error int here. 

I also tried swapping the sensor out for another ( I bought a box of 6 of those SR04's a while back). I get the same problem.

I am sure someone out there has the piece of info I am missing.

 

Is PICAXE BASIC

case sensitive?

symbol dangerLevel = 150
if range < DangerLevel then

You might want to try

low trig
pause 2
  
high trig
pause 10

pulsin echo, 1, range



HC-SR04

the hc-sr04 has also a minimum range, which is about 0-4cm, readings with objects in that range may be inconsistent.

Yes, I noticed.When I am

Yes, I noticed.

When I am testing, however the sensor is looking at a wall about 3 meters away. I just wave my hand in front to try different ranges. So… that’s probably not it.

Nice catch. Fixed it but it

Nice catch. Fixed it but it did not change anything. I think it’s not case sensitive.

Tried it and got the exact

Tried it and got the exact same result. A series of 0’s and a few 1’s in there. Nothing else.

Power has to be your issue
Power has to be your issue then. I would use this sequence to do the triggering as this is more in line with the data sheet in comparison to what you had.