[SOLVED] HC-SR04 and percentage value
Moderators: grovkillen, Stuntteam, TD-er
[SOLVED] HC-SR04 and percentage value
Hello,
So I add an HC-SR04 module to tell me the tank level. I want to express this value in percent example:
HC-SR04 %
7 100
40 0
Under arduino via val = map (val, 7, 40, 100, 0); I get there but through the rules I do not know how to express this value, if someone had a tutorial or a little help thank you.
I am French and translated via google translation, I hope you will understand.
So I add an HC-SR04 module to tell me the tank level. I want to express this value in percent example:
HC-SR04 %
7 100
40 0
Under arduino via val = map (val, 7, 40, 100, 0); I get there but through the rules I do not know how to express this value, if someone had a tutorial or a little help thank you.
I am French and translated via google translation, I hope you will understand.
Last edited by frenesik on 27 Nov 2017, 08:53, edited 1 time in total.
- grovkillen
- Core team member
- Posts: 3621
- Joined: 19 Jan 2017, 12:56
- Location: Hudiksvall, Sweden
- Contact:
Re: HC-SR04 and percentage value
You need to put a equation to your value:
https://www.mathsisfun.com/equation_of_line.html
y=m*x+b
Code: Select all
121-3*%value%
y=m*x+b
Last edited by grovkillen on 27 Nov 2017, 08:58, edited 1 time in total.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you

ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you



Re: HC-SR04 and percentage value
Re,
I found a lua script for domoticz that does very well. THX
I found a lua script for domoticz that does very well. THX
- grovkillen
- Core team member
- Posts: 3621
- Joined: 19 Jan 2017, 12:56
- Location: Hudiksvall, Sweden
- Contact:
Re: [SOLVED] HC-SR04 and percentage value
Good, but it's sometimes easier to just have the ESP Easy-unit report the correct value. I want to have as little intelligence on server-side as possible.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you

ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you



Re: [SOLVED] HC-SR04 and percentage value
Thank you to you friend, but let's say in addition it had to remove a margin of height of the sensor compared to the level of tank.
- grovkillen
- Core team member
- Posts: 3621
- Joined: 19 Jan 2017, 12:56
- Location: Hudiksvall, Sweden
- Contact:
Re: [SOLVED] HC-SR04 and percentage value
Still, easy in equations.
Example:
Code: Select all
121-3*(%value%-5)
No need for a server in my opinion.

ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you

ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you



Re: HC-SR04 and percentage value
Code: Select all
-- Nom du capteur HC-SR04
depth_sensor = 'Niveau'
-- Capteur à creer nom et id à modifier
pct_sensor = 'Niveau cuve'
pct_sensor_id = 1389
-- Hauteur total moins la marge (23 - 9 = 12)
tank_height = 12
commandArray = {}
if (devicechanged[depth_sensor]) then
--Soustraire marge du capteur
depth = ( otherdevices_svalues[depth_sensor] - 9 )
pct = ( tank_height - depth ) / ( tank_height / 100 )
commandArray[1] = {['UpdateDevice'] = pct_sensor_id .. "|0|" .. pct}
end
return commandArray

Last edited by frenesik on 28 Nov 2017, 16:38, edited 1 time in total.
Re: [SOLVED] HC-SR04 and percentage value
grovkillen wrote: ↑28 Nov 2017, 15:57Still, easy in equations.
Example:
Where "5" is the margin removed.Code: Select all
121-3*(%value%-5)
No need for a server in my opinion.Let's make the nodes report the correct value in the first place.
Thanks to you for your perseverance lol, I tried I do not find the same values compared to the script I modify the margin: x
- grovkillen
- Core team member
- Posts: 3621
- Joined: 19 Jan 2017, 12:56
- Location: Hudiksvall, Sweden
- Contact:
Re: [SOLVED] HC-SR04 and percentage value
This formula/equation in your device value would do the exact same thing:
Or typed like this:
Given that tank height is 12, and offset is 9.
Code: Select all
100-100/12*(%value%-9)
Code: Select all
(12+9-%value%)/12*100
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you

ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you



Re: [SOLVED] HC-SR04 and percentage value
Oh a big thank you to you friend, it works niquel good I have not understood everything of the formula but it can be great when displaying on an OLED if you want to display it in% Thank you to you super your effort;)grovkillen wrote: ↑28 Nov 2017, 17:25 This formula/equation in your device value would do the exact same thing:
Or typed like this:Code: Select all
100-100/12*(%value%-9)
Given that tank height is 12, and offset is 9.Code: Select all
(12+9-%value%)/12*100
Re: [SOLVED] HC-SR04 and percentage value
carefull with the water vapor because i had this sensor with mysensors working fine but after 2 months stop give good values, it was the oxid by the water inside the sonar piece.
-
- New user
- Posts: 2
- Joined: 04 Dec 2018, 11:23
Re: [SOLVED] HC-SR04 and percentage value
Hello, I have tried the examples you used but I can not manage, I have a water tank when the tank is full the distance is 43cm and I would like the displayed value to be 100% when the cistern is empty measuring 70cm and I would that the measure displayed was 0% I tried several rules but I have not yet succeeded, do you think since you have arrived at the solution which formula you use? thanks in advance Giovanni
- grovkillen
- Core team member
- Posts: 3621
- Joined: 19 Jan 2017, 12:56
- Location: Hudiksvall, Sweden
- Contact:
Re: [SOLVED] HC-SR04 and percentage value
If 70 = 0% and 43 = 100% you simply add this as a formula for the value:
(70-%value%)/(70-43)*100
(70-%value%)/(70-43)*100
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you

ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you



-
- New user
- Posts: 2
- Joined: 04 Dec 2018, 11:23
Re: [SOLVED] HC-SR04 and percentage value
perfect thanks, considering the examples above I would never have arrived !!! hello thanks again
Who is online
Users browsing this forum: No registered users and 20 guests