Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- #include <Keypad.h>
- //----------------------------- Pins & Connections ------------------------------------
- //------------------------------------------------./Relay + Keypad ---------------------
- int valve = 12;//(Water Valve & Pump) Relay <IN2> to Arduino <Pin 12>
- byte sensorPin = 3;// Water Flow Sensor <Yellow Wire> to Arduino Mega 2560 <Pin 3>
- byte rowPins[4] = {22, 24, 26, 28}; //Rows 0 to 3
- byte colPins[4] = {30, 32, 34, 36}; //Columns 0 to 3
- //--------------------------------------------------------------------------------------
- //-------------------------- 2004A (0x3F) 20x4 LCD Screen -----------------------------
- //---------------------------------------------./Global Variables-----------------------
- // initialize the library with the numbers of the interface pins
- // addr,en,rw,rs,d4,d5,d6,d7,bl,blpol
- LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
- //--------------------------------------------------------------------------------------
- //-------------------------- YF-S201 Sensor ------------------------------------------
- //---------------------------------------------./Global Variables-----------------------
- float calibrationFactor = 6.4;
- byte sensorInterrupt = 0;
- int TurnOffVolume = 0;
- volatile byte pulseCount;
- float flowRate;
- unsigned int flowMilliLitres;
- unsigned long totalMilliLitres;
- unsigned long oldTime;
- //--------------------------------------------------------------------------------------
- //----------------------------- 4x4 Keypad ------------------------------------------
- //---------------------------------------------./Global Variables-----------------------
- long value = 0;
- char keys[4][4] =
- {
- {'1', '2', '3', 'A'},
- {'4', '5', '6', 'B'},
- {'7', '8', '9', 'C'},
- {'*', '0', '#', 'D'}
- };
- Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, 4, 4 );
- //--------------------------------------------------------------------------------------
- //-------------------------- YF-S201 Sensor + Reset Values ----------------------------
- //---------------------------------------------./Functions------------------------------
- void pulseCounter()
- {
- pulseCount++;
- }
- void Volume() {
- if ((millis() - oldTime) > 1000) // Only process counters once per second
- {
- detachInterrupt(sensorInterrupt);
- flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
- oldTime = millis();
- flowMilliLitres = (flowRate / 60) * 1000;
- totalMilliLitres += flowMilliLitres;
- pulseCount = 0;
- attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
- }
- }
- void ResetValues() {
- value = 0; //Now reset ready for next input
- totalMilliLitres = 0;
- TurnOffVolume = 10000;
- digitalWrite(valve, HIGH);
- }
- //--------------------------------------------------------------------------------------
- //||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||Setup
- void setup() {
- //Serial.begin(38400);
- pinMode(valve, OUTPUT);
- digitalWrite(valve, HIGH);
- //-------------------------- 2004A (0x3F) 20x4 LCD Screen ---------------------------
- //-----------------------------------------------./Setup------------------------------
- lcd.begin(20, 4);
- lcd.clear();
- lcd.setCursor(0, 1);
- lcd.print("AUTOMATIC FLOW METER");
- lcd.setCursor(7, 2);
- lcd.print("CONTROL");
- delay(1000);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Enter Water Amount :");
- lcd.setCursor(0, 2);
- lcd.print("Output Liquid : ");
- lcd.setCursor(0, 3);
- lcd.print(totalMilliLitres);
- lcd.print(" (mL)");
- lcd.setCursor(0, 1);
- lcd.setCursor(0, 1);
- //------------------------------------------------------------------------------------
- //-------------------------- YF-S201 Sensor ----------------------------------------
- //-----------------------------------------------./Setup------------------------------
- pinMode(sensorPin, INPUT);
- digitalWrite(sensorPin, HIGH);
- pulseCount = 0;
- flowRate = 0.0;
- flowMilliLitres = 0;
- totalMilliLitres = 0;
- oldTime = 0;
- attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
- //------------------------------------------------------------------------------------
- delay(1000);
- ResetValues();//To Reset All Variables And Start A New Operation
- }
- //||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||Setup
- //||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||Loop
- void loop() {
- //------------------------- Calculate Volume Of Liquid -------------------------------
- Volume();
- //------------------------------------------------------------------------------------
- //----------------------------- Stopping Condition -----------------------------------
- if (totalMilliLitres + (0.03 * totalMilliLitres) >= TurnOffVolume) {
- digitalWrite(valve, HIGH);
- // Print the cumulative total of litres flowed since starting
- lcd.setCursor(0, 2);
- lcd.print("Output Liquid : ");
- lcd.setCursor(0, 3);
- lcd.print(totalMilliLitres);
- lcd.print(" (mL)");
- delay(3000);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Enter Water Amount :");
- lcd.setCursor(0, 2);
- lcd.print("Output Liquid : ");
- lcd.setCursor(0, 3);
- lcd.print(totalMilliLitres);
- lcd.print(" (mL)");
- lcd.setCursor(0, 1);
- ResetValues();//To Reset All Variables And Start A New Operation
- }
- //------------------------------------------------------------------------------------
- //----------- Saving Keypad Inputs In A Variable For Further Operations --------------
- char key = keypad.getKey();
- //------------------------------------------------------------------------------------
- //------------ Code To Stop Showing "A" , "B" , "C" , "D" When Pressed ---------------
- if (key == 'A' || key == 'B' || key == 'C' || key == 'D') {
- //lcd.print("");
- }
- else
- {
- // lcd.print(key);
- }
- //------------------------------------------------------------------------------------
- delay(1);
- if (key != NO_KEY)
- {
- if ( (key >= '0') && (key <= '9') )
- {
- lcd.print(key);
- value = value * 10;
- value = value + key - '0';
- }
- /////////////////////////////////////////////////////////////////////////////////
- // A \\
- /////////////////////////////////////////////////////////////////////////////////
- if ( key == 'A' )
- {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Amount is SET to:");
- lcd.setCursor(0, 1);
- lcd.print("<");
- lcd.print(value);
- lcd.print("> (mL)");
- lcd.setCursor(0, 2);
- lcd.print("Output Liquid : ");
- lcd.setCursor(0, 3);
- lcd.print(totalMilliLitres);
- lcd.print(" (mL)");
- lcd.setCursor(0, 1);
- if (value == 0) {
- ResetValues();//To Reset All Variables And Start A New Operation
- }
- else {
- TurnOffVolume = value;
- digitalWrite(valve, LOW);// Turn ON Pump And Valve (Relay Active LOW)
- }
- }
- }
- /////////////////////////////////////////////////////////////////////////////////
- // B \\
- /////////////////////////////////////////////////////////////////////////////////
- if (key == 'B')
- {
- ResetValues();//To Reset All Variables And Start A New Operation
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Enter Water Amount :");
- lcd.setCursor(0, 2);
- lcd.print("Output Liquid : ");
- lcd.setCursor(0, 3);
- lcd.print(totalMilliLitres);
- lcd.print(" (mL)");
- lcd.setCursor(0, 1);
- }
- /////////////////////////////////////////////////////////////////////////////////
- // C \\
- /////////////////////////////////////////////////////////////////////////////////
- if (key == 'C')
- {
- ResetValues();//To Reset All Variables And Start A New Operation
- digitalWrite(valve, LOW);// Turn ON Pump And Valve (Relay Active LOW)
- lcd.clear();
- lcd.setCursor(0, 1);
- lcd.print("Water Valve is Open");
- lcd.setCursor(0, 2);
- lcd.print(" Caution !");
- }
- /////////////////////////////////////////////////////////////////////////////////
- // D \\
- /////////////////////////////////////////////////////////////////////////////////
- if (key == 'D')
- {
- digitalWrite(valve, HIGH);// Turn Off Pump And Valve (Relay Active LOW)
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Enter Water Amount :");
- lcd.setCursor(0, 2);
- lcd.print("Output Liquid : ");
- lcd.setCursor(0, 3);
- lcd.print(totalMilliLitres);
- lcd.print(" (mL)");
- lcd.setCursor(0, 1);
- ResetValues();//To Reset All Variables And Start A New Operation
- }
- /////////////////////////////////////////////////////////////////////////////////
- // * \\
- /////////////////////////////////////////////////////////////////////////////////
- if (key == '*')
- {
- digitalWrite(valve, HIGH);// Turn Off Pump And Valve (Relay Active LOW)
- lcd.clear();
- lcd.setCursor(0, 1);
- lcd.print("AUTOMATIC FLOW METER");
- lcd.setCursor(7, 2);
- lcd.print("CONTROL");
- delay(5000);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Enter Water Amount :");
- lcd.setCursor(0, 2);
- lcd.print("Output Liquid : ");
- lcd.setCursor(0, 3);
- lcd.print(totalMilliLitres);
- lcd.print(" (mL)");
- lcd.setCursor(0, 1);
- ResetValues();//To Reset All Variables And Start A New Operation
- }
- /////////////////////////////////////////////////////////////////////////////////
- }//end of loop
- //||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||Loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement