Juraj:
it looks good.I use
byte hi = x.read();
byte lo = x.read();
int res = hi * 256 + lo;
but shift operator is faster
Thank you. I got it to work finally!
At first, it didn't work. But, I did run I2C search sketch to make sure ( this problem happened before and took me months to figure it out )
I found the address is 56 Decimal.. I don't know why the address has changes. Edited the code and worked.
Also, implemented your suggestion.
I have problem with LCD screen.
and code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//LCD
#define BACKLIGHT_PIN 3
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void setup() {
Serial.begin(9600);
lcd.begin(20,4);
Wire.begin();
Wire.setClock(400000L);
}
int Range=0;
int Percentage;
void loop() {
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
// Step1: instruct sensor to read
Wire.beginTransmission(56);
Wire.write(byte(0x00)); // sets register pointer to the command register (0x00)
Wire.write(byte(0x51)); //81 decimal
Wire.endTransmission(); // stop transmitting
// step2: for reading to happen
delay(250);
// step 3: instruct sensor to return a particular echo reading
Wire.beginTransmission(56); // transmit to device #112
Wire.write(byte(0x02)); // sets register pointer to echo #1 register (0x02)
Wire.endTransmission(); // stop transmitting
// step 4: request reading from sensor
Wire.requestFrom(56,2);
// step 5: receive reading from sensor
if (2 <= Wire.available()) { // if two bytes were received
byte hi = Wire.read();
byte lo = Wire.read();
int Range = hi * 256 + lo;
Range = constrain(Range, 25, 150);
Percentage = map(Range, 150, 25, 0, 100);
Serial.print("Range= ");Serial.println(Range); // print the reading
Serial.print("% ");Serial.println(Percentage);
lcd.setCursor(0, 0); // top left
lcd.print("Range= "); lcd.println(Range);
lcd.setCursor(0, 1); // bottom left
lcd.println("Water level");
lcd.setCursor(15, 1); // bottom right
lcd.print("% "); lcd.println(Percentage);
}
delay(300);
}
And thank you and PaulS
