Notifications
Clear all

Water level indicator  

Page 3 / 5
  RSS

Tooter
(@tooter)
Eminent Member
Joined: 5 months ago
Posts: 34
2020-05-20 5:55 pm  

I have the custom chars for the bar graph of your interested.?


ReplyQuote
robotBuilder
(@robotbuilder)
Reputable Member
Joined: 1 year ago
Posts: 492
2020-05-20 6:29 pm  

Is there any reason to use Blynk.h if you are only using it for the timer?
I notice also you include but do not use EEPROM.h ?

Using a Blynk App on your mobile phone you can control hardware.
https://tutorial.cytron.io/2018/09/27/arduino-blynk-lesson-1-led/
https://maker.pro/arduino/projects/how-to-remotely-control-an-arduino-with-the-blynk-app

 

Does this code without the Blynk.h work?

// #Water level sensor. This scetch is to add a digital water lwvel sensor for a motor home 
// to monitor the fresh and grey water levels. 
// #The buzzer will sound for 3-5 sec when the level is at 50 and 25% and when grey water is
// at 75%

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

int addr = 0;
int addr2 = 1;
int flag;
byte readval;

// ===================================================================
unsigned long delayStart = 0; // the time the delay started
bool delayRunning = false; // true if still waiting for delay to finish
//====================================================================

#define SONAR_NUM 2                                //numer of sensors used.


//##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 unsigned int Period = 2000;                  //period between pings, in milliseconds

//##Sensor Pins

const int TrigPin1 = 6;                            //Triger pin for sensor 1
const int EchoPin1 = 7;                            //Echo pin for sensor 1
const int TrigPin2 = 10;                           //Triger pin sensor 2 
const int EchoPin2 = 8;                            //Echo pin sensor 2

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;

NewPing sonar [SONAR_NUM] = {                      //Sensor object aerray
  NewPing (TrigPin1, EchoPin1, MAX_DISTANCE),      // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing (TrigPin2, EchoPin2, MAX_DISTANCE)
};

void sendSensorReadings()
{
//#Tank 1 Readings
Distance1 = sonar[0].ping_cm();                  //Get distance to top of tank 
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
delay(50);
 } else {
 Serial.println("Out of range");
    /*Flash a warning led or buzzer, etc... here*/ 
   }

//#Tank2 Readings
Distance2 = sonar[1].ping_cm();                   //Get distance to top of tank 2
if(Distance2 >= 0 and Distance2 <= Depth2) {
Serial.println("In range");
WaterDepth2 = Depth2 - Distance2;                 //calulate the depth of water in tank 1
Litres2 = (Area2 * WaterDepth2) / 1000;           //calulate the volume of water
delay(50);
 } else {
 Serial.println("Out of range");
    /*Flash a warning led or buzzer, etc... here*/ 
   }

digitalWrite(13, HIGH);                          //Flash the led so we know ist running
delay(50);
digitalWrite(13, LOW);

//#Serial Output
Serial.println();
Serial.println();
Serial.println("Tank 1 Water Distance: " + String(Distance1)); //print Depth tank 1
Serial.println("Tank 1 Water Depth: " + String(WaterDepth1));  //print WaterDepth tank 1
Serial.println("Tank 1 Litres: " + String(Litres1));           //print Litres tank 1

Serial.println();
Serial.println("Tank 2 Water Distance: " + String(Distance2));  //print Depth Tank 2
Serial.println("Tank 2 Water Depth: " + String(WaterDepth2));   //print Waterdepth tank 2
Serial.println("Tank 2 Litres: " + String(Litres2));            //print Litres tank 2

}


void setup() {
  lcd.begin(16, 2);
  pinMode(13, OUTPUT);                                          //led pin 13

// ================================================================================
  delayStart = millis();   // start delay
  delayRunning = true; // not finished yet
//=================================================================================
  delay(10);
  
  Serial.begin(19200);                                           //Open serial console
  Serial.println();
  delay(20);
}
  
void loop() {
  
 // check if delay has timed out after 10sec == 20000mS
 if (delayRunning && ((millis() - delayStart) >= 20000)) { 
   sendSensorReadings();
 }

  lcd.setCursor(0,0);
  lcd.print("TNK 1 Litres " + String(Litres1));        //print on lcd amount in liters in Tank1
  lcd.setCursor(0,1);
  lcd.print("TNK 2 Litres " + String(Litres2));        //print on lcd amount in liters in tank2  

}

 

This post was modified 4 months ago by robotBuilder

ReplyQuote
Tooter
(@tooter)
Eminent Member
Joined: 5 months ago
Posts: 34
2020-05-21 1:38 am  
Posted by: @robotbuilder

Is there any reason to use Blynk.h if you are only using it for the timer?
I notice also you include but do not use EEPROM.h ?

Using a Blynk App on your mobile phone you can control hardware.
https://tutorial.cytron.io/2018/09/27/arduino-blynk-lesson-1-led/
https://maker.pro/arduino/projects/how-to-remotely-control-an-arduino-with-the-blynk-app

 

Does this code without the Blynk.h work?

// #Water level sensor. This scetch is to add a digital water lwvel sensor for a motor home 
// to monitor the fresh and grey water levels. 
// #The buzzer will sound for 3-5 sec when the level is at 50 and 25% and when grey water is
// at 75%

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

int addr = 0;
int addr2 = 1;
int flag;
byte readval;

// ===================================================================
unsigned long delayStart = 0; // the time the delay started
bool delayRunning = false; // true if still waiting for delay to finish
//====================================================================

#define SONAR_NUM 2                                //numer of sensors used.


//##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 unsigned int Period = 2000;                  //period between pings, in milliseconds

//##Sensor Pins

const int TrigPin1 = 6;                            //Triger pin for sensor 1
const int EchoPin1 = 7;                            //Echo pin for sensor 1
const int TrigPin2 = 10;                           //Triger pin sensor 2 
const int EchoPin2 = 8;                            //Echo pin sensor 2

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;

NewPing sonar [SONAR_NUM] = {                      //Sensor object aerray
  NewPing (TrigPin1, EchoPin1, MAX_DISTANCE),      // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing (TrigPin2, EchoPin2, MAX_DISTANCE)
};

void sendSensorReadings()
{
//#Tank 1 Readings
Distance1 = sonar[0].ping_cm();                  //Get distance to top of tank 
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
delay(50);
 } else {
 Serial.println("Out of range");
    /*Flash a warning led or buzzer, etc... here*/ 
   }

//#Tank2 Readings
Distance2 = sonar[1].ping_cm();                   //Get distance to top of tank 2
if(Distance2 >= 0 and Distance2 <= Depth2) {
Serial.println("In range");
WaterDepth2 = Depth2 - Distance2;                 //calulate the depth of water in tank 1
Litres2 = (Area2 * WaterDepth2) / 1000;           //calulate the volume of water
delay(50);
 } else {
 Serial.println("Out of range");
    /*Flash a warning led or buzzer, etc... here*/ 
   }

digitalWrite(13, HIGH);                          //Flash the led so we know ist running
delay(50);
digitalWrite(13, LOW);

//#Serial Output
Serial.println();
Serial.println();
Serial.println("Tank 1 Water Distance: " + String(Distance1)); //print Depth tank 1
Serial.println("Tank 1 Water Depth: " + String(WaterDepth1));  //print WaterDepth tank 1
Serial.println("Tank 1 Litres: " + String(Litres1));           //print Litres tank 1

Serial.println();
Serial.println("Tank 2 Water Distance: " + String(Distance2));  //print Depth Tank 2
Serial.println("Tank 2 Water Depth: " + String(WaterDepth2));   //print Waterdepth tank 2
Serial.println("Tank 2 Litres: " + String(Litres2));            //print Litres tank 2

}


void setup() {
  lcd.begin(16, 2);
  pinMode(13, OUTPUT);                                          //led pin 13

// ================================================================================
  delayStart = millis();   // start delay
  delayRunning = true; // not finished yet
//=================================================================================
  delay(10);
  
  Serial.begin(19200);                                           //Open serial console
  Serial.println();
  delay(20);
}
  
void loop() {
  
 // check if delay has timed out after 10sec == 20000mS
 if (delayRunning && ((millis() - delayStart) >= 20000)) { 
   sendSensorReadings();
 }

  lcd.setCursor(0,0);
  lcd.print("TNK 1 Litres " + String(Litres1));        //print on lcd amount in liters in Tank1
  lcd.setCursor(0,1);
  lcd.print("TNK 2 Litres " + String(Litres2));        //print on lcd amount in liters in tank2  

}

 

Hi yes that works. Am I right in thinking the statements you added was a delay in the output from the sensor readings?.

Yes I was going to look in to how the eeprom.h works and try to implement it but struggled to get my head around it

No reason to use the blynk library to be honest but it was the only way I could get it to work.


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 1 year ago
Posts: 477
2020-05-21 12:16 pm  

@tooter

Posted by: @tooter

Is it possible to change the out of range amount  as I would like the range to be low for the frash water tank and high for the grey water and a 3-5 sec buzzer to sound when fresh is down to 25% and grey is at 75%.

Yes, you can change it to suit your needs... you'll just need to store your desired values in some variables.  The logic will be very similar.  Have a go at it first, and if stuck, I or someone else here will help you to make it work.  One thing I thought about in the mean time... is that it's probably a good idea to introduce an offset, because theoretically, at zero, the sensor is under water, or on the verge of being underwater.

You could add another variable and set it's offset to say 5cm above the water level to start with, adding it to your overall calculations, and then fine tuning it's value depending on the exact location of your sensor in relation to the full level position of the water in the tank.

Cheers!


ReplyQuote
Tooter
(@tooter)
Eminent Member
Joined: 5 months ago
Posts: 34
2020-05-21 12:41 pm  
Posted by: @frogandtoad

You could add another variable and set it's offset to say 5cm above the water level to start with, adding it to your overall calculations, and then fine tuning it's value depending on the exact location of your sensor in relation to the full level position of the water in the tank.

Thanks ill give it a shot.

Today I've been trying to make the lcd come on for 5 sec after a button press with some success the only problem is calculations are only made when the screen is off when button is pressed it locks what ever is on the screen until it turns off again. not realy a problem but not expected.

I've highlighted the code ia added with +++++++++ to make it easier to spot. 

Thanks again and ill let you know how i get on with offset. 

I do plan on upgrading or buying new sensors to a water proof version but a offset is still a good idea.

 

// #Water level sensor. This scetch is to add a digital water lwvel sensor for a motor home 
// to monitor the fresh and grey water levels.
// 5 second backlight on so screen isnt on all the time
// #The buzzer will sound for 3-5 sec when the level is at 50 and 25% and when grey water is
// at 75%

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


// ===================================================================
unsigned long delayStart = 0; // the time the delay started
bool delayRunning = false; // true if still waiting for delay to finish
//====================================================================
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//back light on 5ec botton pins
int ButtonPin = 2;                                 //pin attaced to mode button
int lcdbk = 9;                                     //pin atached tolcd anode
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#define SONAR_NUM 2                                //numer of sensors used.


//##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 unsigned int Period = 2000;                  //period between pings, in milliseconds

//##Sensor Pins

const int TrigPin1 = 6;                            //Triger pin for sensor 1
const int EchoPin1 = 7;                            //Echo pin for sensor 1
const int TrigPin2 = 10;                           //Triger pin sensor 2 
const int EchoPin2 = 8;                            //Echo pin sensor 2

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;

NewPing sonar [SONAR_NUM] = {                      //Sensor object aerray
  NewPing (TrigPin1, EchoPin1, MAX_DISTANCE),      // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing (TrigPin2, EchoPin2, MAX_DISTANCE)
};

void sendSensorReadings()
{
//#Tank 1 Readings
Distance1 = sonar[0].ping_cm();                  //Get distance to top of tank 
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
delay(50);
 } else {
 Serial.println("Out of range");
    /*Flash a warning led or buzzer, etc... here*/ 
   }

//#Tank2 Readings
Distance2 = sonar[1].ping_cm();                   //Get distance to top of tank 2
if(Distance2 >= 0 and Distance2 <= Depth2) {
Serial.println("In range");
WaterDepth2 = Depth2 - Distance2;                 //calulate the depth of water in tank 1
Litres2 = (Area2 * WaterDepth2) / 1000;           //calulate the volume of water
delay(50);
 } else {
 Serial.println("Out of range");
    /*Flash a warning led or buzzer, etc... here*/ 
   }

digitalWrite(13, HIGH);                          //Flash the led so we know ist running
delay(50);
digitalWrite(13, LOW);

//#Serial Output
Serial.println();
Serial.println();
Serial.println("Tank 1 Water Distance: " + String(Distance1)); //print Depth tank 1
Serial.println("Tank 1 Water Depth: " + String(WaterDepth1));  //print WaterDepth tank 1
Serial.println("Tank 1 Litres: " + String(Litres1));           //print Litres tank 1

Serial.println();
Serial.println("Tank 2 Water Distance: " + String(Distance2));  //print Depth Tank 2
Serial.println("Tank 2 Water Depth: " + String(WaterDepth2));   //print Waterdepth tank 2
Serial.println("Tank 2 Litres: " + String(Litres2));            //print Litres tank 2

}


void setup() {
  lcd.begin(16, 2);
  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  pinMode(13, OUTPUT);                                            //led pin 13
  pinMode(ButtonPin, INPUT_PULLUP);                               //define button pin as input
  pinMode(lcdbk, OUTPUT);                                         //define lcd pin as output
  attachInterrupt(0, isr, FALLING);
}
volatile long last = 0;
bool turnoff = false;
volatile long offAt =0;
void isr(){
if( (millis() - last ) > 20 )     //if at least 20 ms has passed since last press, this is not a dup
{
  last = millis();                //note the time, for ignoring duplicate presses
  turnoff = true;
  offAt = millis() + 5000;        //save a variable of now + 5 seconds
  digitalWrite(lcdbk, HIGH);      //turn on
  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ================================================================================
  delayStart = millis();   // start delay
  delayRunning = true; // not finished yet
//=================================================================================
  delay(10);
  
  Serial.begin(19200);                                           //Open serial console
  Serial.println();
  delay(20);
    }
}    
void loop() {
  //++++++++++++++++++++
 if(turnoff)
  {
    if(millis() >= offAt)
    {
      //++++++++++++++++
     
  // check if delay has timed out after 10sec == 20000mS
 if (delayRunning && ((millis() - delayStart) >= 20000)) { 
   sendSensorReadings();
 }

  lcd.setCursor(0,0);
  lcd.print("TNK 1 Litres " + String(Litres1));        //print on lcd amount in liters in Tank1
  lcd.setCursor(0,1);
  lcd.print("TNK 2 Litres " + String(Litres2));        //print on lcd amount in liters in tank2 
  //++++++++++++++++++++++++++ 
digitalWrite(lcdbk, LOW);                              //Turn off lcd
    }
  }
}
//+++++++++++++++++++++++++++++


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

Yes, you can change it to suit your needs... you'll just need to store your desired values in some variables.  The logic will be very similar.  Have a go at it first, and if stuck, I or someone else here will help you to make it work.  One thing I thought about in the mean time... is that it's probably a good idea to introduce an offset, because theoretically, at zero, the sensor is under water, or on the verge of being underwater.

Hi I had a go at making the serial print "Fill Tank" when the litres is <=. but for some reason it don't work but dont know why. There must be more to it than this.

 

if(Litres1 <= 1

Serial.println("Fill Tank");


ReplyQuote
Ruplicator
(@ruplicator)
Estimable Member
Joined: 6 months ago
Posts: 110
2020-05-21 3:30 pm  
Posted by: @tooter

Today I've been trying to make the lcd come on for 5 sec after a button press with some success the only problem is calculations are only made when the screen is off when button is pressed it locks what ever is on the screen until it turns off again. not realy a problem but not expected.

I think your problem with screen lock-up is that the code that detects the button activation is in the "setup()" area of the code. The set() is only run once when the MCU is rebooted. Any button detection should be moved to the loop() section that continually run. You have done a great job of using millis() to ensure a potential loop will keep running without delays.

I understand your still in the debugging phase of your project but I have a couple of questions on your approach. I see a lot of code for calculating volume of the tanks and quantity in the tanks. Would it not be more useful and easier to just use percent full? That way someone doesn't need to know how large the tanks are to know if they are about to overflow or go empty?

Secondly, you may want to start looking into methods for averaging your readings. From what I read in you header you want the system to warn you with a buzzer when the tanks need servicing. I am assuming the measurements will be going on while the motor home is moving (this may not be your intention and will only run when stationary). If the system is running while moving, water movement in the tanks my cause a lot of extraneous unwanted warnings. 


ReplyQuote
Ruplicator
(@ruplicator)
Estimable Member
Joined: 6 months ago
Posts: 110
2020-05-21 4:25 pm  
Posted by: @tooter

if(Litres1 <= 1

Serial.println("Fill Tank");

I didn't see this in your posted code but the problem may be as simple as you are missing a closing ")".

 

if(Litres1 <= 1 Serial.println("Fill Tank"));


ReplyQuote
robotBuilder
(@robotbuilder)
Reputable Member
Joined: 1 year ago
Posts: 492
2020-05-21 6:45 pm  
Posted by: @tooter

There must be more to it than this. 

if(Litres1 <= 1

Serial.println("Fill Tank");

Perhaps,

if(Litres1 <= 1)

Serial.println("Fill Tank");

 


ReplyQuote
Ruplicator
(@ruplicator)
Estimable Member
Joined: 6 months ago
Posts: 110
2020-05-21 8:40 pm  
Posted by: @robotbuilder
Posted by: @tooter

There must be more to it than this. 

if(Litres1 <= 1

Serial.println("Fill Tank");

Perhaps,

if(Litres1 <= 1)

Serial.println("Fill Tank");

 

Oops you're right I'm terrible with syntax.


ReplyQuote
Tooter
(@tooter)
Eminent Member
Joined: 5 months ago
Posts: 34
2020-05-21 9:03 pm  
Posted by: @ruplicator

I think your problem with screen lock-up is that the code that detects the button activation is in the "setup()" area of the code. The set() is only run once when the MCU is rebooted. Any button detection should be moved to the loop() section that continually run. You have done a great job of using millis() to ensure a potential loop will keep running without delays.

ahh knew it was something daft like that thanks ill try it tomorrow.

Posted by: @ruplicator

I understand your still in the debugging phase of your project but I have a couple of questions on your approach. I see a lot of code for calculating volume of the tanks and quantity in the tanks. Would it not be more useful and easier to just use percent full? That way someone doesn't need to know how large the tanks are to know if they are about to overflow or go empty?

Yes I suppose that would be easier as the amount of water isn't really that important more how full/empty the tank is. It would be beneficial to me to simplify the code.

Posted by: @ruplicator

Secondly, you may want to start looking into methods for averaging your readings. From what I read in you header you want the system to warn you with a buzzer when the tanks need servicing. I am assuming the measurements will be going on while the motor home is moving (this may not be your intention and will only run when stationary). If the system is running while moving, water movement in the tanks my cause a lot of extraneous unwanted warnings. 

No the water meter will only be active when the van is stationary as the 12v electric has a safety cut out when the van is moving.


ReplyQuote
Tooter
(@tooter)
Eminent Member
Joined: 5 months ago
Posts: 34
2020-05-21 9:06 pm  
Posted by: @robotbuilder
Posted by: @tooter

There must be more to it than this. 

if(Litres1 <= 1

Serial.println("Fill Tank");

Perhaps,

if(Litres1 <= 1)

Serial.println("Fill Tank");

 

sorry yes that is what i have just missed it when i coped it.


ReplyQuote
robotBuilder
(@robotbuilder)
Reputable Member
Joined: 1 year ago
Posts: 492
2020-05-22 2:39 am  

@tooter

<snip code>

Posted by: @tooter

Hi yes that works. Am I right in thinking the statements you added was a delay in the output from the sensor readings?.

No, it is a software "interrupt" every 2000ms.

The "delay" in the labels is misleading in this case.

I tried to redo the code to explain how it works but kept getting problems while trying to test it on actual hardware. Maybe I can come back to it later.

The main loop() is repeated at maximum speed for your particular Arduino but the segment within that loop is only executed if 2000ms has passed regardless of how fast the loop is executed.

In computer games for example you might want to update the display 60 times per second which may be much slower than the main loop is being executed. To do this you save a current clock value and then wait until 1/60th of second has passed by comparing that saved clock value with the current clock value at which point you can display the next game frame or in your case call sendSensorReadings.

This is an example of how it works in the BASIC language I usually use on my PC because it is easy to read and write. I would move as much control code as possible out of the Arduino and into a RPi.  All the Arduino would do is read the sensors and send them to the PC over the serial port (or via the GPIO pins on the RPi).

 

dim as double st 'start time
st = timer 'set st to current time
do
  if timer > st + 0.1 then
    st = timer 'reset start time 
    readInput()
    update()  'update game data
    display() 'display game
  end if
  sleep 2 'free cpu for other things
loop until multikey(&H01) 'loop until ESC key pressed  
  

 

 

This post was modified 4 months ago 9 times by robotBuilder

ReplyQuote
robotBuilder
(@robotbuilder)
Reputable Member
Joined: 1 year ago
Posts: 492
2020-05-22 6:07 am  

@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);
  
}

 

 


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 1 year ago
Posts: 477
2020-05-22 12:34 pm  

@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!


ReplyQuote
Page 3 / 5