Hello everyone,
I am working on a water level management device. Based on Atmega328 (but current prototype on atmega 1280), It monitors the level via ultrasonic sensor SRC-04. When the tank is almost full it shuts off the pump and when the tank is almost one third it switches it on. There is also a LM35 to monitor temp. Till this point every thing works fine. The problem is as under:
I would like to provide two buttons to set the upper and lower limit of water level when pump should switch on or off. I envisioned it to be two buttons to increase the value till it reaches 100% then starts at 0% again, for each upper and lower limit. I had planed to use ext interrupts for this but i think i was wrong and they are not meant to be used like this. Now i am not sure how to manage this issue.
Any inputs are welcome, thanks..
I expect that when the button is pressed, instead of deiplaying the current level and temp, the screen displays "Set High Level" and "Set lower level" with the other button. But the screen just flickrs and does nothing noticiable.
I am sure i am making a real dumb noobie mistake, please point it out and point me in the right direction.
The code is attahed below:
/*
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
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
*/
#include <LiquidCrystal.h>
#include <NewPing.h>
#define TRIGGER_PIN 8 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 9 // 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.
#define RELAY_PIN 10 // Output pin for relay
#define TEMP_PIN 15 // Temp LM35 sensor input pin
#define STATUS_LED 13 // Status LED Blinking active
#define STATUS_MOTER 11 // Motor ON OFF LED
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
LiquidCrystal lcd(5, 6, 0, 1, 2, 3);
float temp;
float level;
float markLOW;
float markHIGH;
void setup() {
//Serial.begin(115200); // used for debugging
lcd.begin(16, 2);
attachInterrupt(2, button1, RISING);
attachInterrupt(3, button2, RISING);
constrain(markLOW, 0, 105);
constrain(markHIGH, 0, 105);
pinMode(RELAY_PIN, OUTPUT);
pinMode(TEMP_PIN, INPUT);
pinMode(STATUS_LED, OUTPUT);
pinMode(STATUS_MOTER, OUTPUT);
}
void loop() {
// Wait a while between pings (about 15 pings/min). 29ms should be the shortest delay between pings.
digitalWrite(STATUS_LED, HIGH);
delay(300);
digitalWrite(STATUS_LED, LOW);
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
temp = analogRead(TEMP_PIN);
temp = temp * 0.48828125;
level = uS / US_ROUNDTRIP_CM;
level = map(level, 0, 130, 100, 0);
lcd.setCursor(0, 0);
lcd.print("Level:");
lcd.print(level);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Temperature:");
lcd.print(temp);
lcd.print("C");
if ( level >= markHIGH)
{
digitalWrite(RELAY_PIN, LOW);
digitalWrite(STATUS_MOTER, LOW);
}
else if ( level <= markLOW)
{
digitalWrite(RELAY_PIN, HIGH);
digitalWrite(STATUS_MOTER, HIGH);
}
}
void button1() {
delay(1000);
markLOW = markLOW + 5;
if (markLOW = 105)
{
markLOW = 0;
}
lcd.setCursor(0, 0);
lcd.print("Set Low Level");
lcd.setCursor(0, 1);
lcd.print(markLOW);
lcd.print("%");
delay(5000);
}
void button2() {
delay(1000);
markHIGH = markHIGH + 5;
if (markHIGH = 105)
{
markHIGH = 0;
}
lcd.setCursor(0, 0);
lcd.print("Set High Level");
lcd.setCursor(0, 1);
lcd.print(markHIGH);
lcd.print("%");
delay(5000);
}
another method i tried was with "while" and later "if" function, but the program loops only at "Set Low Level".
Code attached
/*
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
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
*/
#include <LiquidCrystal.h>
#include <NewPing.h>
#define TRIGGER_PIN 8 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 9 // 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.
#define RELAY_PIN 10 // Output pin for relay
#define TEMP_PIN 15 // Temp LM35 sensor input pin
#define STATUS_LED 13 // Status LED Blinking active
#define STATUS_MOTER 11 // Moter ON OFF LED
#define BUTTON1 21 // Button 1
#define BUTTON2 20 // Button 2
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
LiquidCrystal lcd(5, 6, 0, 1, 2, 3);
float temp;
float level;
float markLOW;
float markHIGH;
void setup() {
//Serial.begin(115200);
lcd.begin(16, 2);
constrain(markLOW, 0, 105);
constrain(markHIGH, 0, 105);
pinMode(RELAY_PIN, OUTPUT);
pinMode(TEMP_PIN, INPUT);
pinMode(STATUS_LED, OUTPUT);
pinMode(STATUS_MOTER, OUTPUT);
}
void loop() {
// while the button1 is pressed, do the following
while (digitalRead(BUTTON1) == HIGH) {
button1();
delay(2000);
}
// while the button1 is pressed, do the following
while (digitalRead(BUTTON2) == HIGH) {
button2();
delay(2000);
}
// Wait a while between pings (about 15 pings/min). 29ms should be the shortest delay between pings.
digitalWrite(STATUS_LED, HIGH);
delay(300);
digitalWrite(STATUS_LED, LOW);
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
temp = analogRead(TEMP_PIN);
temp = temp * 0.48828125;
level = uS / US_ROUNDTRIP_CM;
level = map(level, 0, 130, 100, 0);
lcd.setCursor(0, 0);
lcd.print("Level:");
lcd.print(level);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Temperature:");
lcd.print(temp);
lcd.print("C");
if ( level >= markHIGH)
{
digitalWrite(RELAY_PIN, LOW);
digitalWrite(STATUS_MOTER, LOW);
}
else if ( level <= markLOW)
{
digitalWrite(RELAY_PIN, HIGH);
digitalWrite(STATUS_MOTER, HIGH);
}
//Serial.print("markLOW");
//Serial.println(markLOW);
//Serial.print("markHIGH");
//Serial.println(markHIGH);
}
void button1() {
markLOW = markLOW + 5;
if (markLOW = 105){
markLOW = 0;
}
lcd.setCursor(0, 0);
lcd.print("Set Low Level");
lcd.setCursor(0, 1);
lcd.print(markLOW);
lcd.print("%");
delay(500);
}
void button2() {
markHIGH = markHIGH + 5;
if (markHIGH = 105){
markHIGH = 0;
}
lcd.setCursor(0, 0);
lcd.print("Set High Level");
lcd.setCursor(0, 1);
lcd.print(markHIGH);
lcd.print("%");
delay(500);
}
I am not really experienced with coding, but i m here to learn, thanks for all your help.
Rahul