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
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
/* LiquidCrystal Library - Hello World Demonstrates the use a 16x2 LCD display. The LiquidCrystal library works with all LCD displays that are compatible with the Hitachi HD44780 driver. There are many of them out there, and youre can usually tell them by the 16-pin interface. This sketch prints "Hello World!" to the LCD and shows the time. 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) Library originally added 18 Apr 2008 by David A. Mellis library modified 5 Jul 2009 by Limor Fried ( http://www.ladyada.net ) example added 9 Jul 2009 by Tom Igoe modified 22 Nov 2010 by Tom Igoe modified 7 Nov 2016 by Arturo Guadalupi This example code is in the public domain. http://www.arduino.cc/en/Tutorial/LiquidCrystalHelloWorld */ // 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 TRIGGER_PIN 6 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define ECHO_PIN 7 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MAX_DISTANCE 500 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. #define RELAYPIN 8 // Arduino pin tied to Relaypin of the Relay Circuit #define EXTRELAYPIN 13 // Arduino pin tied to vin pin of the External Relay Circuit #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 10 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(RELAYPIN,OUTPUT);// Relay pin as output pin pinMode(EXTRELAYPIN,OUTPUT);// External Relay pin as output pin digitalWrite(RELAYPIN,LOW); //Turn off the relay digitalWrite(EXTRELAYPIN,HIGH); //Turn off the external relay(External Relay I used was turning on while giving LOW signal, Check your one while doing the program) pinMode(BUZZER,OUTPUT);// Buzzer pin as output pin digitalWrite(BUZZER,LOW); //Turn off the Buzzer lcd.print("Automatic Water"); lcd.setCursor(0,1); lcd.print("*Pumping System*"); 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(8,0); lcd.print("Lev:"); lcd.print(percentage); lcd.print("% "); lcd.setCursor(0, 1); lcd.print("LOW"); lcd.setCursor(11, 1); lcd.print("HIGH "); if(SpmpsensorValue>=100) { if(percentage<=20) { lcd.setCursor(0,0); lcd.print("PMP ON "); digitalWrite(RELAYPIN,HIGH); digitalWrite(EXTRELAYPIN,LOW); 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) { digitalWrite(RELAYPIN,HIGH); digitalWrite(EXTRELAYPIN,LOW); lcd.setCursor(0,0); lcd.print("PMP ON "); 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) { digitalWrite(RELAYPIN,LOW); digitalWrite(EXTRELAYPIN,HIGH); lcd.setCursor(0,0); lcd.print("PMP OFF "); 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(0,0); lcd.print("PMP OFF "); lcd.setCursor(8,0); lcd.print("Lev:"); lcd.print("100"); lcd.print("% "); digitalWrite(RELAYPIN,LOW); digitalWrite(EXTRELAYPIN,HIGH); 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*"); lcd.setCursor(0,1); lcd.print("*Pump kept OFF*"); digitalWrite(BUZZER,HIGH); digitalWrite(RELAYPIN, LOW); digitalWrite(EXTRELAYPIN, 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)); }
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));
There are ways to make your code easier to read.
https://forum.dronebotworkshop.com/question-suggestion/sticky-post-for-editing/#post-4939
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. 🙂
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.
Hi thanks to all for the suggestions. I've tidied the code up a bit hope that's easier to read and used the sugested method to post it. i've also added the foll error at the bottom.
/* 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; const int TRIGGER_PIN2 = 10; 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]={ 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)); }
Error Recived
Arduino: 1.8.12 (Windows 7), Board: "Arduino Uno"
water-level-indicator-2:36:9: error: expected primary-expression before 'sonar'
NewPing sonar(TRIGGER_PIN1, ECHO_PIN1, MAX_DISTANCE), // NewPing setup of pins and maximum distance.
^~~~~
water-level-indicator-2:36:9: error: expected '}' before 'sonar'
water-level-indicator-2:36:9: error: expected ',' or ';' before 'sonar'
C:\Users\garage\AppData\Local\Temp\arduino_modified_sketch_901569\water-level-indicator-2.ino: In function 'void setup()':
water-level-indicator-2:160:21: error: request for member 'ping_cm' in 'sonar', which is of non-class type 'NewPing [2]'
TankHeight =sonar.ping_cm();
^~~~~~~
C:\Users\garage\AppData\Local\Temp\arduino_modified_sketch_901569\water-level-indicator-2.ino: In function 'void loop()':
water-level-indicator-2:183:22: error: request for member 'ping_cm' in 'sonar', which is of non-class type 'NewPing [2]'
SonarReading=sonar.ping_cm();
^~~~~~~
water-level-indicator-2:205:3: error: 'ZeroPercentage' was not declared in this scope
ZeroPercentage();
^~~~~~~~~~~~~~
C:\Users\garage\AppData\Local\Temp\arduino_modified_sketch_901569\water-level-indicator-2.ino:205:3: note: suggested alternative: 'percentage'
ZeroPercentage();
^~~~~~~~~~~~~~
percentage
water-level-indicator-2:214:9: error: 'TwentyFivePercentage' was not declared in this scope
TwentyFivePercentage();
^~~~~~~~~~~~~~~~~~~~
water-level-indicator-2:218:9: error: 'FiftyPercentage' was not declared in this scope
FiftyPercentage();
^~~~~~~~~~~~~~~
C:\Users\garage\AppData\Local\Temp\arduino_modified_sketch_901569\water-level-indicator-2.ino:218:9: note: suggested alternative: 'percentage'
FiftyPercentage();
^~~~~~~~~~~~~~~
percentage
water-level-indicator-2:223:10: error: 'SeventyFivePercentage' was not declared in this scope
SeventyFivePercentage();
^~~~~~~~~~~~~~~~~~~~~
water-level-indicator-2:227:11: error: 'HundredPercentage' was not declared in this scope
HundredPercentage();
^~~~~~~~~~~~~~~~~
C:\Users\garage\AppData\Local\Temp\arduino_modified_sketch_901569\water-level-indicator-2.ino:227:11: note: suggested alternative: 'percentage'
HundredPercentage();
^~~~~~~~~~~~~~~~~
percentage
water-level-indicator-2:236:9: error: 'TwentyFivePercentage' was not declared in this scope
TwentyFivePercentage();
^~~~~~~~~~~~~~~~~~~~
water-level-indicator-2:241:9: error: 'FiftyPercentage' was not declared in this scope
FiftyPercentage();
^~~~~~~~~~~~~~~
C:\Users\garage\AppData\Local\Temp\arduino_modified_sketch_901569\water-level-indicator-2.ino:241:9: note: suggested alternative: 'percentage'
FiftyPercentage();
^~~~~~~~~~~~~~~
percentage
water-level-indicator-2:248:9: error: 'SeventyFivePercentage' was not declared in this scope
SeventyFivePercentage();
^~~~~~~~~~~~~~~~~~~~~
water-level-indicator-2:252:9: error: 'HundredPercentage' was not declared in this scope
HundredPercentage();
^~~~~~~~~~~~~~~~~
C:\Users\garage\AppData\Local\Temp\arduino_modified_sketch_901569\water-level-indicator-2.ino:252:9: note: suggested alternative: 'percentage'
HundredPercentage();
^~~~~~~~~~~~~~~~~
percentage
water-level-indicator-2:268:3: error: 'HundredPercentage' was not declared in this scope
HundredPercentage();
^~~~~~~~~~~~~~~~~
C:\Users\garage\AppData\Local\Temp\arduino_modified_sketch_901569\water-level-indicator-2.ino:268:3: note: suggested alternative: 'percentage'
HundredPercentage();
^~~~~~~~~~~~~~~~~
percentage
water-level-indicator-2:290:9: error: 'ZeroPercentage' was not declared in this scope
ZeroPercentage();
^~~~~~~~~~~~~~
C:\Users\garage\AppData\Local\Temp\arduino_modified_sketch_901569\water-level-indicator-2.ino:290:9: note: suggested alternative: 'percentage'
ZeroPercentage();
^~~~~~~~~~~~~~
percentage
water-level-indicator-2:294:9: error: 'TwentyFivePercentage' was not declared in this scope
TwentyFivePercentage();
^~~~~~~~~~~~~~~~~~~~
water-level-indicator-2:298:9: error: 'FiftyPercentage' was not declared in this scope
FiftyPercentage();
^~~~~~~~~~~~~~~
C:\Users\garage\AppData\Local\Temp\arduino_modified_sketch_901569\water-level-indicator-2.ino:298:9: note: suggested alternative: 'percentage'
FiftyPercentage();
^~~~~~~~~~~~~~~
percentage
water-level-indicator-2:304:10: error: 'SeventyFivePercentage' was not declared in this scope
SeventyFivePercentage();
^~~~~~~~~~~~~~~~~~~~~
water-level-indicator-2:308:11: error: 'HundredPercentage' was not declared in this scope
HundredPercentage();
^~~~~~~~~~~~~~~~~
C:\Users\garage\AppData\Local\Temp\arduino_modified_sketch_901569\water-level-indicator-2.ino:308:11: note: suggested alternative: 'percentage'
HundredPercentage();
^~~~~~~~~~~~~~~~~
percentage
water-level-indicator-2:312:10: error: 'HundredPercentage' was not declared in this scope
HundredPercentage();
^~~~~~~~~~~~~~~~~
C:\Users\garage\AppData\Local\Temp\arduino_modified_sketch_901569\water-level-indicator-2.ino:312:10: note: suggested alternative: 'percentage'
HundredPercentage();
^~~~~~~~~~~~~~~~~
percentage
exit status 1
expected primary-expression before 'sonar'
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Thanks again.
Ok I was hoping for a little help from this forum. I was under the impression that people was glad to help.
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)
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?
Hi @tooter
This is a good project, but I agree with @ruplicator on the "divide and conquer" approach. The code listing seems to have a lot of stuff going on which may not be relevant - relay, temp sense, eeprom read/write etc. Most of the code is taken up with display formatting which distracts from the core function.
Break the problem down into project files for each sensor. Once all the individual components are tested, then you can combine them into a single sketch.
An example to read from 2 ultrasonic sensors is below (tested on Arduino Mega2560):
// --------------------------------------------------------------------------- // NewPing sketch to read from two ultrasonic sensors. // --------------------------------------------------------------------------- #include <NewPing.h> #define TRIGGER_PIN_1 36 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define ECHO_PIN_1 37 // Arduino pin tied to echo pin on the ultrasonic sensor. #define TRIGGER_PIN_2 38 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define ECHO_PIN_2 39 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. NewPing sonar1(TRIGGER_PIN_1, ECHO_PIN_1, MAX_DISTANCE); // NewPing setup of pins and maximum distance. NewPing sonar2(TRIGGER_PIN_2, ECHO_PIN_2, MAX_DISTANCE); // NewPing setup of pins and maximum distance. void setup() { Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results. } void loop() { delay(500); // 29ms should be the shortest delay between pings. Serial.print("Ping sonar1:"); Serial.print(sonar1.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range) Serial.print("cm : sonar2:"); Serial.print(sonar2.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range) Serial.println("cm"); }
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.
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.
//#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 1 sec when the level is at 50 and 25% and when grey water is at 75% #include <Blynk.h> #include <NewPing.h> #include <LiquidCrystal.h> #define SONAR_NUM 2 //numer of sensors used. #define pi 3.1415926535897932384626433832795 //##Tank dimentions change to suit const int MAX_DISTANCE = 300; //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 * ((Diameter1 / 2) * (Diameter1 /2)); //Area of tank 1 const int Area2 = PI * ((Diameter1 / 2) * (Diameter2 /2)); //Area of takn 2 //Global variables int Litres1, Litres2, Distance1, Distance2, WaterDepth1, WaterDepth2; BlynkTimer timer; //Config Timer 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 1 if (Distance1 >= Depth1 || Distance1 == 0 ) Distance1 = Depth1; //Check it dont go negative WaterDepth1 = Depth1 - Distance1; //calulate the depth of water in tank 1 Litres1 = (Area1 * WaterDepth1) / 1000; //calulate the volume of water delay(50); //#Tank2 Readings Distance2 = sonar [1].ping_cm(); //Get distance to top of tank 2 if (Distance2 >= Depth2 || Distance2 == 0 ) Distance2 = Depth2; //Check it dont go negative WaterDepth2 = Depth2 - Distance2; //calculate the Depth of water in tank 2 Litres2 = (Area2 * WaterDepth2) / 1000; //calculate the volume of water 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() { pinMode(13, OUTPUT); //led pin 13 timer.setInterval(Period, sendSensorReadings); // Setup a function to be called every 'n' seconds delay(10); Serial.begin(19200); //Open serial console Serial.println(); delay(20); } void loop() { timer.run(); }
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?