Components and supplies
Resistor 10k ohm
Relay Module (Generic)
Arduino Nano R3
Pushbutton switch 12mm
1N4007 – High Voltage, High Current Rated Diode
0.96" OLED 64x128 Display Module
Tools and machines
Soldering iron (generic)
Project description
Code
code
arduino
1#include <Wire.h> 2#include <ACROBOTIC_SSD1306.h> 3 4int Off = LOW; 5int On = HIGH; 6 7//initializing states 8int WaterRelayState = On; 9int PowerRelayState = On; 10int MemoryState = Off; 11int ServerState = 0; 12int TSSourceCity = 0; 13int TSSourceRain = 0; 14 15//setting GPIO pins 16const int ManualOverride = 8; 17const int WaterSupplySwitch = 7; 18const int PowerSupplySwitch = 6; 19const int PressureSensor = A0; 20const int BottomSensor = 4; 21const int TopSensor = 5; 22 23//pressure var 24float CurrentPressure = 0; 25float WaterPressureToggle = 0; 26float WaterPressureToggleHIGH = 0; 27float WaterPressureRef = 0; 28int LowValue = 0; 29int HighValue = 0; 30 31boolean FLOW = false; 32boolean RECALIBRATED = true; 33boolean ManualFlow = false; 34 35String Source = ""; 36String val = ""; 37 38//variables for delay without using delay 39unsigned long CurrentMillis = 0; // stores the value of millis() in each iteration of loop() 40unsigned long PreviousMillis = 0; 41unsigned long flowmillis = 0; 42 43 44//variable to protect against disconnected hose 45unsigned long RunningWater = 0; 46 47//constants 48const long Interval = 1000; 49//const long Interval_Wifi = 15000; 50const int MaxWaterDuration = 300; 51 52const int led = 13; 53 54 55 56void calibrate(){ 57 CurrentPressure = analogRead(PressureSensor); 58 WaterPressureRef = CurrentPressure; 59 } 60 61void setup(void) { 62 Serial.begin(115200); 63 pinMode(ManualOverride, INPUT); 64 pinMode(WaterSupplySwitch, OUTPUT); 65 pinMode(PowerSupplySwitch, OUTPUT); 66 pinMode(BottomSensor, INPUT); 67 pinMode(TopSensor, INPUT); 68 pinMode(PressureSensor, INPUT); 69 70 digitalWrite(PowerSupplySwitch, PowerRelayState); 71 digitalWrite(WaterSupplySwitch, WaterRelayState); 72 FLOW = true; 73 //checkwaterlevel(1); 74 delay(5000); 75 FLOW = false; 76 77 calibrate(); 78 79 Wire.begin(); 80 oled.init(); // Initialze SSD1306 OLED display 81 oled.clearDisplay(); // clear screen 82} 83 84void loop(void) { 85 86 CurrentMillis = millis(); 87 88 if(digitalRead(ManualOverride) == LOW){ 89 ManualFlow = true;} 90 else{ 91 ManualFlow = false; 92 } 93 94 if(CurrentMillis - PreviousMillis >= Interval) { 95 PreviousMillis = CurrentMillis; 96 CurrentPressure = analogRead(PressureSensor); 97 Serial.print("CurrentPressure"); 98 Serial.println(CurrentPressure); 99 checkwaterlevel(0); 100 } 101} 102 103void sense(){ 104 WaterPressureToggle = ((100/WaterPressureRef)*CurrentPressure); 105 if(Source == "Citywater"){LowValue = 85;HighValue = 89;} 106 else{LowValue = 65;HighValue = 80; 107 } 108 Serial.print("WaterPressureToggle"); 109 if(WaterPressureToggle < LowValue or ManualFlow == true){ 110 FLOW = true; 111 } 112 else{ 113 if(WaterPressureToggle > HighValue){ 114 FLOW = false; 115 WaterPressureToggleHIGH = WaterPressureToggle; 116 } 117 } 118 } 119 120void checkwaterlevel(int x) { 121 Serial.print("Flow state = "); 122 Serial.println(ManualFlow); 123 /*if (RunningWater > MaxWaterDuration) { 124 sensorfault(); 125 }*/ 126 127 if (x==0){sense();} 128 129 if (FLOW or ManualFlow) { 130 flowmillis = millis(); 131 PowerRelayState = On; 132 RunningWater += (Interval / 1000); 133 if (digitalRead(BottomSensor) == LOW and digitalRead(TopSensor) == LOW) {//LOW means sensor is floating 134 WaterRelayState = On; 135 MemoryState = WaterRelayState; 136 Source = "Rainwater"; 137 Serial.println("Bottomsensor and Topsensor closed, running on rainwater"); 138 } 139 else{ 140 if(digitalRead(BottomSensor) == LOW){ 141 WaterRelayState = MemoryState; 142 if(WaterRelayState == On){Source = "Rainwater";} 143 else{Source = "Citywater";} 144 } 145 else{ 146 WaterRelayState = Off; 147 MemoryState = WaterRelayState; 148 Source = "Citywater"; 149 Serial.println("Bottomsensor open, running on Pidpa");} 150 } 151 } 152 else 153 { 154 if(CurrentMillis - flowmillis >= Interval * 10) 155 { 156 calibrate(); 157 PowerRelayState = Off; 158 WaterRelayState = Off; 159 Source = "No power "; 160 RunningWater = 0; 161 } 162 } 163 lcd(); 164 digitalWrite(PowerSupplySwitch, PowerRelayState); 165 digitalWrite(WaterSupplySwitch, WaterRelayState); 166 167} 168 169void lcd() { 170 oled.setTextXY(0, 0); // Set cursor position, start of line 0 171 oled.putString("Running on "); 172 oled.setTextXY(1, 0); // Set cursor position, start of line 1 173 oled.putString(Source); 174 oled.setTextXY(2,0); 175 oled.putString("Top="); 176 oled.setTextXY(2,4); 177 if (digitalRead(TopSensor) == LOW){oled.putString("WET");}else{oled.putString("DRY");} 178 oled.setTextXY(2,9); 179 oled.putString("Bot="); 180 oled.setTextXY(2,13); 181 if (digitalRead(BottomSensor) == LOW){oled.putString("WET");}else{oled.putString("DRY");} 182 183 oled.setTextXY(3, 0); 184 oled.putString("LO "); 185 oled.setTextXY(3,4); 186 oled.putString(" "); 187 oled.setTextXY(3,4); 188 oled.putNumber(WaterPressureToggle); 189 oled.setTextXY(3,9); 190 oled.putString("HI "); 191 oled.setTextXY(3,13); 192 oled.putString(" "); 193 oled.setTextXY(3,13); 194 oled.putNumber(WaterPressureToggleHIGH); 195 oled.setTextXY(4, 0); 196 if (PowerRelayState == On) 197 { 198 oled.putString("valves opened"); 199 } 200 else 201 { 202 oled.putString("valves closed"); 203 } 204 oled.setTextXY(5, 0); 205 val = digitalRead(WaterSupplySwitch); 206 oled.putString(" "); 207 oled.setTextXY(5, 0); 208 oled.putString(val); 209 oled.setTextXY(6, 0); 210 oled.putString(" "); 211 oled.setTextXY(6, 0); 212 oled.putNumber(WaterPressureToggle); 213}
code
arduino
1#include <Wire.h> 2#include <ACROBOTIC_SSD1306.h> 3 4int Off = LOW; 5int On = HIGH; 6 7//initializing states 8int WaterRelayState = On; 9int PowerRelayState = On; 10int MemoryState = Off; 11int ServerState = 0; 12int TSSourceCity = 0; 13int TSSourceRain = 0; 14 15//setting GPIO pins 16const int ManualOverride = 8; 17const int WaterSupplySwitch = 7; 18const int PowerSupplySwitch = 6; 19const int PressureSensor = A0; 20const int BottomSensor = 4; 21const int TopSensor = 5; 22 23//pressure var 24float CurrentPressure = 0; 25float WaterPressureToggle = 0; 26float WaterPressureToggleHIGH = 0; 27float WaterPressureRef = 0; 28int LowValue = 0; 29int HighValue = 0; 30 31boolean FLOW = false; 32boolean RECALIBRATED = true; 33boolean ManualFlow = false; 34 35String Source = ""; 36String val = ""; 37 38//variables for delay without using delay 39unsigned long CurrentMillis = 0; // stores the value of millis() in each iteration of loop() 40unsigned long PreviousMillis = 0; 41unsigned long flowmillis = 0; 42 43 44//variable to protect against disconnected hose 45unsigned long RunningWater = 0; 46 47//constants 48const long Interval = 1000; 49//const long Interval_Wifi = 15000; 50const int MaxWaterDuration = 300; 51 52const int led = 13; 53 54 55 56void calibrate(){ 57 CurrentPressure = analogRead(PressureSensor); 58 WaterPressureRef = CurrentPressure; 59 } 60 61void setup(void) { 62 Serial.begin(115200); 63 pinMode(ManualOverride, INPUT); 64 pinMode(WaterSupplySwitch, OUTPUT); 65 pinMode(PowerSupplySwitch, OUTPUT); 66 pinMode(BottomSensor, INPUT); 67 pinMode(TopSensor, INPUT); 68 pinMode(PressureSensor, INPUT); 69 70 digitalWrite(PowerSupplySwitch, PowerRelayState); 71 digitalWrite(WaterSupplySwitch, WaterRelayState); 72 FLOW = true; 73 //checkwaterlevel(1); 74 delay(5000); 75 FLOW = false; 76 77 calibrate(); 78 79 Wire.begin(); 80 oled.init(); // Initialze SSD1306 OLED display 81 oled.clearDisplay(); // clear screen 82} 83 84void loop(void) { 85 86 CurrentMillis = millis(); 87 88 if(digitalRead(ManualOverride) == LOW){ 89 ManualFlow = true;} 90 else{ 91 ManualFlow = false; 92 } 93 94 if(CurrentMillis - PreviousMillis >= Interval) { 95 PreviousMillis = CurrentMillis; 96 CurrentPressure = analogRead(PressureSensor); 97 Serial.print("CurrentPressure"); 98 Serial.println(CurrentPressure); 99 checkwaterlevel(0); 100 } 101} 102 103void sense(){ 104 WaterPressureToggle = ((100/WaterPressureRef)*CurrentPressure); 105 if(Source == "Citywater"){LowValue = 85;HighValue = 89;} 106 else{LowValue = 65;HighValue = 80; 107 } 108 Serial.print("WaterPressureToggle"); 109 if(WaterPressureToggle < LowValue or ManualFlow == true){ 110 FLOW = true; 111 } 112 else{ 113 if(WaterPressureToggle > HighValue){ 114 FLOW = false; 115 WaterPressureToggleHIGH = WaterPressureToggle; 116 } 117 } 118 } 119 120void checkwaterlevel(int x) { 121 Serial.print("Flow state = "); 122 Serial.println(ManualFlow); 123 /*if (RunningWater > MaxWaterDuration) { 124 sensorfault(); 125 }*/ 126 127 if (x==0){sense();} 128 129 if (FLOW or ManualFlow) { 130 flowmillis = millis(); 131 PowerRelayState = On; 132 RunningWater += (Interval / 1000); 133 if (digitalRead(BottomSensor) == LOW and digitalRead(TopSensor) == LOW) {//LOW means sensor is floating 134 WaterRelayState = On; 135 MemoryState = WaterRelayState; 136 Source = "Rainwater"; 137 Serial.println("Bottomsensor and Topsensor closed, running on rainwater"); 138 } 139 else{ 140 if(digitalRead(BottomSensor) == LOW){ 141 WaterRelayState = MemoryState; 142 if(WaterRelayState == On){Source = "Rainwater";} 143 else{Source = "Citywater";} 144 } 145 else{ 146 WaterRelayState = Off; 147 MemoryState = WaterRelayState; 148 Source = "Citywater"; 149 Serial.println("Bottomsensor open, running on Pidpa");} 150 } 151 } 152 else 153 { 154 if(CurrentMillis - flowmillis >= Interval * 10) 155 { 156 calibrate(); 157 PowerRelayState = Off; 158 WaterRelayState = Off; 159 Source = "No power "; 160 RunningWater = 0; 161 } 162 } 163 lcd(); 164 digitalWrite(PowerSupplySwitch, PowerRelayState); 165 digitalWrite(WaterSupplySwitch, WaterRelayState); 166 167} 168 169void lcd() { 170 oled.setTextXY(0, 0); // Set cursor position, start of line 0 171 oled.putString("Running on "); 172 oled.setTextXY(1, 0); // Set cursor position, start of line 1 173 oled.putString(Source); 174 oled.setTextXY(2,0); 175 oled.putString("Top="); 176 oled.setTextXY(2,4); 177 if (digitalRead(TopSensor) == LOW){oled.putString("WET");}else{oled.putString("DRY");} 178 oled.setTextXY(2,9); 179 oled.putString("Bot="); 180 oled.setTextXY(2,13); 181 if (digitalRead(BottomSensor) == LOW){oled.putString("WET");}else{oled.putString("DRY");} 182 183 oled.setTextXY(3, 0); 184 oled.putString("LO "); 185 oled.setTextXY(3,4); 186 oled.putString(" "); 187 oled.setTextXY(3,4); 188 oled.putNumber(WaterPressureToggle); 189 oled.setTextXY(3,9); 190 oled.putString("HI "); 191 oled.setTextXY(3,13); 192 oled.putString(" "); 193 oled.setTextXY(3,13); 194 oled.putNumber(WaterPressureToggleHIGH); 195 oled.setTextXY(4, 0); 196 if (PowerRelayState == On) 197 { 198 oled.putString("valves opened"); 199 } 200 else 201 { 202 oled.putString("valves closed"); 203 } 204 oled.setTextXY(5, 0); 205 val = digitalRead(WaterSupplySwitch); 206 oled.putString(" "); 207 oled.setTextXY(5, 0); 208 oled.putString(val); 209 oled.setTextXY(6, 0); 210 oled.putString(" "); 211 oled.setTextXY(6, 0); 212 oled.putNumber(WaterPressureToggle); 213}
Downloadable files
schematics
schematics
Comments
Only logged in users can leave comments
designedrat
0 Followers
•0 Projects
Table of contents
Intro
11
0