Help with my code for my water level controller.

seaninnes

New Member
good morning,
I was just seeing if anyone could help with my code. The problem I am having is that once the water reaches a certain level < 30cm away from the sensor, I have a pump (B.2) which starts, the problem I am having is I don't know how to keep my ultrasonic range sensor calculating the distance whilst the pump is on.

Here is my code:
symbol SIG = C.2 ;Define pin for trigger and echo
symbol range = w1 ;16 bit word variable for range

main:
pulsout SIG,2 ;produce 20uS trigger pulse (must be minimum of 10uS)
pulsin SIG,1,range ;measure the range in 10uS steps
;now convert range to cm (devide by 5.8) or inches (devide by 14.8)
;as picaxe cannot use 5.8, multiply by 10 then devide by 58 instead
let range = range * 10 / 58 ;multiply by 10 then devide by 58
debug range ; display range via debug command
pause 50 ;short delay
if w1 > 30 then pump ;if the distance of the water is greater than 30 cm from the sensor then go to motor
goto main ;loop around forever

pump:
high B.2 ;Start B2
if w1 < 12 then main ;if the distance of the water is less than 12cm from the sensor then goto main


If anyone could help that would be great.
Thanks
 

hippy

Technical Support
Staff member
You need to re-read the ultrasonic sensor and recalculate the level in your 'pump' loop as that is the only way the PICAXE program can tell the level has changed once you have turned the pump on.
 

techElder

Well-known member
seaninnes, welcome to the forum!

You didn't say what your "ultrasonic range sensor" was. Do you have a datasheet that you can attach?

First, your program is quite workable, but you can improve it.

Your program needs to be in a constant level measuring loop. You almost have that, except that you do "if w1 > 30 then pump" which is a GOTO out of the loop and to an END.

The "if w1 < 12 then main " is almost there, except that you never update W1 from within "pump:". You need a loop in "pump:" that keeps updating W1.

Here's some suggestions:

Don't take just a single reading of the water level. Take multiple readings and calculate an average from those readings. Act on the average value. Use a variable named "AverageLevel".

Make a subroutine that is "PUMP_ON:" and a subroutine that is "PUMP_OFF:". While you are in your "MAIN:" loop, go to these subroutines to perform that function; off or on.

Maintain a variable "PUMP_STATE" so your program knows whether your pump is on (1) or off (0).

Within your "MAIN:" loop, check that your average level is more than the "HighLevel" if "PUMP_STATE" is off (0) or less than the "LowLevel" if "PUMP_STATE" is on (1).
 
Top