Components and supplies
Cable Gland Nylon Plastic Waterproof Adjustable, Cable Glands Joints Wire Protectors- Pg7, Pg9, Pg11, Pg13.5, Pg16 35pcs
AC-DC Isolated Switching Power Supply Module Input 85V-264V Output 5V 2A 10W
Lexan Sheet - Polycarbonate - .060" - 1/16"Thick, Clear, 12" x 12" Nominal
NOYITO 30A 2-Channel Relay Module High Low Level Trigger With Optocoupler Isolation Load DC 30V AC 250V 30A for PLC Automation Equipment Control, Industrial Control (2-Channel 5V)
AUTEX Pressure Transducer/Sender/Sensor 150/200 Psi Stainless Steel Compatible With Oil, Fuel, Air, Water (150 Psi)
FTCBlock IIC I2C TWI Serial 2004 20x4 LCD Module Shield with IIC/I2C Serial Interface Adapter Module for Arduino Uno Mega2560
Gikfun Prototype Shield DIY KIT For Arduino UNO R3 Mega 328P (Pack of 3 Sets) Ek1038x3
1/4" NPT 2 Way 2 Position Pneumatic Electric Solenoid Valve AC 110 V
Arduino UNO
Metal Enclosure, Shielded, Diecast, IP54, Instrument, Aluminium Alloy, IP54, 188 mm, 120 mm
12MM Waterproof Momentary Push Button Switch 15PCS ON- OFF Switch (5 Colors)
5 mm LED: Red
Tools and machines
Soldering iron (generic)
Drill / Driver, 20V
Project description
Code
The Controller code
arduino
1/* 2 WWTP_Compressor_Controller_rev24 3 RedTar4 4 8/20/19-8/29/19 5 6 Analog Resolution is 0-1023 (204.8 per volt or 0.004883v per resolution) 7 Pressure sensor is linear: 8 0 psi = 0.5v = 102.4 9 150 psi = 4.5v = 921.6 10*/ 11 12// this is the libraries section 13#include <Wire.h> // Wire library 14#include <LiquidCrystal_I2C.h> // Liquid Crystal I2C library 15 LiquidCrystal_I2C lcd(0x27,20,4); // Display address 0x27, I2C 20 x 4 16#include <EEPROM.h> // EEPROM library 17#include <Bounce2.h> // Bounce2 library 18 Bounce debouncer = Bounce(); // instantiate a bounce object 19 20// this is the digital input button variables section 21int P1 = 2; // button SET MENU' // this sets the tag as a discreate input to pin 2 22int P2 = 3; // button + // this sets the tag as a discreate input to pin 3 23int P3 = 4; // button - // this sets the tag as a discreate input to pin 4 24 25// this is the menu section 26int menu = 0; // this sets the variable tag menu to 0 27 28// this is the analog input variable section 29int CompPSIRaw = A0; // this sets the tag as an analog input to pin A0 30 31// this is the digital output variables section 32int CompAlarm = 9; // this sets the tag as a discrete output to pin 12 33int CompRun = 10; // this sets the tag as a discrete output to pin 10 34int CompUnloadValve = 11; // this sets the tag as a discrete output to pin 11 35int CompDrain = 12; // this sets the tag as a discrete output to pin 12 36 37// this is the variables section 38int Start = LOW; // this sets the variable tag that stores the start psi set point tolow 39int Stop = LOW; // this sets the variable tag that stores the stop psi set point to low 40int CompRunBit = LOW; // this sets the variable tag CompRunBit to low 41int CompUnloadValveBit = LOW; // this sets the variable tag CompUnloadValveBit to low 42int CompDrainBit = LOW; 43int CompDrainOn = LOW; // this sets the variable tag CompDrainOn bit 44int CompDrainOff = LOW; // this sets the variable tag CompDrainOff bit 45int CompDrainManual = LOW; // this sets the variable tag CompDrainManual to low 46int CompDrainOffCalc = 0; 47int CompAlarmBitL = LOW; // this sets the variable tag CompAlarmBitL to low 48int CompAlarmBitH = LOW; // this sets the variable tag CompAlarmBitH to low 49int CompManual = LOW; // this sets the variable tag manual to 0 50int CompPSIValue = 0; // this sets the tag CompPSIAlarmL, that will store the value coming from the sensor 51int CompPSIAlarmL = LOW; // this sets the tag CompPSIAlarmL, that will store the value for the raw alarm low setpoint 52int CompPSIAlarmH = LOW; // this sets the tag CompPSIAlarmH, that will store the value for the raw alarm high setpoint 53 54// this is the EEPROM section 55int eeAddress1 = 10; // this sets the eeAddress1 tag and sets the address to 10, eeprom 56int eeAddress2 = 20; // this sets the eeAddress2 tag and sets the address to 20, eeprom 57int eeAddress3 = 30; // this sets the eeAddress3 tag and sets the address to 30, eeprom 58int eeAddress4 = 40; // this sets the eeAddress4 tag and sets the address to 40, eeprom 59int eeAddress5 = 50; // this sets the eeAddress5 tag and sets the address to 50, eeprom 60int eeAddress6 = 60; // this sets the eeAddress6 tag and sets the address to 60, eeprom 61 62// this is the millis section, the "Clock" 63long previousMillis = 0; // this sets the tag "previousMillis", that will store the last time "delay" that was updated 64long interval = 30000; // this sets the tag "interval" for the menu(9) return timer 65long intervalSec = 500; // this sets the tag "intervalSec", at which to blink (milliseconds), 500 = 1 second, 30000 = 1 minute 66int ledStateSec = 0; // this sets the tag "ledStateSec", 67 68 69// this is the Debounce section 70int buttonState; // this set the tag "buttonState", to the current button state from the input pin 71int lastButtonState = LOW; // this set the tag "lastButtonState", to the previous button state from the input pin 72unsigned long lastDebounceTime = 0; // the last time the output pin was toggled 73unsigned long debounceDelay = 100; // the debounce time; increase if the output flickers 74 75void setup() 76{ 77 78// this section assigns the inputs and outputs 79 // inputs 80 pinMode(P1,INPUT_PULLUP); // button "MENU" 81 debouncer.attach(P1); // Attach the debouncer to P1 with INPUT_PULLUP mode 82 debouncer.interval(debounceDelay); // interval in ms 83 pinMode(P2,INPUT_PULLUP); // button "+" advance 84 debouncer.attach(P2); // Attach the debouncer to P2 with INPUT_PULLUP mode 85 debouncer.interval(debounceDelay); // interval in ms 86 pinMode(P3,INPUT_PULLUP); // button "-" decrease 87 debouncer.attach(P3); // Attach the debouncer to P3 with INPUT_PULLUP mode 88 debouncer.interval(debounceDelay); // interval in ms 89 // outputs 90 pinMode(CompAlarm, OUTPUT); // comperssor alarm relay 91 digitalWrite(CompAlarm, LOW); // initially sets output tag "CompAlarm" low 92 pinMode(CompRun, OUTPUT); // comperssor run relay 93 digitalWrite(CompRun, LOW); // initially sets output tag "CompRun" low 94 pinMode(CompUnloadValve, OUTPUT); // comperssor unloader valve relay 95 digitalWrite(CompUnloadValve, LOW); // initially sets output tag "CompUnloadValve" low 96 pinMode(CompDrain, OUTPUT); // comperssor drain valve relay 97 digitalWrite(CompDrain, LOW); // initially sets output tag "CompDrain" low 98 99// this section retrieves the setpoints from the arduino eeprom so the unit can start up after a power fail 100 EEPROM.get(eeAddress1, Start); // retrieves the start psi from eeprom 101 EEPROM.get(eeAddress2, Stop); // retrieves the stop psi from eeprom 102 EEPROM.get(eeAddress3, CompPSIAlarmL); // retrieves the lo alarm from eeprom 103 EEPROM.get(eeAddress4, CompPSIAlarmH); // retrieves the hi alarm from eeprom 104 EEPROM.get(eeAddress5, CompDrainOn); // retrieves the hi alarm from eeprom 105 EEPROM.get(eeAddress6, CompDrainOff); // retrieves the hi alarm from eeprom 106 107// this section starts up the different libraries 108 Serial.begin(9600); // start the serial monitor at 9600 baud 109 lcd.init(); // start the lcd library 110 lcd.backlight(); // turn on the lcd backlight 111 lcd.clear(); // clear the cld screen 112 Wire.begin(); // start the I2C library 113} 114 115void loop() 116{ 117 118// Update the Bounce instance : 119 debouncer.update(); 120 121// this is the menu selection section 122 if(digitalRead(P1)){menu=menu+1;} // press the MENU(P1)button and advance through the menu index by 1 123 debouncer.attach(P1); // Attach the debouncer to P1 with INPUT_PULLUP mode 124 debouncer.interval(debounceDelay); // intervalMin in ms 125 if(menu==0){DisplayPSI();} // go to the DisplayPSI subroutine 126 if(menu==1){DisplayStart();} // go to the DisplayStart subroutine 127 if(menu==2){DisplayStop();} // go to the DisplayStop subroutine 128 if(menu==3){DisplayAlarmLow();} // go to the DisplayAlarmLow subroutine 129 if(menu==4){DisplayAlarmHigh();} // go to the DisplayAlarmHigh subroutine 130 if(menu==5){DisplayCompDrainOn();} // go to the DisplayCompDrainOn subroutine 131 if(menu==6){DisplayCompDrainOff();} // go to the DisplayCompDrainOff subroutine 132 if(menu==7){DisplayCompDrainManual();} // go to the DisplayCompDrainManual subroutine 133 if(menu==8){DisplayCompMan();} // go to the DisplayAutoMan subroutine 134 if(menu==9){StoreAll(); delay(500); menu=0;} // go to the StoreAll subroutine and set the menu tag to 0 135 delay(100); 136} 137 138// this section declares the minutecount variable and sets its initial values 139volatile unsigned long secondCount = 0; // use volatile for shared variables 140 141void DisplayPSI () // main display 142{ 143 144// this section keeps track of the millis counts (this is the "clock!") 145 // millis is running in the background so any program delays wont effect the timing 146 // only copies are used so accuracy is mantained 147unsigned long currentMillis = millis(); // updates and holds a copy of the SecondMillis 148 if (currentMillis - previousMillis >= intervalSec) 149 { 150 previousMillis = currentMillis; // updates and holds a copy of the previousMillis 151 if (ledStateSec == LOW ){ledStateSec = HIGH; secondCount = secondCount + 1;} 152 else {ledStateSec = LOW;} 153 } 154unsigned long secondCopy; 155 secondCopy = secondCount; //updates and holds a copy of the secondCount 156 157// this section monitors the live psi and turns the compressor run bit on or off based off setpoints 158 int psi = analogRead(0); // this reads the analog input(A0) and scales it 159 psi = map(psi, 102, 921, 0, 150); // this maps the raw analog input value to the converted PSI value 160 if (psi <= Start){CompRunBit = HIGH;} // if psi is less than start setpoint turn on run bit 161 if (psi >= Stop ){CompRunBit = LOW;} // if psi is greater than stop setpoint turn off run bit 162 if (psi <= Start){CompUnloadValveBit = LOW;} // if psi is less than start setpoint turn off unload bit 163 if (psi >= Stop ){CompUnloadValveBit = HIGH;} // if psi is greater than stop setpoint turn on unload bit 164 165// this section monitors the raw analog input for errors 166 CompPSIValue = analogRead(CompPSIRaw); 167 if (CompPSIValue < CompPSIAlarmL){CompAlarmBitL = HIGH;} // if comppsivalue is less than the CompPSIAlarmL bit, set alarm low bit high 168 if (CompPSIValue > CompPSIAlarmL){CompAlarmBitL = LOW;} // if comppsivalue is greater than the CompPSIAlarmL bit, set alarm low bit low 169 if (CompPSIValue < CompPSIAlarmH){CompAlarmBitH = LOW;} // if comppsivalue is less than the CompAlarmBitH bit, set alarm high bit low 170 if (CompPSIValue > CompPSIAlarmH){CompAlarmBitH = HIGH;} // if comppsivalue is greater than the CompAlarmBitH bit, set alarm high bit high 171 172// this section controls the drain timing 173 if (CompDrainOn >= secondCount) {CompDrainBit = HIGH;} 174 else {CompDrainBit = LOW;} 175int CompDrainOffCalc = CompDrainOff * 60; 176 if (CompDrainOffCalc <= secondCount + 1) {secondCount = 0;} 177 178// this section turns on or off the digital outputs based on the control and alarm bits 179 digitalWrite(CompRun, CompRunBit && !CompAlarmBitL && !CompAlarmBitH); // comp run safety interlock if sensor fails 180 digitalWrite(CompUnloadValve, CompUnloadValveBit || CompAlarmBitL || CompAlarmBitH); // comp unload valve safety interlock if sensor fails 181 digitalWrite(CompAlarm, CompAlarmBitL || CompAlarmBitH); // comp alarm if sensor fails 182 digitalWrite(CompDrain, CompDrainBit); // comp drain valve 183 184// this is the serial monitor setion for testing and debug. remove the "//" to print value to the serial monitor 185// Serial.print(" Sensor raw value "); 186// Serial.print(CompPSIValue); 187// Serial.print(" Sensor psi value "); 188// Serial.print(psi); 189// Serial.print(", Start @ "); 190// Serial.print(Start); 191// Serial.print(", Stop @ "); 192// Serial.print(Stop); 193// Serial.print(","); 194// Serial.print(" Low alarm "); 195// Serial.print(CompPSIAlarmL); 196// Serial.print(","); 197// Serial.print(" High alarm "); 198// Serial.print(CompPSIAlarmH); 199// Serial.print(", CRB "); 200// Serial.print(CompRunBit); 201// Serial.print(", CUL "); 202// Serial.print(CompUnloadValveBit); 203// Serial.print(", CAL "); 204// Serial.print(CompAlarmBitL); 205// Serial.print(", CAH "); 206// Serial.print(CompAlarmBitH); 207// Serial.print(" CompDrain "); 208// Serial.print(CompDrain); 209// Serial.print(" Comp On "); 210// Serial.print(CompDrainOn); 211// Serial.print(" Comp Off "); 212// Serial.print(CompDrainOff); 213// Serial.print(" Comp Off calc "); 214// Serial.print(CompDrainOffCalc); 215// Serial.print(" ledState sec "); 216// Serial.print(ledStateSec); 217// Serial.print(" intervalSec "); 218// Serial.print(intervalSec); 219// Serial.print(", Seconds count "); 220// Serial.print(secondCount); 221// Serial.print(", Seconds copy "); 222// Serial.print(secondCopy); 223// Serial.print(", Previous Millis "); 224 Serial.println(previousMillis); 225 226// this is the lcd section 227 // line 1 228 lcd.setCursor(0, 0); lcd.print("Currently @ "); // this prints whats in between the quotes 229 lcd.setCursor(13, 0); lcd.print(" "); // this clears the display field so anything left is deleted 230 lcd.setCursor(13, 0); lcd.print(psi); // this prints the tag value 231 lcd.setCursor(17, 0); lcd.print("PSI"); // this prints whats in between the quotes 232 // line 2 233 lcd.setCursor(0, 1); lcd.print(" Start @ "); // this prints whats in between the quotes 234 lcd.setCursor(13, 1); lcd.print(" "); // this clears the display field so anything left is deleted 235 lcd.setCursor(13, 1); lcd.print(Start); // this prints the tag value 236 lcd.setCursor(17, 1); lcd.print("PSI"); // this prints whats in between the quotes 237 // line 3 238 lcd.setCursor(0, 2); lcd.print(" Stop @ "); // this prints whats in between the quotes 239 lcd.setCursor(13, 2); lcd.print(" "); // this clears the display field so anything left is deleted 240 lcd.setCursor(13, 2); lcd.print(Stop); // this prints the tag value 241 lcd.setCursor(17, 2); lcd.print("PSI"); // this prints whats in between the quotes 242 // line 4 243 // this section displays the alarm/run status on lcd line 4 244 if (CompAlarmBitL && CompAlarmBitH == LOW); // if CompAlarmBitL and CompAlarmBitH alarm bits are low then its true and prints whats below 245 { 246 lcd.setCursor(0, 3); lcd.print(" "); // this clears the display field so anything left is deleted 247 lcd.setCursor(0, 3); lcd.print("Sensor OK "); // this prints whats in between the quotes 248 lcd.setCursor(11, 3); lcd.print(" Raw "); // this prints whats in between the quotes 249 lcd.setCursor(16, 3); lcd.print(" "); // this clears the display field so anything left is deleted 250 lcd.setCursor(16, 3); lcd.print(CompPSIValue); // this prints the tag value 251 } 252 if (CompAlarmBitL == HIGH){lcd.setCursor(0, 3); lcd.print("ALARM: "); lcd.setCursor(7, 3); lcd.print("Xmtr Open ");} // if the CompAlarmBitL alarm bit is high then its in alarm and prints text 253 if (CompAlarmBitH == HIGH){lcd.setCursor(0, 3); lcd.print("ALARM: "); lcd.setCursor(7, 3); lcd.print("Xmtr Short");} // if the CompAlarmBitH alarm bit is high then its in alarm and prints text 254 255} 256 257void DisplayStart() // this menu is for adjusting the start psi setpoint 258{ 259 260// this section advances to menu 9 after 30 sec's, so the program doesn't stall if left on this menu selection 261 unsigned long currentMillis = millis(); 262 if(currentMillis - previousMillis > interval) 263 {previousMillis = currentMillis; menu = 9;} 264 265// this section reads the +/- buttons and adjusts the start value 266 if(digitalRead(P2)== HIGH) 267 { 268 if (Start == 150){Start = 0;} // 150 is the max, sets back to 0 269 else {Start = Start + 1;} // incremnent by 1 per button press 270 } 271 272 if(digitalRead(P3) == HIGH) 273 { 274 if (Start == 0){Start = 150;} // 0 is the min, sets back to 150 275 else {Start = Start - 1;} // decrease by 1 per button press 276 } 277 278// this is the lcd section 279 lcd.clear(); 280 lcd.setCursor(0, 0); lcd.print(" Start PSI Menu:"); 281 lcd.setCursor(0, 1); lcd.print(" Set Start PSI to"); 282 lcd.setCursor(9, 3); lcd.print(Start,DEC); 283 delay(100); 284} 285 286void DisplayStop() // this menu is for adjusting the stop psi setpoint 287{ 288 289// this section advances to menu 9 after 30 sec's, so the program doesn't stall if left on this menu selection 290unsigned long currentMillis = millis(); 291 if(currentMillis - previousMillis > interval) 292 {previousMillis = currentMillis; menu = 9;} 293 294// this section reads the +/- buttons and adjusts the stop value 295 if(digitalRead(P2) == HIGH) 296 { 297 if (Stop == 150){Stop = 0;} // 150 is the max, sets back to 0 298 else {Stop = Stop + 1;} // incremnent by 1 per button press 299 } 300 301 if(digitalRead(P3) == HIGH) 302 { 303 if (Stop == 0){Stop = 150;} // 0 is the min, sets back to 150 304 else {Stop = Stop - 1;} // decrease by 1 per button press 305 } 306 307// this is the lcd section 308 lcd.clear(); 309 lcd.setCursor(0, 0); lcd.print(" Stop PSI Menu:"); 310 lcd.setCursor(0, 1); lcd.print(" Set Stop PSI to"); 311 lcd.setCursor(9, 3); lcd.print(Stop,DEC); 312 delay(100); 313} 314 315void DisplayAlarmLow() // this menu is for adjusting the raw alarm low setpoint 316{ 317 318// this section advances to menu 9 after 30 sec's, so the program doesn't stall if left on this menu selection 319unsigned long currentMillis = millis(); 320 if(currentMillis - previousMillis > interval) 321 {previousMillis = currentMillis; menu = 9;} 322 323// this section reads the +/- buttons and adjusts the raw low alarm setpoint 324 if(digitalRead(P2) == HIGH) 325 { 326 if (CompPSIAlarmL == 1023){CompPSIAlarmL = 0;} // 1023 is the max, sets to 0 327 else {CompPSIAlarmL = CompPSIAlarmL + 1;} // increase by 1 per button press 328 } 329 330 if(digitalRead(P3) == HIGH) 331 { 332 if (CompPSIAlarmL == 0){CompPSIAlarmL = 1023;} // 0 is the min, sets to 1023 333 else {CompPSIAlarmL = CompPSIAlarmL - 1;} // decrease by 1 per button press 334 } 335 336// this is the lcd section 337 lcd.clear(); 338 lcd.setCursor(0, 0); lcd.print(" Raw Alarm Lo Menu:"); 339 lcd.setCursor(0, 1); lcd.print(" Set Lo Alarm to"); 340 lcd.setCursor(9, 3); lcd.print(CompPSIAlarmL,DEC); 341 delay(100); 342} 343 344void DisplayAlarmHigh() // this menu is for adjusting the raw alarm high setpoint 345{ 346 347// this section advances to menu 9 after 30 sec's, so the program doesn't stall if left on this menu selection 348unsigned long currentMillis = millis(); 349 if(currentMillis - previousMillis > interval) 350 {previousMillis = currentMillis; menu = 9;} 351 352// this section reads the +/- buttons and adjusts the raw high alarm setpoint 353 if(digitalRead(P2) == HIGH) 354 { 355 if (CompPSIAlarmH == 1023){CompPSIAlarmH = 0;} // 1023 is the max, sets to 0 356 else {CompPSIAlarmH = CompPSIAlarmH + 1;} // increase by 1 per button press 357 } 358 359 if(digitalRead(P3) == HIGH) 360 { 361 if (CompPSIAlarmH == 0){CompPSIAlarmH = 1023;} // 0 is the min, sets to 1023 362 else {CompPSIAlarmH = CompPSIAlarmH - 1;} // decrease by 1 per button press 363 } 364 365// this is the lcd section 366 lcd.clear(); 367 lcd.setCursor(0, 0); lcd.print(" Raw Alarm Hi Menu:"); 368 lcd.setCursor(0, 1); lcd.print(" Set Hi Alarm to"); 369 lcd.setCursor(9, 3); lcd.print(CompPSIAlarmH,DEC); 370 delay(100); 371} 372 373void DisplayCompDrainOn() // this menu is for adjusting the drain on time setpoint 374{ 375 376// this section advances to menu 9 after 30 sec's, so the program doesn't stall if left on this menu selection 377unsigned long currentMillis = millis(); 378 if(currentMillis - previousMillis > interval) 379 {previousMillis = currentMillis; menu = 9;} 380 381// this section reads the +/- buttons and adjusts the drain on/off setpoint 382 if(digitalRead(P2) == HIGH) 383 { 384 if (CompDrainOn == 10){CompDrainOn = 0;} // 10 is the max, sets to 0 385 else {CompDrainOn = CompDrainOn + 1;} // increase by 1 per button press 386 } 387 388 if(digitalRead(P3) == HIGH) 389 { 390 if (CompDrainOn == 0){CompDrainOn = 10;} // 0 is the min, sets to 10 391 else {CompDrainOn = CompDrainOn - 1;} // decrease by 1 per button press 392 } 393 394// this is the lcd section 395 lcd.clear(); 396 lcd.setCursor(0, 0); lcd.print(" Drain On: "); 397 lcd.setCursor(0, 1); lcd.print("Set Drain on in sec"); 398 lcd.setCursor(9, 3); lcd.print(CompDrainOn,DEC); 399 400 401 delay(100); 402} 403 404void DisplayCompDrainOff() // this menu is for adjusting the drain off time setpoint 405{ 406 407// this section advances to menu 9 after 30 sec's, so the program doesn't stall if left on this menu selection 408unsigned long currentMillis = millis(); 409 if(currentMillis - previousMillis > interval) 410 {previousMillis = currentMillis; menu = 9;} 411 412// this section reads the +/- buttons and adjusts the drain on/off setpoint 413 if(digitalRead(P2) == HIGH) 414 { 415 if (CompDrainOff == 45){CompDrainOff = 0;} // 45 is the max, sets to 0 416 else {CompDrainOff = CompDrainOff + 1;} // increase by 1 per button press 417 } 418 419 if(digitalRead(P3) == HIGH) 420 { 421 if (CompDrainOff == 0){CompDrainOff = 45;} // 0 is the min, sets to 45 422 else {CompDrainOff = CompDrainOff - 1;} // decrease by 1 per button press 423 } 424 425// this is the lcd section 426 lcd.clear(); 427 lcd.setCursor(0, 0); lcd.print(" Drain off:"); 428 lcd.setCursor(0, 1); lcd.print("Set Drain Off in min"); 429 lcd.setCursor(9, 3); lcd.print(CompDrainOff,DEC); 430 431 delay(100); 432} 433 434void DisplayCompDrainManual() // this menu is for manually turning the drain on/off 435{ 436 437// this section advances to menu 9 after 30 sec's, so the program doesn't stall if left on this menu selection 438unsigned long currentMillis = millis(); 439 if(currentMillis - previousMillis > interval) 440 {previousMillis = currentMillis; menu = 9;} 441 442// this section reads the +/- buttons and adjusts the raw high alarm setpoint 443 if(digitalRead(P2) == HIGH) 444 { 445 if (CompDrainManual == 1){CompDrainManual = 0;} // 1 is the max, sets to 0 446 else {CompDrainManual = CompDrainManual + 1;} // increase by 1 per button press 447 } 448 449 if(digitalRead(P3) == HIGH) 450 { 451 if (CompDrainManual == 0){CompDrainManual = 1;} // 0 is the min, sets to 1 452 else {CompDrainManual = CompDrainManual - 1;} // decrease by 1 per button press 453 } 454 digitalWrite(CompDrain, CompDrainManual); // turns on/off compDrain relay based on CompDrainManual 455 456// this is the lcd section 457 458 lcd.clear(); 459 lcd.setCursor(0, 0); lcd.print(" Comp Drain Man:"); 460 lcd.setCursor(0, 1); lcd.print(" UP to turn On/Off"); 461 if (CompDrainManual == HIGH){ lcd.setCursor(0, 3); lcd.print("Comp Drain, On");} 462 if (CompDrainManual == LOW){ lcd.setCursor(0, 3); lcd.print("Comp Drain, Off");} 463 delay(100); 464} 465 466void DisplayCompMan() // this menu is for manually turning on and off the compressor relay and unload valve relay 467{ 468 469// this section reads the +/- buttons and adjusts the raw high alarm setpoint 470 if(digitalRead(P2) == HIGH) 471 { 472 if (CompManual == 1){CompManual = 0;} // 1 is the max, sets to 0 473 else {CompManual = CompManual + 1;} // increase by 1 per button press 474 } 475 476 if(digitalRead(P3) == HIGH) 477 { 478 if (CompManual == 0){CompManual = 1;} // 0 is the min, sets to 1 479 else {CompManual = CompManual - 1;} // decrease by 1 per button press 480 } 481 digitalWrite(CompRun, CompManual); // turns on comp run relay if Compmanual = 1 482 digitalWrite(CompUnloadValve, !CompManual); // turns off comp unload relay if Compmanual != 1 483 484// this is the lcd section 485 486 lcd.clear(); 487 lcd.setCursor(0, 0); lcd.print(" Compressor Man:"); 488 lcd.setCursor(0, 1); lcd.print(" UP to turn On/Off"); 489 if (CompManual == HIGH){ lcd.setCursor(0, 2); lcd.print("Comp On, Unload Off");} 490 if (CompManual == LOW){ lcd.setCursor(0, 2); lcd.print("Comp Off, Unload On");} 491 lcd.setCursor(0, 3); lcd.print("Dont forget to exit!"); 492 delay(100); 493} 494 495// this section stores the setpoints into the arduino eeprom so the unit can start up after a power fail 496void StoreAll() 497{ 498 499 lcd.clear(); 500 EEPROM.put(eeAddress1, Start); // saves the start psi setpoint to eeprom if different from before 501 EEPROM.put(eeAddress2, Stop); // saves the stop psi setpoint to eeprom if different from before 502 EEPROM.put(eeAddress3, CompPSIAlarmL); // saves the raw low alarm setpoint to eeprom if different from before 503 EEPROM.put(eeAddress4, CompPSIAlarmH); // saves the raw high alarm setpoint to eeprom if different from before 504 EEPROM.put(eeAddress5, CompDrainOn); // retrieves the drain on time from eeprom 505 EEPROM.put(eeAddress6, CompDrainOff); // retrieves the drain off time from eeprom 506 507// this is the lcd section 508 lcd.setCursor(0,0); lcd.print("SAVING IN"); 509 lcd.setCursor(0,1); lcd.print("PROGRESS"); 510 delay(400); 511} 512
The Controller code
arduino
1/* 2 WWTP_Compressor_Controller_rev24 3 RedTar4 4 8/20/19-8/29/19 5 6 7 Analog Resolution is 0-1023 (204.8 per volt or 0.004883v per resolution) 8 9 Pressure sensor is linear: 10 0 psi = 0.5v = 102.4 11 150 psi = 4.5v 12 = 921.6 13*/ 14 15// this is the libraries section 16#include <Wire.h> // 17 Wire library 18#include <LiquidCrystal_I2C.h> // Liquid Crystal I2C library 19 20 LiquidCrystal_I2C lcd(0x27,20,4); // Display address 0x27, I2C 20 x 4 21#include 22 <EEPROM.h> // EEPROM library 23#include <Bounce2.h> // 24 Bounce2 library 25 Bounce debouncer = Bounce(); // instantiate a bounce object 26 27// 28 this is the digital input button variables section 29int P1 = 2; // button SET 30 MENU' // this sets the tag as a discreate input to pin 2 31int P2 = 3; // button 32 + // this sets the tag as a discreate input to pin 3 33int P3 = 4; 34 // button - // this sets the tag as a discreate input to pin 4 35 36// 37 this is the menu section 38int menu = 0; // this sets the 39 variable tag menu to 0 40 41// this is the analog input variable section 42int 43 CompPSIRaw = A0; // this sets the tag as an analog input to pin A0 44 45// 46 this is the digital output variables section 47int CompAlarm = 9; // 48 this sets the tag as a discrete output to pin 12 49int CompRun = 10; // 50 this sets the tag as a discrete output to pin 10 51int CompUnloadValve = 11; // 52 this sets the tag as a discrete output to pin 11 53int CompDrain = 12; // 54 this sets the tag as a discrete output to pin 12 55 56// this is the variables 57 section 58int Start = LOW; // this sets the variable tag that 59 stores the start psi set point tolow 60int Stop = LOW; // this 61 sets the variable tag that stores the stop psi set point to low 62int CompRunBit 63 = LOW; // this sets the variable tag CompRunBit to low 64int CompUnloadValveBit 65 = LOW; // this sets the variable tag CompUnloadValveBit to low 66int CompDrainBit 67 = LOW; 68int CompDrainOn = LOW; // this sets the variable tag CompDrainOn 69 bit 70int CompDrainOff = LOW; // this sets the variable tag CompDrainOff 71 bit 72int CompDrainManual = LOW; // this sets the variable tag CompDrainManual 73 to low 74int CompDrainOffCalc = 0; 75int CompAlarmBitL = LOW; // this 76 sets the variable tag CompAlarmBitL to low 77int CompAlarmBitH = LOW; // 78 this sets the variable tag CompAlarmBitH to low 79int CompManual = LOW; // 80 this sets the variable tag manual to 0 81int CompPSIValue = 0; // 82 this sets the tag CompPSIAlarmL, that will store the value coming from the sensor 83int 84 CompPSIAlarmL = LOW; // this sets the tag CompPSIAlarmL, that will store 85 the value for the raw alarm low setpoint 86int CompPSIAlarmH = LOW; // 87 this sets the tag CompPSIAlarmH, that will store the value for the raw alarm high 88 setpoint 89 90// this is the EEPROM section 91int eeAddress1 = 10; // 92 this sets the eeAddress1 tag and sets the address to 10, eeprom 93int eeAddress2 94 = 20; // this sets the eeAddress2 tag and sets the address to 20, 95 eeprom 96int eeAddress3 = 30; // this sets the eeAddress3 tag and 97 sets the address to 30, eeprom 98int eeAddress4 = 40; // this sets 99 the eeAddress4 tag and sets the address to 40, eeprom 100int eeAddress5 = 50; // 101 this sets the eeAddress5 tag and sets the address to 50, eeprom 102int eeAddress6 103 = 60; // this sets the eeAddress6 tag and sets the address to 60, 104 eeprom 105 106// this is the millis section, the "Clock" 107long previousMillis 108 = 0; // this sets the tag "previousMillis", that will store the last 109 time "delay" that was updated 110long interval = 30000; // this sets 111 the tag "interval" for the menu(9) return timer 112long intervalSec = 500; // 113 this sets the tag "intervalSec", at which to blink (milliseconds), 500 = 1 second, 114 30000 = 1 minute 115int ledStateSec = 0; // this sets the tag "ledStateSec", 116 117 118 119// this is the Debounce section 120int buttonState; // 121 this set the tag "buttonState", to the current button state from the input pin 122int 123 lastButtonState = LOW; // this set the tag "lastButtonState", to the 124 previous button state from the input pin 125unsigned long lastDebounceTime = 0; 126 // the last time the output pin was toggled 127unsigned long debounceDelay = 100; 128 // the debounce time; increase if the output flickers 129 130void setup() 131{ 132 133// 134 this section assigns the inputs and outputs 135 // inputs 136 pinMode(P1,INPUT_PULLUP); 137 // button "MENU" 138 debouncer.attach(P1); // 139 Attach the debouncer to P1 with INPUT_PULLUP mode 140 debouncer.interval(debounceDelay); 141 // interval in ms 142 pinMode(P2,INPUT_PULLUP); // button "+" 143 advance 144 debouncer.attach(P2); // Attach the debouncer to P2 145 with INPUT_PULLUP mode 146 debouncer.interval(debounceDelay); // interval in 147 ms 148 pinMode(P3,INPUT_PULLUP); // button "-" decrease 149 debouncer.attach(P3); 150 // Attach the debouncer to P3 with INPUT_PULLUP mode 151 debouncer.interval(debounceDelay); 152 // interval in ms 153 // outputs 154 pinMode(CompAlarm, OUTPUT); // 155 comperssor alarm relay 156 digitalWrite(CompAlarm, LOW); // initially 157 sets output tag "CompAlarm" low 158 pinMode(CompRun, OUTPUT); // 159 comperssor run relay 160 digitalWrite(CompRun, LOW); // initially sets 161 output tag "CompRun" low 162 pinMode(CompUnloadValve, OUTPUT); // comperssor 163 unloader valve relay 164 digitalWrite(CompUnloadValve, LOW); // initially sets 165 output tag "CompUnloadValve" low 166 pinMode(CompDrain, OUTPUT); // 167 comperssor drain valve relay 168 digitalWrite(CompDrain, LOW); // initially 169 sets output tag "CompDrain" low 170 171// this section retrieves the setpoints 172 from the arduino eeprom so the unit can start up after a power fail 173 EEPROM.get(eeAddress1, 174 Start); // retrieves the start psi from eeprom 175 EEPROM.get(eeAddress2, 176 Stop); // retrieves the stop psi from eeprom 177 EEPROM.get(eeAddress3, 178 CompPSIAlarmL); // retrieves the lo alarm from eeprom 179 EEPROM.get(eeAddress4, 180 CompPSIAlarmH); // retrieves the hi alarm from eeprom 181 EEPROM.get(eeAddress5, 182 CompDrainOn); // retrieves the hi alarm from eeprom 183 EEPROM.get(eeAddress6, 184 CompDrainOff); // retrieves the hi alarm from eeprom 185 186// this section starts 187 up the different libraries 188 Serial.begin(9600); // start the serial monitor 189 at 9600 baud 190 lcd.init(); // start the lcd library 191 lcd.backlight(); 192 // turn on the lcd backlight 193 lcd.clear(); // clear the cld screen 194 195 Wire.begin(); // start the I2C library 196} 197 198void loop() 199{ 200 201// 202 Update the Bounce instance : 203 debouncer.update(); 204 205// this is the menu 206 selection section 207 if(digitalRead(P1)){menu=menu+1;} // press the 208 MENU(P1)button and advance through the menu index by 1 209 debouncer.attach(P1); 210 // Attach the debouncer to P1 with INPUT_PULLUP mode 211 212 debouncer.interval(debounceDelay); // intervalMin in ms 213 if(menu==0){DisplayPSI();} 214 // go to the DisplayPSI subroutine 215 if(menu==1){DisplayStart();} 216 // go to the DisplayStart subroutine 217 if(menu==2){DisplayStop();} 218 // go to the DisplayStop subroutine 219 if(menu==3){DisplayAlarmLow();} 220 // go to the DisplayAlarmLow subroutine 221 if(menu==4){DisplayAlarmHigh();} 222 // go to the DisplayAlarmHigh subroutine 223 if(menu==5){DisplayCompDrainOn();} 224 // go to the DisplayCompDrainOn subroutine 225 if(menu==6){DisplayCompDrainOff();} 226 // go to the DisplayCompDrainOff subroutine 227 if(menu==7){DisplayCompDrainManual();} 228 // go to the DisplayCompDrainManual subroutine 229 if(menu==8){DisplayCompMan();} 230 // go to the DisplayAutoMan subroutine 231 if(menu==9){StoreAll(); 232 delay(500); menu=0;} // go to the StoreAll subroutine and set the menu tag to 0 233 234 delay(100); 235} 236 237// this section declares the minutecount variable and 238 sets its initial values 239volatile unsigned long secondCount = 0; // use 240 volatile for shared variables 241 242void DisplayPSI () // main display 243{ 244 245// 246 this section keeps track of the millis counts (this is the "clock!") 247 // millis 248 is running in the background so any program delays wont effect the timing 249 // 250 only copies are used so accuracy is mantained 251unsigned long currentMillis = millis(); 252 // updates and holds a copy of the SecondMillis 253 if (currentMillis - previousMillis 254 >= intervalSec) 255 { 256 previousMillis = currentMillis; // updates and holds 257 a copy of the previousMillis 258 if (ledStateSec == LOW ){ledStateSec = HIGH; 259 secondCount = secondCount + 1;} 260 else {ledStateSec = LOW;} 261 } 262unsigned 263 long secondCopy; 264 secondCopy = secondCount; //updates and holds a copy of the 265 secondCount 266 267// this section monitors the live psi and turns the compressor 268 run bit on or off based off setpoints 269 int psi = analogRead(0); // this reads 270 the analog input(A0) and scales it 271 psi = map(psi, 102, 921, 0, 150); // 272 this maps the raw analog input value to the converted PSI value 273 if (psi <= 274 Start){CompRunBit = HIGH;} // if psi is less than start setpoint turn on 275 run bit 276 if (psi >= Stop ){CompRunBit = LOW;} // if psi is greater 277 than stop setpoint turn off run bit 278 if (psi <= Start){CompUnloadValveBit = 279 LOW;} // if psi is less than start setpoint turn off unload bit 280 if (psi >= 281 Stop ){CompUnloadValveBit = HIGH;} // if psi is greater than stop setpoint turn 282 on unload bit 283 284// this section monitors the raw analog input for errors 285 286 CompPSIValue = analogRead(CompPSIRaw); 287 if (CompPSIValue < CompPSIAlarmL){CompAlarmBitL 288 = HIGH;} // if comppsivalue is less than the CompPSIAlarmL bit, set alarm low bit 289 high 290 if (CompPSIValue > CompPSIAlarmL){CompAlarmBitL = LOW;} // if comppsivalue 291 is greater than the CompPSIAlarmL bit, set alarm low bit low 292 if (CompPSIValue 293 < CompPSIAlarmH){CompAlarmBitH = LOW;} // if comppsivalue is less than the CompAlarmBitH 294 bit, set alarm high bit low 295 if (CompPSIValue > CompPSIAlarmH){CompAlarmBitH 296 = HIGH;} // if comppsivalue is greater than the CompAlarmBitH bit, set alarm high 297 bit high 298 299// this section controls the drain timing 300 if (CompDrainOn >= 301 secondCount) {CompDrainBit = HIGH;} 302 else {CompDrainBit = LOW;} 303int CompDrainOffCalc 304 = CompDrainOff * 60; 305 if (CompDrainOffCalc <= secondCount + 1) {secondCount 306 = 0;} 307 308// this section turns on or off the digital outputs based on the control 309 and alarm bits 310 digitalWrite(CompRun, CompRunBit && !CompAlarmBitL && !CompAlarmBitH); 311 // comp run safety interlock if sensor fails 312 digitalWrite(CompUnloadValve, 313 CompUnloadValveBit || CompAlarmBitL || CompAlarmBitH); // comp unload valve safety 314 interlock if sensor fails 315 digitalWrite(CompAlarm, CompAlarmBitL || CompAlarmBitH); 316 // comp alarm if sensor fails 317 digitalWrite(CompDrain, 318 CompDrainBit); // comp drain valve 319 320// 321 this is the serial monitor setion for testing and debug. remove the "//" to print 322 value to the serial monitor 323// Serial.print(" Sensor raw value "); 324// Serial.print(CompPSIValue); 325// 326 Serial.print(" Sensor psi value "); 327// Serial.print(psi); 328// Serial.print(", 329 Start @ "); 330// Serial.print(Start); 331// Serial.print(", Stop @ "); 332// 333 Serial.print(Stop); 334// Serial.print(","); 335// Serial.print(" Low alarm 336 "); 337// Serial.print(CompPSIAlarmL); 338// Serial.print(","); 339// Serial.print(" 340 High alarm "); 341// Serial.print(CompPSIAlarmH); 342// Serial.print(", CRB 343 "); 344// Serial.print(CompRunBit); 345// Serial.print(", CUL "); 346// Serial.print(CompUnloadValveBit); 347// 348 Serial.print(", CAL "); 349// Serial.print(CompAlarmBitL); 350// Serial.print(", 351 CAH "); 352// Serial.print(CompAlarmBitH); 353// Serial.print(" CompDrain "); 354// 355 Serial.print(CompDrain); 356// Serial.print(" Comp On "); 357// Serial.print(CompDrainOn); 358// 359 Serial.print(" Comp Off "); 360// Serial.print(CompDrainOff); 361// Serial.print(" 362 Comp Off calc "); 363// Serial.print(CompDrainOffCalc); 364// Serial.print(" 365 ledState sec "); 366// Serial.print(ledStateSec); 367// Serial.print(" intervalSec 368 "); 369// Serial.print(intervalSec); 370// Serial.print(", Seconds count "); 371// 372 Serial.print(secondCount); 373// Serial.print(", Seconds copy "); 374// Serial.print(secondCopy); 375// 376 Serial.print(", Previous Millis "); 377 Serial.println(previousMillis); 378 379// 380 this is the lcd section 381 // line 1 382 lcd.setCursor(0, 0); lcd.print("Currently 383 @ "); // this prints whats in between the quotes 384 lcd.setCursor(13, 0); 385 lcd.print(" "); // this clears the display field so anything left 386 is deleted 387 lcd.setCursor(13, 0); lcd.print(psi); // this prints 388 the tag value 389 lcd.setCursor(17, 0); lcd.print("PSI"); // this 390 prints whats in between the quotes 391 // line 2 392 lcd.setCursor(0, 1); lcd.print(" 393 Start @ "); // this prints whats in between the quotes 394 lcd.setCursor(13, 395 1); lcd.print(" "); // this clears the display field so anything 396 left is deleted 397 lcd.setCursor(13, 1); lcd.print(Start); // this 398 prints the tag value 399 lcd.setCursor(17, 1); lcd.print("PSI"); // 400 this prints whats in between the quotes 401 // line 3 402 lcd.setCursor(0, 2); 403 lcd.print(" Stop @ "); // this prints whats in between the quotes 404 lcd.setCursor(13, 405 2); lcd.print(" "); // this clears the display field so anything 406 left is deleted 407 lcd.setCursor(13, 2); lcd.print(Stop); // this 408 prints the tag value 409 lcd.setCursor(17, 2); lcd.print("PSI"); // 410 this prints whats in between the quotes 411 // line 4 412 // this section displays 413 the alarm/run status on lcd line 4 414 if (CompAlarmBitL && CompAlarmBitH == LOW); 415 // if CompAlarmBitL and CompAlarmBitH alarm bits are low then its true and 416 prints whats below 417 { 418 lcd.setCursor(0, 3); lcd.print(" "); // 419 this clears the display field so anything left is deleted 420 lcd.setCursor(0, 421 3); lcd.print("Sensor OK "); // this prints whats in between the quotes 422 423 lcd.setCursor(11, 3); lcd.print(" Raw "); // this prints whats in between 424 the quotes 425 lcd.setCursor(16, 3); lcd.print(" "); // this clears 426 the display field so anything left is deleted 427 lcd.setCursor(16, 3); lcd.print(CompPSIValue); 428 // this prints the tag value 429 } 430 if (CompAlarmBitL == HIGH){lcd.setCursor(0, 431 3); lcd.print("ALARM: "); lcd.setCursor(7, 3); lcd.print("Xmtr Open ");} 432 // if the CompAlarmBitL alarm bit is high then its in alarm and prints text 433 434 if (CompAlarmBitH == HIGH){lcd.setCursor(0, 3); lcd.print("ALARM: "); lcd.setCursor(7, 435 3); lcd.print("Xmtr Short");} // if the CompAlarmBitH alarm bit is high then 436 its in alarm and prints text 437 438} 439 440void DisplayStart() // this menu 441 is for adjusting the start psi setpoint 442{ 443 444// this section advances to 445 menu 9 after 30 sec's, so the program doesn't stall if left on this menu selection 446 447 unsigned long currentMillis = millis(); 448 if(currentMillis - previousMillis 449 > interval) 450 {previousMillis = currentMillis; menu = 9;} 451 452// this section 453 reads the +/- buttons and adjusts the start value 454 if(digitalRead(P2)== HIGH) 455 456 { 457 if (Start == 150){Start = 0;} // 150 is the max, sets back to 0 458 459 else {Start = Start + 1;} // incremnent by 1 per button press 460 } 461 462 463 if(digitalRead(P3) == HIGH) 464 { 465 if (Start == 0){Start = 150;} // 466 0 is the min, sets back to 150 467 else {Start = Start - 1;} // decrease 468 by 1 per button press 469 } 470 471// this is the lcd section 472 lcd.clear(); 473 474 lcd.setCursor(0, 0); lcd.print(" Start PSI Menu:"); 475 lcd.setCursor(0, 476 1); lcd.print(" Set Start PSI to"); 477 lcd.setCursor(9, 3); lcd.print(Start,DEC); 478 479 delay(100); 480} 481 482void DisplayStop() // this menu is for adjusting the 483 stop psi setpoint 484{ 485 486// this section advances to menu 9 after 30 sec's, 487 so the program doesn't stall if left on this menu selection 488unsigned long currentMillis 489 = millis(); 490 if(currentMillis - previousMillis > interval) 491 {previousMillis 492 = currentMillis; menu = 9;} 493 494// this section reads the +/- buttons and adjusts 495 the stop value 496 if(digitalRead(P2) == HIGH) 497 { 498 if (Stop == 150){Stop 499 = 0;} // 150 is the max, sets back to 0 500 else {Stop = Stop + 1;} // 501 incremnent by 1 per button press 502 } 503 504 if(digitalRead(P3) == HIGH) 505 506 { 507 if (Stop == 0){Stop = 150;} // 0 is the min, sets back to 150 508 509 else {Stop = Stop - 1;} // decrease by 1 per button press 510 } 511 512// 513 this is the lcd section 514 lcd.clear(); 515 lcd.setCursor(0, 0); lcd.print(" 516 Stop PSI Menu:"); 517 lcd.setCursor(0, 1); lcd.print(" Set Stop PSI to"); 518 519 lcd.setCursor(9, 3); lcd.print(Stop,DEC); 520 delay(100); 521} 522 523void 524 DisplayAlarmLow() // this menu is for adjusting the raw alarm low setpoint 525{ 526 527// 528 this section advances to menu 9 after 30 sec's, so the program doesn't stall if 529 left on this menu selection 530unsigned long currentMillis = millis(); 531 if(currentMillis 532 - previousMillis > interval) 533 {previousMillis = currentMillis; menu = 9;} 534 535// 536 this section reads the +/- buttons and adjusts the raw low alarm setpoint 537 if(digitalRead(P2) 538 == HIGH) 539 { 540 if (CompPSIAlarmL == 1023){CompPSIAlarmL = 0;} // 1023 541 is the max, sets to 0 542 else {CompPSIAlarmL = CompPSIAlarmL + 1;} // increase 543 by 1 per button press 544 } 545 546 if(digitalRead(P3) == HIGH) 547 { 548 549 if (CompPSIAlarmL == 0){CompPSIAlarmL = 1023;} // 0 is the min, sets to 1023 550 551 else {CompPSIAlarmL = CompPSIAlarmL - 1;} // decrease by 1 per button press 552 553 } 554 555// this is the lcd section 556 lcd.clear(); 557 lcd.setCursor(0, 558 0); lcd.print(" Raw Alarm Lo Menu:"); 559 lcd.setCursor(0, 1); lcd.print(" Set 560 Lo Alarm to"); 561 lcd.setCursor(9, 3); lcd.print(CompPSIAlarmL,DEC); 562 delay(100); 563} 564 565void 566 DisplayAlarmHigh() // this menu is for adjusting the raw alarm high setpoint 567{ 568 569// 570 this section advances to menu 9 after 30 sec's, so the program doesn't stall if 571 left on this menu selection 572unsigned long currentMillis = millis(); 573 if(currentMillis 574 - previousMillis > interval) 575 {previousMillis = currentMillis; menu = 9;} 576 577// 578 this section reads the +/- buttons and adjusts the raw high alarm setpoint 579 if(digitalRead(P2) 580 == HIGH) 581 { 582 if (CompPSIAlarmH == 1023){CompPSIAlarmH = 0;} // 1023 583 is the max, sets to 0 584 else {CompPSIAlarmH = CompPSIAlarmH + 1;} // increase 585 by 1 per button press 586 } 587 588 if(digitalRead(P3) == HIGH) 589 { 590 591 if (CompPSIAlarmH == 0){CompPSIAlarmH = 1023;} // 0 is the min, sets to 1023 592 593 else {CompPSIAlarmH = CompPSIAlarmH - 1;} // decrease by 1 per button press 594 595 } 596 597// this is the lcd section 598 lcd.clear(); 599 lcd.setCursor(0, 600 0); lcd.print(" Raw Alarm Hi Menu:"); 601 lcd.setCursor(0, 1); lcd.print(" Set 602 Hi Alarm to"); 603 lcd.setCursor(9, 3); lcd.print(CompPSIAlarmH,DEC); 604 delay(100); 605} 606 607void 608 DisplayCompDrainOn() // this menu is for adjusting the drain on time setpoint 609{ 610 611// 612 this section advances to menu 9 after 30 sec's, so the program doesn't stall if 613 left on this menu selection 614unsigned long currentMillis = millis(); 615 if(currentMillis 616 - previousMillis > interval) 617 {previousMillis = currentMillis; menu = 9;} 618 619// 620 this section reads the +/- buttons and adjusts the drain on/off setpoint 621 if(digitalRead(P2) 622 == HIGH) 623 { 624 if (CompDrainOn == 10){CompDrainOn = 0;} // 10 is the max, 625 sets to 0 626 else {CompDrainOn = CompDrainOn + 1;} // increase by 1 per button 627 press 628 } 629 630 if(digitalRead(P3) == HIGH) 631 { 632 if (CompDrainOn 633 == 0){CompDrainOn = 10;} // 0 is the min, sets to 10 634 else {CompDrainOn = 635 CompDrainOn - 1;} // decrease by 1 per button press 636 } 637 638// this is 639 the lcd section 640 lcd.clear(); 641 lcd.setCursor(0, 0); lcd.print(" Drain 642 On: "); 643 lcd.setCursor(0, 1); lcd.print("Set Drain on in sec"); 644 lcd.setCursor(9, 645 3); lcd.print(CompDrainOn,DEC); 646 647 648 delay(100); 649} 650 651void DisplayCompDrainOff() 652 // this menu is for adjusting the drain off time setpoint 653{ 654 655// this section 656 advances to menu 9 after 30 sec's, so the program doesn't stall if left on this 657 menu selection 658unsigned long currentMillis = millis(); 659 if(currentMillis 660 - previousMillis > interval) 661 {previousMillis = currentMillis; menu = 9;} 662 663// 664 this section reads the +/- buttons and adjusts the drain on/off setpoint 665 if(digitalRead(P2) 666 == HIGH) 667 { 668 if (CompDrainOff == 45){CompDrainOff = 0;} // 45 is the 669 max, sets to 0 670 else {CompDrainOff = CompDrainOff + 1;} // increase by 671 1 per button press 672 } 673 674 if(digitalRead(P3) == HIGH) 675 { 676 if 677 (CompDrainOff == 0){CompDrainOff = 45;} // 0 is the min, sets to 45 678 else 679 {CompDrainOff = CompDrainOff - 1;} // decrease by 1 per button press 680 } 681 682// 683 this is the lcd section 684 lcd.clear(); 685 lcd.setCursor(0, 0); lcd.print(" 686 Drain off:"); 687 lcd.setCursor(0, 1); lcd.print("Set Drain Off in min"); 688 689 lcd.setCursor(9, 3); lcd.print(CompDrainOff,DEC); 690 691 delay(100); 692} 693 694void 695 DisplayCompDrainManual() // this menu is for manually turning the drain on/off 696{ 697 698// 699 this section advances to menu 9 after 30 sec's, so the program doesn't stall if 700 left on this menu selection 701unsigned long currentMillis = millis(); 702 if(currentMillis 703 - previousMillis > interval) 704 {previousMillis = currentMillis; menu = 9;} 705 706// 707 this section reads the +/- buttons and adjusts the raw high alarm setpoint 708 if(digitalRead(P2) 709 == HIGH) 710 { 711 if (CompDrainManual == 1){CompDrainManual = 0;} // 712 1 is the max, sets to 0 713 else {CompDrainManual = CompDrainManual + 1;} // 714 increase by 1 per button press 715 } 716 717 if(digitalRead(P3) == HIGH) 718 719 { 720 if (CompDrainManual == 0){CompDrainManual = 1;} // 0 is the 721 min, sets to 1 722 else {CompDrainManual = CompDrainManual - 1;} // 723 decrease by 1 per button press 724 } 725 digitalWrite(CompDrain, CompDrainManual); 726 // turns on/off compDrain relay based on CompDrainManual 727 728// this is the lcd 729 section 730 731 lcd.clear(); 732 lcd.setCursor(0, 0); lcd.print(" Comp Drain 733 Man:"); 734 lcd.setCursor(0, 1); lcd.print(" UP to turn On/Off"); 735 if (CompDrainManual 736 == HIGH){ lcd.setCursor(0, 3); lcd.print("Comp Drain, On");} 737 if (CompDrainManual 738 == LOW){ lcd.setCursor(0, 3); lcd.print("Comp Drain, Off");} 739 delay(100); 740} 741 742void 743 DisplayCompMan() // this menu is for manually turning on and off the compressor 744 relay and unload valve relay 745{ 746 747// this section reads the +/- buttons and 748 adjusts the raw high alarm setpoint 749 if(digitalRead(P2) == HIGH) 750 { 751 752 if (CompManual == 1){CompManual = 0;} // 1 is the max, sets to 0 753 else 754 {CompManual = CompManual + 1;} // increase by 1 per button press 755 } 756 757 758 if(digitalRead(P3) == HIGH) 759 { 760 if (CompManual == 0){CompManual = 761 1;} // 0 is the min, sets to 1 762 else {CompManual = CompManual - 1;} 763 // decrease by 1 per button press 764 } 765 digitalWrite(CompRun, 766 CompManual); // turns on comp run relay if Compmanual = 1 767 digitalWrite(CompUnloadValve, 768 !CompManual); // turns off comp unload relay if Compmanual != 1 769 770// this is 771 the lcd section 772 773 lcd.clear(); 774 lcd.setCursor(0, 0); lcd.print(" Compressor 775 Man:"); 776 lcd.setCursor(0, 1); lcd.print(" UP to turn On/Off"); 777 if (CompManual 778 == HIGH){ lcd.setCursor(0, 2); lcd.print("Comp On, Unload Off");} 779 if (CompManual 780 == LOW){ lcd.setCursor(0, 2); lcd.print("Comp Off, Unload On");} 781 lcd.setCursor(0, 782 3); lcd.print("Dont forget to exit!"); 783 delay(100); 784} 785 786// this 787 section stores the setpoints into the arduino eeprom so the unit can start up after 788 a power fail 789void StoreAll() 790{ 791 792 lcd.clear(); 793 EEPROM.put(eeAddress1, 794 Start); // saves the start psi setpoint to eeprom if different from before 795 796 EEPROM.put(eeAddress2, Stop); // saves the stop psi setpoint to eeprom 797 if different from before 798 EEPROM.put(eeAddress3, CompPSIAlarmL); // saves the 799 raw low alarm setpoint to eeprom if different from before 800 EEPROM.put(eeAddress4, 801 CompPSIAlarmH); // saves the raw high alarm setpoint to eeprom if different from 802 before 803 EEPROM.put(eeAddress5, CompDrainOn); // retrieves the drain on time 804 from eeprom 805 EEPROM.put(eeAddress6, CompDrainOff); // retrieves the drain 806 off time from eeprom 807 808// this is the lcd section 809 lcd.setCursor(0,0); 810 lcd.print("SAVING IN"); 811 lcd.setCursor(0,1); lcd.print("PROGRESS"); 812 813 delay(400); 814} 815
The Circuit
arduino
This is the wire circuit thats saved with the code.
1/* 2 3 The circuit: 4 5 Arduino uno r3 6 5v to VCC 7 gnd to GND 8 9 20X4 I2C LCD display: 10 SCL: to A5 on arduino 11 SDA: to A4 on arduino 12 VCC: to VCC 13 GND: to GND 14 15 push buttons: X 4 16 button lead 1 to VCC 17 button lead 2 to Arduino pins 2,3,4,5 18 button lead 2 to lead 1 of 10k ohm resistor 19 lead 2 of 10k ohm resistor to GND 20 21 LED / Relays: 22 long +LED leads to Arduino digital pins 10,11,12,13 23 short -LED leads to 10K ohm resistors, resistors to ground 24 if using relays: 25 relay coil pin 1 to arduino pin 10,11,12,13 26 relay coil pin 2 to ground 27 28 Also be sure to use plastic stand off for all PC boards for isolation and noise dampening 29 After download start the serial monitor 30 */ 31
Downloadable files
Schematic
Schematic
Schematic
Schematic
Comments
Only logged in users can leave comments