Advertisement
safwan092

Untitled

May 14th, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.85 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3. #include <Keypad.h>
  4.  
  5.  
  6.  
  7. //-----------------------------  Pins & Connections ------------------------------------
  8. //------------------------------------------------./Relay + Keypad ---------------------
  9. int   valve           =   12;//(Water Valve & Pump) Relay <IN2> to Arduino <Pin 12>
  10.  
  11. byte  sensorPin       =   3;//  Water Flow Sensor <Yellow Wire> to Arduino Mega 2560 <Pin 3>
  12.  
  13. byte rowPins[4] = {22, 24, 26, 28}; //Rows 0 to 3
  14.  
  15. byte colPins[4] = {30, 32, 34, 36}; //Columns 0 to 3
  16.  
  17. //--------------------------------------------------------------------------------------
  18.  
  19.  
  20. //--------------------------  2004A (0x3F) 20x4 LCD Screen -----------------------------
  21. //---------------------------------------------./Global Variables-----------------------
  22.  
  23. // initialize the library with the numbers of the interface pins
  24. //                    addr,en,rw,rs,d4,d5,d6,d7,bl,blpol
  25. LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  26. //--------------------------------------------------------------------------------------
  27.  
  28.  
  29. //--------------------------  YF-S201 Sensor  ------------------------------------------
  30. //---------------------------------------------./Global Variables-----------------------
  31. float calibrationFactor = 6.4;
  32. byte sensorInterrupt = 0;
  33. int TurnOffVolume = 0;
  34. volatile byte pulseCount;
  35. float flowRate;
  36. unsigned int flowMilliLitres;
  37. unsigned long totalMilliLitres;
  38. unsigned long oldTime;
  39. //--------------------------------------------------------------------------------------
  40.  
  41.  
  42. //-----------------------------  4x4 Keypad   ------------------------------------------
  43. //---------------------------------------------./Global Variables-----------------------
  44. long value = 0;
  45. char keys[4][4] =
  46. {
  47.   {'1', '2', '3', 'A'},
  48.   {'4', '5', '6', 'B'},
  49.   {'7', '8', '9', 'C'},
  50.   {'*', '0', '#', 'D'}
  51. };
  52. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, 4, 4 );
  53. //--------------------------------------------------------------------------------------
  54.  
  55.  
  56. //--------------------------  YF-S201 Sensor + Reset Values ----------------------------
  57. //---------------------------------------------./Functions------------------------------
  58. void pulseCounter()
  59. {
  60.   pulseCount++;
  61. }
  62.  
  63.  
  64. void Volume() {
  65.   if ((millis() - oldTime) > 1000)   // Only process counters once per second
  66.   {
  67.     detachInterrupt(sensorInterrupt);
  68.     flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
  69.     oldTime = millis();
  70.     flowMilliLitres = (flowRate / 60) * 1000;
  71.     totalMilliLitres += flowMilliLitres;
  72.     pulseCount = 0;
  73.     attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  74.   }
  75. }
  76.  
  77.  
  78. void ResetValues() {
  79.   value = 0; //Now reset ready for next input
  80.   totalMilliLitres = 0;
  81.   TurnOffVolume = 10000;
  82.   digitalWrite(valve, HIGH);
  83. }
  84. //--------------------------------------------------------------------------------------
  85.  
  86.  
  87.  
  88. //||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||Setup
  89. void setup() {
  90.  
  91.   //Serial.begin(38400);
  92.  
  93.   pinMode(valve, OUTPUT);
  94.   digitalWrite(valve, HIGH);
  95.  
  96.   //--------------------------  2004A (0x3F) 20x4 LCD Screen ---------------------------
  97.   //-----------------------------------------------./Setup------------------------------
  98.   lcd.begin(20, 4);
  99.   lcd.clear();
  100.   lcd.setCursor(0, 1);
  101.   lcd.print("AUTOMATIC FLOW METER");
  102.   lcd.setCursor(7, 2);
  103.   lcd.print("CONTROL");
  104.   delay(1000);
  105.   lcd.clear();
  106.   lcd.setCursor(0, 0);
  107.   lcd.print("Enter Water Amount :");
  108.   lcd.setCursor(0, 2);
  109.   lcd.print("Output Liquid : ");
  110.   lcd.setCursor(0, 3);
  111.   lcd.print(totalMilliLitres);
  112.   lcd.print(" (mL)");
  113.   lcd.setCursor(0, 1);
  114.   lcd.setCursor(0, 1);
  115.  
  116.  
  117.   //------------------------------------------------------------------------------------
  118.  
  119.  
  120.   //--------------------------  YF-S201 Sensor  ----------------------------------------
  121.   //-----------------------------------------------./Setup------------------------------
  122.  
  123.   pinMode(sensorPin, INPUT);
  124.   digitalWrite(sensorPin, HIGH);
  125.  
  126.   pulseCount        = 0;
  127.   flowRate          = 0.0;
  128.   flowMilliLitres   = 0;
  129.   totalMilliLitres  = 0;
  130.   oldTime           = 0;
  131.  
  132.   attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  133.  
  134.   //------------------------------------------------------------------------------------
  135.  
  136.   delay(1000);
  137.   ResetValues();//To Reset All Variables And Start A New Operation
  138.  
  139. }
  140. //||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||Setup
  141.  
  142.  
  143. //||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||Loop
  144. void loop() {
  145.  
  146.  
  147.  
  148.   //------------------------- Calculate Volume Of Liquid -------------------------------
  149.   Volume();
  150.   //------------------------------------------------------------------------------------
  151.  
  152.  
  153.   //----------------------------- Stopping Condition -----------------------------------
  154.   if (totalMilliLitres + (0.03 * totalMilliLitres) >= TurnOffVolume) {
  155.     digitalWrite(valve, HIGH);
  156.     // Print the cumulative total of litres flowed since starting
  157.     lcd.setCursor(0, 2);
  158.     lcd.print("Output Liquid : ");
  159.     lcd.setCursor(0, 3);
  160.     lcd.print(totalMilliLitres);
  161.     lcd.print(" (mL)");
  162.     delay(3000);
  163.     lcd.clear();
  164.     lcd.setCursor(0, 0);
  165.     lcd.print("Enter Water Amount :");
  166.     lcd.setCursor(0, 2);
  167.     lcd.print("Output Liquid : ");
  168.     lcd.setCursor(0, 3);
  169.     lcd.print(totalMilliLitres);
  170.     lcd.print(" (mL)");
  171.     lcd.setCursor(0, 1);
  172.  
  173.     ResetValues();//To Reset All Variables And Start A New Operation
  174.   }
  175.   //------------------------------------------------------------------------------------
  176.  
  177.  
  178.   //----------- Saving Keypad Inputs In A Variable For Further Operations --------------
  179.   char key = keypad.getKey();
  180.   //------------------------------------------------------------------------------------
  181.  
  182.   //------------ Code To Stop Showing "A" , "B" , "C" , "D" When Pressed ---------------
  183.   if (key == 'A' || key == 'B' || key == 'C' || key == 'D') {
  184.     //lcd.print("");
  185.   }
  186.  
  187.   else
  188.   {
  189.     // lcd.print(key);
  190.   }
  191.  
  192.   //------------------------------------------------------------------------------------
  193.  
  194.  
  195.   delay(1);
  196.  
  197.  
  198.  
  199.   if (key != NO_KEY)
  200.   {
  201.  
  202.  
  203.     if ( (key >= '0') && (key <= '9') )
  204.     {
  205.       lcd.print(key);
  206.       value = value * 10;
  207.       value = value + key - '0';
  208.     }
  209.     /////////////////////////////////////////////////////////////////////////////////
  210.  
  211.     //       A       \\
  212.  
  213.     /////////////////////////////////////////////////////////////////////////////////
  214.  
  215.     if ( key == 'A' )
  216.     {
  217.       lcd.clear();
  218.       lcd.setCursor(0, 0);
  219.       lcd.print("Amount is SET to:");
  220.       lcd.setCursor(0, 1);
  221.       lcd.print("<");
  222.       lcd.print(value);
  223.       lcd.print("> (mL)");
  224.       lcd.setCursor(0, 2);
  225.       lcd.print("Output Liquid : ");
  226.       lcd.setCursor(0, 3);
  227.       lcd.print(totalMilliLitres);
  228.       lcd.print(" (mL)");
  229.       lcd.setCursor(0, 1);
  230.       if (value == 0) {
  231.         ResetValues();//To Reset All Variables And Start A New Operation
  232.       }
  233.       else {
  234.         TurnOffVolume = value;
  235.         digitalWrite(valve, LOW);// Turn ON Pump And Valve (Relay Active LOW)
  236.       }
  237.     }
  238.   }
  239.  
  240.   /////////////////////////////////////////////////////////////////////////////////
  241.  
  242.   //       B       \\
  243.  
  244.   /////////////////////////////////////////////////////////////////////////////////
  245.  
  246.   if (key == 'B')
  247.   {
  248.     ResetValues();//To Reset All Variables And Start A New Operation
  249.     lcd.clear();
  250.     lcd.setCursor(0, 0);
  251.     lcd.print("Enter Water Amount :");
  252.     lcd.setCursor(0, 2);
  253.     lcd.print("Output Liquid : ");
  254.     lcd.setCursor(0, 3);
  255.     lcd.print(totalMilliLitres);
  256.     lcd.print(" (mL)");
  257.     lcd.setCursor(0, 1);
  258.   }
  259.  
  260.   /////////////////////////////////////////////////////////////////////////////////
  261.  
  262.   //       C       \\
  263.  
  264.   /////////////////////////////////////////////////////////////////////////////////
  265.  
  266.   if (key == 'C')
  267.   {
  268.     ResetValues();//To Reset All Variables And Start A New Operation
  269.     digitalWrite(valve, LOW);// Turn ON Pump And Valve (Relay Active LOW)
  270.     lcd.clear();
  271.     lcd.setCursor(0, 1);
  272.     lcd.print("Water Valve is Open");
  273.     lcd.setCursor(0, 2);
  274.     lcd.print("     Caution !");
  275.   }
  276.  
  277.   /////////////////////////////////////////////////////////////////////////////////
  278.  
  279.   //       D       \\
  280.  
  281.   /////////////////////////////////////////////////////////////////////////////////
  282.  
  283.   if (key == 'D')
  284.   {
  285.     digitalWrite(valve, HIGH);// Turn Off Pump And Valve (Relay Active LOW)
  286.     lcd.clear();
  287.     lcd.setCursor(0, 0);
  288.     lcd.print("Enter Water Amount :");
  289.     lcd.setCursor(0, 2);
  290.     lcd.print("Output Liquid : ");
  291.     lcd.setCursor(0, 3);
  292.     lcd.print(totalMilliLitres);
  293.     lcd.print(" (mL)");
  294.     lcd.setCursor(0, 1);
  295.     ResetValues();//To Reset All Variables And Start A New Operation
  296.   }
  297.  
  298.   /////////////////////////////////////////////////////////////////////////////////
  299.  
  300.   //       *       \\
  301.  
  302.   /////////////////////////////////////////////////////////////////////////////////
  303.  
  304.   if (key == '*')
  305.   {
  306.     digitalWrite(valve, HIGH);// Turn Off Pump And Valve (Relay Active LOW)
  307.  
  308.     lcd.clear();
  309.  
  310.     lcd.setCursor(0, 1);
  311.     lcd.print("AUTOMATIC FLOW METER");
  312.     lcd.setCursor(7, 2);
  313.     lcd.print("CONTROL");
  314.     delay(5000);
  315.  
  316.  
  317.     lcd.clear();
  318.  
  319.     lcd.setCursor(0, 0);
  320.     lcd.print("Enter Water Amount :");
  321.     lcd.setCursor(0, 2);
  322.     lcd.print("Output Liquid : ");
  323.     lcd.setCursor(0, 3);
  324.     lcd.print(totalMilliLitres);
  325.     lcd.print(" (mL)");
  326.     lcd.setCursor(0, 1);
  327.     ResetValues();//To Reset All Variables And Start A New Operation
  328.   }
  329.  
  330.   /////////////////////////////////////////////////////////////////////////////////
  331.  
  332. }//end of loop
  333. //||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||Loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement