I am currently working on UltraSonic Sensor based Distance Measurement. I am using the sensor HCSR04 for that. With help of Danaaknight, I have designed the attached project, but it is not working as I am not getting the distance. The PSoC4 pins of trigger, echo and the sensor's results are checked on CRO. They are tested OK. I think the problem lies in measuring the time period on the Echo pin. Everybody is welcome to give solution.
are you facing a problem with the display, the formatting of the result or the values you compute?
Which hardware does the project run on?
It is always advisable to have a main loop running at a constant frequency instead of a loop that is governed by external hardware. In this case: the smaller the distance, the faster the loop will run. To avoid this and to shorten software, use a PWM that generates a 10µs pulse at the desired frequency of 40Hz, trigger the sensor with that pulse, clear/load the timer and so on.
Your documentation is very good, although I didn't find (maybe overlooked) the underlying hardware specs (board: Kit or custom)
Your program structure may be better using an initialization part, a sensor handler, a display handler and a handler for calculating distance. For more accuracy: Largest error influence comes from temperature, wikipedia helds a formula for that and the chip has got a raw measurement capability. Especially in your country where environment temps are comparably high the difference betweein conditioned inside and on the street may be remarkable.
Thank you for the reply. I just debugged the code and found that the code gives me distance when I "watched" the variable in debugging environment. It means the LCD output is not coming properly. I checked other projects with LCD, they are running well.
Anything on the LCD? Is the "Distance =" showing? -> Hardware issue.
There is some peculiar about "print float in PSoC4", have a keyword search (at top of this page) for that and take the newer infos from Robyn.
I got it working by putting integer %d in sprintf instead of float %f. I did not know that float does not work with sprintf.
I did the steps given on the above link to get sprintf working with float, but I am afraid it is still not working.
I am using the Creator 3.2 early access and there is in the project settings an item to enable float formatting. This option costs you 14kB of flash. If that is too much, you may find some libraries using quite less program memory. Alternatively convert the float yourself by converting the fractional part separately as integer with the required precision
Intpart = (uint)MyFloat;
FracPart = (uint)((MyFloat-Intpart)*1000);
Will give your MyFloat converted with 3 decimal places. You must care for the sign separately.
If you want to avoide sprintf( ) altogether -
Not sure of its general capability or limitations.