Water level indicat...
 
Notifications
Clear all

Water level indicator

81 Posts
12 Users
11 Likes
10.9 K Views
(@tooter)
Member
Joined: 4 years ago
Posts: 33
Topic starter  

Hi all. I've just started a new project a water level indicator for my fresh and grey water on my motor home. I've decided to use a arduino and the ultra sonic sensors( I plan to use water proof sensors). I want to use a 16x2 or 18x4 lcd screen with a mode button to switch between the 2 sensors showing the state of the tank. I found some code that is sort of what I need but it will need updating to add another sensor and the mode button to the code and remove some code that won't be needed. But my coding skills aren't up to the task in hand. It would be great if someone could give me some pointers to where to start with it all. Any input would be greatly received.

Link to cade used.

https://drive.google.com/file/d/1QPsYrArl0MN80G_l3lszzTftsYXi1rSa/view?usp=sharing


   
Inq reacted
Quote
codecage
(@codecage)
Member Admin
Joined: 5 years ago
Posts: 1016
 

@tooter

I have approved your post but would ask you to rethink your link.  I doesn't work for me and I doubt for others either.

I also suggest you just embed your code directly by using the "<>" selection in the menu bar above.

SteveG


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2035
 

   
ReplyQuote
(@tooter)
Member
Joined: 4 years ago
Posts: 33
Topic starter  

Hi its me again. OK so I've made a little progress with the code I've managed to remove all the code relating to the relays (I think ) and changed some of the text that shows on the lcd but ive hit a brickwall trying to change the number of sensors. I've added the pin numbers i want to use and told newping what they are byt i keep getting a error. expected primary-expretion before sonar. I cant work out why i'm very new to this and just trying to learn as i go. All input welcome thanks.

 

/*

The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)

*/

// include the library code:

#include <LiquidCrystal.h>

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; // define the Arduino pins used to connect with the LCD pins RS, EN, D4 to D7
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);//initialize the library by associating any needed LCD interface pinwith the arduino pin number it is connected to

#include <NewPing.h> // Include library used for measuring the distance using HC-SR 06 sonar sensor
#define SONAR_NUM 2 //Number of sonar sensors

const int TRIGGER_PIN1 = 6; // Arduino pin tied to trigger pin on the ultrasonic sensor.
const int ECHO_PIN1 = 7;      // Arduino pin tied to echo pin on the ultrasonic sensor.
const int ECHO_PIN2 = 13;    // Arduino pin tied to echo pin on the ultrasonic sensor2.
const int TRIGGER_PIN2 = 10;  // Arduino pin tied to trigger pin on the ultrasonic sensor2.

const int MAX_DISTANCE = 400;// Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm

NewPing sonar[SONAR_NUM] = {  // Sensor object array.
NewPing sonar(TRIGGER_PIN1, ECHO_PIN1, MAX_DISTANCE), // NewPing setup of pins and maximum distance.
NewPing sonar(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE);

#define BUZZER 9 // Arduino pin tied to +ve terminal of the Buzzer
float val;
#include <EEPROM.h>
int addr = 0;
int addr2 = 1;
int flag;
byte readval;

#define buttonPin 8
int buttonState = 0; // variable for reading the pushbutton status
float TankHeight, MaxWaterLevel, EmptySpace, SonarReading, ActualReading, Temp;
int percentage;
int SpmpSensorPin = A0; // set A0 as the Spump water sensor pin
int SpmpsensorValue = 0; // variable to store the value coming from the sensor

// Creating Charaters for Bar Graph and Reverse Mode Icon
byte Level0[8] = {
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b11111,
0b11111
};
byte Level1[8] = {
0b00000,
0b00000,
0b00000,
0b00000,
0b11111,
0b11111,
0b11111,
0b11111
};
byte Level2[8] = {
0b00000,
0b00000,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111
};
byte Level3[8] = {
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111
};
byte NoLevel[8] = {
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
};

void setup() {
Serial.begin(9600);
lcd.createChar(0, Level0);
lcd.createChar(1, Level1);
lcd.createChar(2, Level2);
lcd.createChar(3, Level3);
lcd.createChar(4, NoLevel);
Serial.println(flag);
lcd.begin(16, 2); // set up the LCD's number of columns and rows:
pinMode(BUZZER,OUTPUT);// Buzzer pin as output pin
digitalWrite(BUZZER,LOW); //Turn off the Buzzer
lcd.print("GREY & FRESH");
lcd.setCursor(0,1);
lcd.print("WATER LEVELS");
delay(2000);
lcd.clear();
lcd.setCursor(0,1);
lcd.print("*Welcome*");
delay(2000);
lcd.clear();
lcd.print("Long Press Button to Change Tank Height");
delay(1000);
for (int positionCounter = 0; positionCounter < 23; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
// wait a bit:
delay(300);
}
delay(1000);
lcd.clear();
lcd.print("Tank must be Empty!");
delay(1000);
for (int positionCounter = 0; positionCounter < 3; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
// wait a bit:
delay(300);
}
delay(1000);
lcd.clear();

lcd.print("Change Tnk Height");

for (int i=0; i<=5; i++)
{
lcd.setCursor(0,1);
lcd.print(" in:");
lcd.print(5-i);
lcd.print(" Seconds");
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
{
TankHeight =sonar.ping_cm();
EEPROM.write(addr, TankHeight);
}
delay(1000);
}

TankHeight= EEPROM.read(addr);
lcd.clear();
lcd.print("Tnk Height Set:");
lcd.setCursor(0,1);
lcd.print(" ");
lcd.print(TankHeight);
lcd.print("cm ");
delay(2000);
lcd.clear();
MaxWaterLevel=0.85*TankHeight;
EmptySpace=TankHeight-MaxWaterLevel;
}

void loop() {
// put your main code here, to run repeatedly:

delay(50); // Wait 100ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
SonarReading=sonar.ping_cm();
SpmpsensorValue=analogRead(SpmpSensorPin);
Serial.println(SpmpsensorValue);
Temp= SonarReading-EmptySpace;
ActualReading= MaxWaterLevel-Temp;
percentage=(ActualReading/MaxWaterLevel*100);
lcd.setCursor(0,0);
lcd.print("Lev:");
lcd.print(percentage);
lcd.print("% ");
lcd.setCursor(0, 1);
lcd.print("LOW");
lcd.setCursor(12, 1);
lcd.print("HIGH ");
if(SpmpsensorValue>=100)
{
if(percentage<=20)
{

flag=1;
EEPROM.write(addr2, flag);
flag= EEPROM.read(addr2);
ZeroPercentage();
}
else if(percentage>20 && percentage<=100)
{
flag= EEPROM.read(addr2);
if(percentage>20 && percentage<=100 && flag ==1)
{
if(percentage>20 && percentage<=25)
{
TwentyFivePercentage();
}
else if(percentage>25 && percentage<=50)
{
FiftyPercentage();
}

else if(percentage>50 && percentage<=75)
{
SeventyFivePercentage();
}
else if(percentage>75 && percentage<=100)
{
HundredPercentage();
}
}
else if(percentage>20 && percentage<=100 && flag ==0)
{

if(percentage>20 && percentage<=25)
{

TwentyFivePercentage();

}
else if(percentage>25 && percentage<=50)
{
FiftyPercentage();
}

else if(percentage>50 && percentage<=75)
{

SeventyFivePercentage();
}
else if(percentage>75 && percentage<=100)
{
HundredPercentage();

}
}
}

//else if(percentage>100)
{
delay(500);
lcd.setCursor(8,0);
lcd.print("");
lcd.print("");
lcd.print(" ");
flag=0;
EEPROM.write(addr2, flag);
flag= EEPROM.read(addr2);
HundredPercentage();
}
}
else if(SpmpsensorValue<=100)
{
flag= EEPROM.read(addr2);
if(flag==1)
{ lcd.clear();
lcd.setCursor(0,0);
lcd.print("*SumTank Empty*");
digitalWrite(BUZZER,HIGH);
delay(100);
digitalWrite(BUZZER,LOW);
delay(100);
}
else if(flag==0)
{
lcd.setCursor(0,0);
lcd.print("STnk MT ");

if(percentage<=20)
{
ZeroPercentage();
}
else if(percentage>20 && percentage<=25)
{
TwentyFivePercentage();
}
else if(percentage>25 && percentage<=50)
{
FiftyPercentage();
}

else if(percentage>50 && percentage<=75)
{
SeventyFivePercentage();
}
else if(percentage>75 && percentage<=100)
{
HundredPercentage();
}
else if(percentage>100)
{
HundredPercentage();
}
}
}
}
void ZeroPercentage()
{
lcd.setCursor(3, 1);
lcd.write(byte(4));
lcd.setCursor(4, 1);
lcd.write(byte(4));
lcd.setCursor(5, 1);
lcd.write(byte(4));
lcd.setCursor(6, 1);
lcd.write(byte(4));
lcd.setCursor(7, 1);
lcd.write(byte(4));
lcd.setCursor(8, 1);
lcd.write(byte(4));
lcd.setCursor(9, 1);
lcd.write(byte(4));
lcd.setCursor(10, 1);
lcd.write(byte(4));
}
void TwentyFivePercentage()
{
lcd.setCursor(3, 1);
lcd.write(byte(0));
lcd.setCursor(4, 1);
lcd.write(byte(0));
lcd.setCursor(5, 1);
lcd.write(byte(4));
lcd.setCursor(6, 1);
lcd.write(byte(4));
lcd.setCursor(7, 1);
lcd.write(byte(4));
lcd.setCursor(8, 1);
lcd.write(byte(4));
lcd.setCursor(9, 1);
lcd.write(byte(4));
lcd.setCursor(10, 1);
lcd.write(byte(4));
}
void FiftyPercentage()
{
lcd.setCursor(3, 1);
lcd.write(byte(0));
lcd.setCursor(4, 1);
lcd.write(byte(0));
lcd.setCursor(5, 1);
lcd.write(byte(1));
lcd.setCursor(6, 1);
lcd.write(byte(1));
lcd.setCursor(7, 1);
lcd.write(byte(4));
lcd.setCursor(8, 1);
lcd.write(byte(4));
lcd.setCursor(9, 1);
lcd.write(byte(4));
lcd.setCursor(10, 1);
lcd.write(byte(4));
}
void SeventyFivePercentage()
{
lcd.setCursor(3, 1);
lcd.write(byte(0));
lcd.setCursor(4, 1);
lcd.write(byte(0));
lcd.setCursor(5, 1);
lcd.write(byte(1));
lcd.setCursor(6, 1);
lcd.write(byte(1));
lcd.setCursor(7, 1);
lcd.write(byte(2));
lcd.setCursor(8, 1);
lcd.write(byte(2));
lcd.setCursor(9, 1);
lcd.write(byte(4));
lcd.setCursor(10, 1);
lcd.write(byte(4));

}
void HundredPercentage()
{
lcd.setCursor(3, 1);
lcd.write(byte(0));
lcd.setCursor(4, 1);
lcd.write(byte(0));
lcd.setCursor(5, 1);
lcd.write(byte(1));
lcd.setCursor(6, 1);
lcd.write(byte(1));
lcd.setCursor(7, 1);
lcd.write(byte(2));
lcd.setCursor(8, 1);
lcd.write(byte(2));
lcd.setCursor(9, 1);
lcd.write(byte(3));
lcd.setCursor(10, 1);
lcd.write(byte(3));


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2035
 

There are ways to make your code easier to read.

https://forum.dronebotworkshop.com/question-suggestion/sticky-post-for-editing/#post-4939

 


   
ReplyQuote
Ruplicator
(@ruplicator)
Member
Joined: 4 years ago
Posts: 127
 

Here are a couple of suggestions.

The code is such a jumble I don't think anyone is going to touch it. You also have indicated that your coding skills my not be at a pro level. Why not set this code aside for now as you may want to extract items from it later. Find a simple code example that will work with your ultrasonic transducer. Understand how the code works and make it work with one of your sensor and display reading on your serial monitor. Once your confident with the operation add the second sensor with what ever method you decide is best to switch between them. Finally, add the LCD display.

I know this may not be what you were looking but it is the best advice I have and you might have some fun learning along the way. 🙂 


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@tooter

You should always post the full error message with line number - If you do not have adequate error messages turned on, you can do that under the IDE preference settings.

The first couple of errors I see are as follows... I did not look at anything beyond them:

NewPing sonar[SONAR_NUM] = {  // Sensor object array.
NewPing sonar(TRIGGER_PIN1, ECHO_PIN1, MAX_DISTANCE), // NewPing setup of pins and maximum distance.

 


   
ReplyQuote
(@tooter)
Member
Joined: 4 years ago
Posts: 33
Topic starter  

   
ReplyQuote
(@tooter)
Member
Joined: 4 years ago
Posts: 33
Topic starter  

Ok I was hoping for a little help from this forum. I was under the impression that people was glad to help.  


   
ReplyQuote
(@starnovice)
Member
Joined: 5 years ago
Posts: 110
 

@tooter

I changed your declaration of the sonar array to:

NewPing sonar1(TRIGGER_PIN1, ECHO_PIN1, MAX_DISTANCE);
NewPing sonar2(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE);
NewPing Sonar[SONAR_NUM]={sonar1, sonar2};

After that every place where you call sonar (I found 2) you need to include the array index.

Pat Wicker (Portland, OR, USA)


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2035
 
Posted by: @tooter

Ok I was hoping for a little help from this forum. I was under the impression that people was glad to help.  

There well be a lot more members wanting help than able to give help.  Many beginners who don't know very much chasing the few members experienced in electronics and programming.  I have not yet used the 18x4 lcd screen or applied a sonar sensor to measure water depths. Given the hardware and a clear statement of the problem I could get the required information elsewhere on the internet and provide the solution but that is very time consuming and at the moment I have other real life problems to deal with.  In other words it is not that people aren't glad to help,  it is they can't or don't have the time to try and figure out what exactly you have done. Have you looked for similar projects on the internet you might learn from?

 


   
ReplyQuote
(@sysguru)
Member
Joined: 4 years ago
Posts: 16
 

   
ReplyQuote
(@tooter)
Member
Joined: 4 years ago
Posts: 33
Topic starter  

Thanks for the help and sorry for my previous comment I was getting a little frustrated with it all. i have decided to take the advice and break the project in to smaller chunks starting with setting up the sensors and printing to serial. with the help above. Thanks again and apologies.  


   
ReplyQuote
(@tooter)
Member
Joined: 4 years ago
Posts: 33
Topic starter  

   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2035
 
Posted by: @tooter

Hi all I feel I have made a lot of progress today I've taken some code written for a wireless water meter and adjusted it to compile for the uno. I felt i would be beneficial to re write the code as opposed to copy and paste it I found it to be a beneficial learning curve there was a few hiccups along the way but managed to get it working quite well through the serial output. 

Great work.  Being able to figure it out for yourself comes with great personal satisfaction.

I noticed you use a header file called Blynk.h that I am unfamiliar with.

Checking the internet I read that Blynk allows arduino to communicate over the internet?

 


   
ReplyQuote
Page 1 / 6