hi yall,
So this is my first time doing an arduino project and i'm still a newbie...
So my project is to control the usage of water.
the flow of my project:
WATER SENSOR>>WATER VALVE WITH RELAY -ON->>WATER FLOW SENSOR>>LCD( SHOW A METER OF HOW MUCH WATER HAS FLOWED OUT)>>if over the limit>> PIEZO -ON->> LED -ON->>WATER VALVE WITH RELAY -OFF->>if wanna let water flow more >>PRESS PUSH BUTTON>>WATER VALVE WITH RELAY -ON->>can use an app to turn off /ON the water VALVE (MIT APP)
the problem i have while doing this is that the 5v relay keeps on turning on and off !!!!!!!!!!!!
/* A0 = water flow sensor
* D2-D8 = lcd
* D9 =water sensor
* D10 = piezo
* D11= 5V relay for water solenoid valve
* D13= push button
* A1 & A2 = bluetooth module
*/
#include <LiquidCrystal.h> //
LiquidCrystal lcd (7, 8, 5, 4, 3, 2);
int X;
int Y;
float TIME = 0;
float FREQUENCY = 0; //LCD FOR WATER FLOW SENSOR @ WATER METER
float WATER = 0;
float TOTAL = 0;
float LS = 0;
const int input = A0;
int Contrast=50; //
#define Grove_Water_Sensor A5 //WATER SENSOR
const int RELAY_ENABLE = 12; //RELAY (WATER VALVE)
const int inPin=13; //PUSHBUTTONS
int currentState = LOW;
int previousState = LOW;
int outState = LOW; //
#include <SoftwareSerial.h> //BLUETOOTH MODULE
SoftwareSerial Bluetooth(A1,A2);
String message; //
void setup()
{
Serial.begin(9600);
pinMode(Grove_Water_Sensor, INPUT); //WATER SENSOR
pinMode(RELAY_ENABLE, OUTPUT); //RELAY
pinMode(inPin, INPUT); //PUSH BUTTON
pinMode(A3,OUTPUT);
pinMode(A4,OUTPUT);
pinMode (9, OUTPUT);
Bluetooth.begin(9600); //BLUETOOTH MODULE
{ //
analogWrite(6,Contrast);
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print ("Water Flow Meter"); //LCD FOR WATER FLOW SENSOR (WATER FLOW SENSOR WILL
lcd.setCursor(0, 1); //SHOW UP WHEN ELECTRIC SUPPLY IS CONNECTED TO IT
lcd.print ("****************");
delay(2000);
} //
}
void loop()
{
if( digitalRead(Grove_Water_Sensor) == LOW)
{
{ Serial.println("Relay ON");
digitalWrite(RELAY_ENABLE, LOW);
delay(3000);}
{
X = pulseIn(input, HIGH);
Y = pulseIn (input, LOW);
TIME = X + Y;
FREQUENCY = 1000000/TIME;
WATER = FREQUENCY/7.F;
LS = WATER/60;
if(FREQUENCY >= 0)
if(isinf(FREQUENCY))
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("****************");
lcd.setCursor(0,1);
lcd.print("TOTAL:");
lcd.print( TOTAL);
lcd.print(" L");
}
else
{
TOTAL = TOTAL + LS;
Serial.println(FREQUENCY);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("****************");
lcd.setCursor(0,1);
lcd.print("TOTAL:");
lcd.print( TOTAL);
lcd.print(" L");
}
else if (lcd.print ("1.00 L"))
{tone (10,494,250);
delay(2000);
delay(10);
digitalWrite(9, HIGH);
digitalWrite (A3, LOW);
digitalWrite (A4, LOW);
Serial.println("Relay ON");
digitalWrite(RELAY_ENABLE, LOW);
delay(3000);}
else if (lcd.print ("2.00 L"))
{ tone (10,494,250);
delay(2000);
tone (10,494,250);
delay(2000);
digitalWrite(9, LOW);
digitalWrite (A3, HIGH);
digitalWrite (A4, LOW);
Serial.println("Relay ON");
digitalWrite(RELAY_ENABLE, LOW);
delay(3000);}
else if (lcd.print ("3.00 L"))
{tone (10,494,250);
delay(2000);
tone (10,494,250);
delay(2000);
tone (10,494,250);
delay(2000);
Serial.println("Relay OFF");
digitalWrite(RELAY_ENABLE, HIGH);
digitalWrite(9, LOW);
digitalWrite (A3, LOW);
digitalWrite (A4, HIGH);
delay(1000);}
else
{Serial.println("Relay ON");
digitalWrite(RELAY_ENABLE, LOW);
digitalWrite(9, LOW);
digitalWrite (A3, LOW);
digitalWrite (A4, LOW);
delay(1000);}
}
{
currentState = digitalRead(inPin);
if ((currentState == HIGH) && (previousState == LOW));
{
if (outState == LOW)
{outState = HIGH;
{
Serial.println("Relay ON");
digitalWrite(RELAY_ENABLE, LOW);
if (lcd.print ("4.00 L"));
{Serial.println("Relay OFF");
digitalWrite(RELAY_ENABLE, HIGH);
digitalWrite(9, HIGH);
digitalWrite (A3, HIGH);
digitalWrite (A4, HIGH);
}}}
else
{outState = LOW;}}
previousState = currentState;
digitalWrite (RELAY_ENABLE, outState);
delay(10);
} }
else //if no water in contact with water sensor
{
Serial.println("Relay OFF");
digitalWrite(RELAY_ENABLE, HIGH);
digitalWrite(9, HIGH);
digitalWrite (A3, HIGH);
digitalWrite (A4, HIGH);
}
delay(2000);
{
while (Bluetooth.available() > 0)
{
delay(10);
char c = Bluetooth.read();
message += c;
}
if (message.length() >0)
{ Serial.println (message);
if (message=="ON")
{ Serial.println("Relay ON");
digitalWrite(RELAY_ENABLE, LOW);}
if (message=="OFF")
{ Serial.println("Relay OFF");
digitalWrite(RELAY_ENABLE, HIGH);}
}
message = "";
}
}