Notifications
Clear all

Water level indicator  

Page 4 / 5
  RSS

Tooter
(@tooter)
Eminent Member
Joined: 3 months ago
Posts: 34
2020-05-23 7:31 pm  
Posted by: @robotbuilder

@tooter

So I had time to have another look at the code and run it on my version of your hardware.

In your original code the Blynk timer method called the sendSensorReading every 2000ms

I am not sure why it is only read every 2000ms something to do with it printing to the IDE Monitor?

In a stand alone circuit there would be no IDE Monitor and perhaps in this case no requirement for only reading the sensors every 2000ms.

Anyway I have kept it so they are only updated every 2000ms and written the code to print to the lcd not the Serial port.

 

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 13);

#include "NewPing.h"
 
// Define Constants

#define TRIGGER_PIN_1  6
#define ECHO_PIN_1     7
#define TRIGGER_PIN_2  10
#define ECHO_PIN_2     8

#define MAX_DISTANCE 400

NewPing sonar1(TRIGGER_PIN_1, ECHO_PIN_1, MAX_DISTANCE);
NewPing sonar2(TRIGGER_PIN_2, ECHO_PIN_2, MAX_DISTANCE);

// Define Variables
//##Tank dimentions change to suit
// const int MAX_DISTANCE = 400;                      //Max Distance to measure.
const int Diameter1 = 19;                          //Diameter of tank 1.
const int Diameter2 = 19;                          //Diameter of tank2 2.
const int Depth1 = 25;                             //Depth of tank 1.
const int Depth2 = 25;                             //Depth of tank2.
const int Area1 = PI * sq(Diameter1 / 2);          //Area of tank 1
const int Area2 = PI * sq(Diameter1 / 2);          //Area of takn 2

//Global variables
int Litres1, Litres2, Distance1, Distance2, WaterDepth1, WaterDepth2;

// =================================================================================
unsigned long previousMillis = 0;        // hold last time the clock was read
unsigned long interval = 2000;           // interval at which to blink (milliseconds)
//==================================================================================


void setup() {
  lcd.begin(16, 2);  // 16 digits and two rows
}

void loop() {

    // check to see if it's time to read the sensors
  unsigned long currentMillis = millis();
  
  // =======================  this segement is executed every 2000ms  =======================
  if(currentMillis - previousMillis >= interval){
    
    previousMillis = currentMillis;   // reset previous to the current time
    
    Distance1 = sonar1.ping_cm();

    lcd.clear();  // clear lcd screen
    
    if(Distance1 >= 0 and Distance1 <= Depth1) {
      // Serial.println("In range");

      WaterDepth1 = Depth1 - Distance1;                 //calulate the depth of water in tank 1
      Litres1 = (Area1 * WaterDepth1) / 1000;           //calulate the volume of water
      lcd.setCursor(0,0);
      lcd.print("TNK 1 Litres " + String(Litres1));
    } else {
      lcd.setCursor(0,0);
      lcd.print("Out of range");
    }

    Distance2 = sonar2.ping_cm();
    
    if(Distance2 >= 0 and Distance2 <= Depth2) {
      // Serial.println("In range");
      WaterDepth2 = Depth2 - Distance2;                 //calulate the depth of water in tank 2
      Litres2 = (Area2 * WaterDepth2) / 1000;           //calulate the volume of water
      lcd.setCursor(0,1);
      lcd.print("TNK 2 Litres " + String(Litres2));
    } else {
      lcd.setCursor(0,1);
      lcd.print("Out of range");
    }
    
  }
  // ========================================================================================


  delay(50);
  
}

 

 

Hi thanks that woks great. just need some time to pick at it and and try to understand it. I just haven't had a lot of time to mess with it lately.(I'm a key worker here in the UK 😥 ). When i get chance ill be back on it I'm rely eager to get it running and learn and understand the code to. thanks again.


ReplyQuote
Tooter
(@tooter)
Eminent Member
Joined: 3 months ago
Posts: 34
2020-05-23 7:34 pm  
Posted by: @frogandtoad

@tooter

Ok, for your delay, try something like the following... when you break code down into it's smallest manageable problem areas, it becomes much easier to work out what's going on.  Once you have it working, then it's just a matter of merging it with your main code - Hopefully the following can help:

const int led = LED_BUILTIN;

void setup() {
  Serial.begin(9600);
  pinMode(led, OUTPUT); 
 }
 
unsigned long interval = 5000; // 5 Seconds
unsigned long previousMillis = millis();
unsigned long currentMillis = 0;

// digitalRead opperation (this should be in the loop where you read the button)
bool SwitchPressed = true;;

void loop() {
  
 if(SwitchPressed) {
    digitalWrite(led, HIGH);
    currentMillis = millis();
     
    if(currentMillis - previousMillis > interval) {
       digitalWrite(led, LOW);
       SwitchPressed = false;        
       previousMillis = millis();
      }
    }    
  }

Cheers!

Thank struggled to get that to work. I'm a bit pushed fro time at the moment though. I will get back to it and give it another try.


ReplyQuote
robotBuilder
(@robotbuilder)
Reputable Member
Joined: 1 year ago
Posts: 467
2020-05-23 11:52 pm  
Posted by: @tooter

Hi thanks that works great. just need some time to pick at it and and try to understand it.

With practice and familiarity with the computer language of your choice this becomes easier.

However it is better to have it written in a high level human like language first so you can work out the logic and flow of a program that is easy to read and understand and then have it translated down to the computer language of your choice.

A high level description also makes it easier to translate your code from one language to another (C++ to Python perhaps).  A higher level description will also make it easier to "pick at and understand" the code.

If I remember correctly you wanted the display to come on for a short time after you pressed a button?

 


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 1 year ago
Posts: 455
2020-05-25 8:34 am  

@tooter

Posted by: @tooter

Thank struggled to get that to work. I'm a bit pushed fro time at the moment though. I will get back to it and give it another try.

Hm... not sure why you struggled to get it to work.  The code I provided was a simple copy and paste, with nothing else to do to it but run it to understand how it works.  What problems did you have with it?


ReplyQuote
Tooter
(@tooter)
Eminent Member
Joined: 3 months ago
Posts: 34
2020-05-25 1:38 pm  
Posted by: @frogandtoad

@tooter

Posted by: @tooter

Thank struggled to get that to work. I'm a bit pushed fro time at the moment though. I will get back to it and give it another try.

Hm... not sure why you struggled to get it to work.  The code I provided was a simple copy and paste, with nothing else to do to it but run it to understand how it works.  What problems did you have with it?

Hi I couldn't get it to compile. 


ReplyQuote
Tooter
(@tooter)
Eminent Member
Joined: 3 months ago
Posts: 34
2020-05-27 11:41 am  

@frogandto

I apologize it did work. Thanks 


ReplyQuote
Tooter
(@tooter)
Eminent Member
Joined: 3 months ago
Posts: 34
2020-05-27 9:11 pm  

Ok finally found some time to spend on it after a lot of reading. I've worked out how to convert a measured distance in to a percentage. 😉 Managed to write the code myself with help from the previous posts. My only problem is I need the percentage in tank 1 to go down from 100% (when full) to zero not from zero to 100%. I'm struggling to achieve this. Here is my code.

 

//Distance measure in cm converts to persentage

#include <NewPing.h>
const int trigPin1 = 6;    //Trigger pin for sensor 1.
const int echoPin1 = 7;    //Echo pin for sensor 1
const int trigPin2 = 8;    //Trigger pin for sensor 2
const int echoPin2 = 9;    //Echo pin for sensor 2
int pause1 = 5;            //5 millsecond delay 
int pause2 = 10;           //10 millisecond delay
int Distance, Distance2, Duration, Duration2, percentage, percentage2;
int TankDepth1 = 26;       // put your tank 1 depth here.
int TankDepth2 = 20;       // put your tamkk 2 depth here.
const int MAX_DISTANCE = 30;

NewPing Sonar1 (trigPin1, echoPin1, MAX_DISTANCE);
NewPing Sonar2 (trigPin2, echoPin2, MAX_DISTANCE);



void setup() {
 pinMode (trigPin1, OUTPUT);       //Trigger pin 1 output
 pinMode (echoPin1, INPUT);        //Echo pin 1 input
 pinMode (trigPin2, OUTPUT);       //Trigger pin 2 output
 pinMode (echoPin2, INPUT);        //Echo pin 2 input
 Serial.begin(9600);
 

}

void loop() {
  digitalWrite (trigPin1, LOW);     //Trigger pin 1 low
  delay(pause1);
  digitalWrite (trigPin1, HIGH);    //Trigger pin 1 high
  delay(pause2);
  digitalWrite (trigPin1, LOW);     //Trigger pin 1 low
  Duration = pulseIn(echoPin1, HIGH);  
  Distance = Duration * 0.034/2;      //convert duration to distance in cm
  percentage = (100*Distance/TankDepth1); //convert distance to percent
 
  
  digitalWrite (trigPin2, LOW);     //
  delay(pause1);
  digitalWrite (trigPin2, HIGH);
  delay(pause2);
  digitalWrite (trigPin2, LOW);
   Duration2 = pulseIn(echoPin2, HIGH);
   Distance2 = Duration2 * 0.034/2;
   percentage2 = (100*Distance/TankDepth2);
   
  Serial.print ("TANK1 ");
  Serial.print (percentage);
  Serial.println ( "%");
  delay(1000);
  Serial.println();
  Serial.print ("TANK2 ");
  Serial.print (percentage2);
  Serial.println ( "%");
  Serial.println();
  delay(1000);

  

}


ReplyQuote
robotBuilder
(@robotbuilder)
Reputable Member
Joined: 1 year ago
Posts: 467
2020-05-27 11:05 pm  
Posted by: @tooter

Ok finally found some time to spend on it after a lot of reading. I've worked out how to convert a measured distance in to a percentage. 😉 Managed to write the code myself with help from the previous posts. My only problem is I need the percentage in tank 1 to go down from 100% (when full) to zero not from zero to 100%. I'm struggling to achieve this. Here is my code.


Great satisfaction working it out for yourself 🙂

If your outputs are in the range 0 to 100 you can simply invert them.
value = 100-value

Have you actually placed your sonar sensors on the top of the tanks yet?

 


ReplyQuote
Tooter
(@tooter)
Eminent Member
Joined: 3 months ago
Posts: 34
2020-05-28 8:48 am  
Posted by: @robotbuilder
Posted by: @tooter

Ok finally found some time to spend on it after a lot of reading. I've worked out how to convert a measured distance in to a percentage. 😉 Managed to write the code myself with help from the previous posts. My only problem is I need the percentage in tank 1 to go down from 100% (when full) to zero not from zero to 100%. I'm struggling to achieve this. Here is my code.

 

Great satisfaction working it out for yourself 🙂

If your outputs are in the range 0 to 100 you can simply invert them.
value = 100-value

Have you actually placed your sonar sensors on the top of the tanks yet?

 

Hi thanks. Yes it does feel good. No they are not in place yet Still testing but i have the measurements of the tanks so i can set the depth on a test rig i have set up. 


ReplyQuote
Tooter
(@tooter)
Eminent Member
Joined: 3 months ago
Posts: 34
2020-05-28 9:04 am  
Posted by: @robotbuilder

If your outputs are in the range 0 to 100 you can simply invert them.
value = 100-value

Thanks that worked great.


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 1 year ago
Posts: 455
2020-05-28 10:16 am  
Posted by: @tooter

@frogandto

I apologize it did work. Thanks 

Cool, that's great to hear!

Just saw your recent emails too, so congrats on working it out to get your program working.  It becomes very satisfying when you can get stuff to work!

When you get a little more proficient in coding, the next step might be to refactor your code to incorporate functions, which can make your code much more readable and in many cases shorter too.

Anytime you use multiple variable names for the same quantity, you have to ask yourself the question... do I really need to keep repeating these variables, or is there a better way?... and yes there is!

Might be a good exercise to look into 🙂

Cheers!


ReplyQuote
Tooter
(@tooter)
Eminent Member
Joined: 3 months ago
Posts: 34
2020-05-28 2:52 pm  

@frogandtoad

Thanks will look in to it.

What i'm doing at the mo is following a great series on youtube by a great teacher called Paul McWhorter. I follow his lessons and if there is something i feel is relevant to my project i try to apply it. It seems to be working well at the moment just a little slow process but i'm enjoying the learning.


ReplyQuote
Tooter
(@tooter)
Eminent Member
Joined: 3 months ago
Posts: 34
2020-05-28 5:32 pm  

Hi discovered a problem with my code. 😪 😪  I've been racking my brain on it all afternoon. I will do my best to explain. When the sensors are at same level the math works out fine. but if the water levels are different in both tanks it cant calculate them correctly and gives me inaccurate percentages. I hope that makes sense.


ReplyQuote
Tooter
(@tooter)
Eminent Member
Joined: 3 months ago
Posts: 34
2020-05-28 6:07 pm  

It's ok think i got it working I changed from int to float on percentage 1 and 2 and it messed it up.


ReplyQuote
Ruplicator
(@ruplicator)
Trusted Member
Joined: 3 months ago
Posts: 95
2020-05-29 2:18 pm  
Posted by: @tooter

percentage2 = (100*Distance/TankDepth2);

From a much earlier post it looks like you were using "Distance" instead of Distance2 in your second tank calculation.


ReplyQuote
Page 4 / 5