06-11-2017, 05:49
(08-06-2017, 06:12)danner Wrote: I need that! :-)
the code is simple but I can not integrate it for lack of knowledge in programming
I reconstructed here my measure of the water level of the well currently in place and I added on the display the simulation of the height of snow that I prepare.
We have the distance between the SRF05 sensor and the well water in cm and the filling level in%.
For height of snow, simply remove the distance to the ground with the measure
Code:
//HC RS04 Ultrasonic ranging module
int triggerPort = 2;
int echoPort = 3;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3A,20,4);
void setup() {
lcd.begin();
lcd.clear();
Serial.begin(9600);
pinMode( triggerPort, OUTPUT );
pinMode( echoPort, INPUT );
}
void loop() {
digitalWrite(triggerPort, LOW); // set to LOW trigger's output
digitalWrite(triggerPort, HIGH); // send a 10us pulse to the trigger
delayMicroseconds( 10 );
digitalWrite(triggerPort, LOW);
long duration = pulseIn(echoPort, HIGH);
long r = 3.4 * duration / 2; // here we calculate the distance using duration
float distance = r / 100.00;
float pourcent = duration;
float snow = 20 - distance ; //20 is distance in cm with ground
lcd.setCursor(0, 0);
lcd.print("time: ");
lcd.print(duration);
lcd.print("ms ");
lcd.setCursor(0, 1);
lcd.print("distance: ");
if( duration > 38000 ) lcd.print("out of reach"); // if duration in greather than 38ms, the obstacle is out of reach
else { lcd.print(distance); lcd.print("cm");}
pourcent = map(duration, 1000, 10500, 100, 0);
lcd.setCursor(0, 2);
lcd.print("level:");
lcd.print(pourcent);
lcd.print(" % ");
lcd.setCursor(0, 3);
lcd.print("height:");
lcd.print(snow);
lcd.print("cm ");
delay( 1000 ); // wait for 1s
}![[Image: p_201712.jpg]](https://i62.servimg.com/u/f62/18/46/27/88/p_201712.jpg)

