Hey everyone, so over the past few weeks I've managed to build a pumping project, replace many different components for better more reliable ones....and make who knows how many changes to my program, while learning as much as I can.
This is my first Arduino project, so it's all been a bit of a learning experience for me...it's been very fun though.
I'm going to post my code in hopes to receive some critiques on how to make it more efficient, smarter ways to accomplish tasks, etc.
As of now the project works, so I'm happy....I'd just love to learn smarter ways to handle some of this stuff with my next project.
There are some items that aren't used that I never deleted....I was attempting to remove all delays for the millis method but was having issues with some of them, so there are some remnants from that.
I'm not positive if my method of level detection works currently, but I'll find that out very soon.
// Pin 8 = Recirc Flow
// Pin 7 = Normal Flow
// pin 6 = Pumps
// Pin 5 = Level LCD
// Pin 3 = Flow LCD
// Pin 2 = Flow Meter
// Analog 3 = Pressure
#include "Arduino.h"
#include <SoftwareSerial.h>
SoftwareSerial Serial1 = SoftwareSerial(0, 3);
SoftwareSerial Serial2 = SoftwareSerial(0, 5);
const long displayinterval = 10000;
const long newdisplaytime = 21000;
const long flowinterval = 60000;
const long onesecond = 1000;
const long interval = 18000000;
const long thirtysecond = 30000;
const long tensecond = 10000;
unsigned long displaystart;
unsigned long rfms;
unsigned long fms;
unsigned long cms = 0;
unsigned long newdisplay;
unsigned long pms = 0;
unsigned long pfms = 0;
unsigned long dpms = 0;
unsigned long ost = 0;
unsigned long tst = 0;
unsigned long tenst = 0;
int counter;
int totalFlow;
float flowRate;
volatile int NbTopsFan;
int Calc;
int PCalc;
int Level;
int PrevLevel;
int ChangeLevel;
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(3, OUTPUT);
pinMode(A3, INPUT);
pinMode(2, INPUT);
attachInterrupt(digitalPinToInterrupt(2), rpm, RISING);
rlcd1();
rlcd2();
Serial1.print("Prototype Pump");
Serial1.write(0xFE);
Serial1.write(0x80 + 64);
Serial1.print("Working Smarter");
Serial2.print("Prototype Pump");
Serial2.write(0xFE);
Serial2.write(0x80 + 64);
Serial2.print("Working Smarter");
}
void loop() {
cms = millis();
if (cms <= 1000) {
Serial.println("Initializing System");
digitalWrite(7, HIGH);
delay(7500);
digitalWrite(6, HIGH);
}
while (flowRate >= 0.25) {
cms = millis();
if (cms - tenst >= displayinterval) {
Serial.println(cms);
Serial.println("Flow Rate > 0.1");
Serial.println("Running Normal Flow Path");
tenst += displayinterval;
Serial.println(tenst);
}
if (cms - dpms >= newdisplaytime) {
Serial.println("Update Displays");
UpdateDisplays();
dpms += newdisplaytime;
Serial.println(dpms);
}
if (cms - pfms >= flowinterval) {
pfms += flowinterval;
Serial.println("Time to Read New Flows");
ReadFlow();
Serial.println(pfms);
}
if (cms - pms >= interval) {
Serial.println("Starting Level Calculation");
Serial.println("Putting Valves Into Recycle Path");
digitalWrite(8, HIGH);
digitalWrite(7, LOW);
delay(7500);
Serial.println("Wait 30 Seconds");
delay(30000);
Serial.println("Locking In Pressure");
digitalWrite(8, LOW);
delay(5500);
digitalWrite(6, LOW);
delay(2000);
Serial.println("Wait 10 Seconds For Pressure Stabilization");
delay(10000);
Serial.println("Calculate Level");
ReadLevel();
pms += interval;
Serial.println(pms);
}
}
while (flowRate < 0.25) {
Serial.println("There Is No Flow Detected");
Serial.println("Wait 10 Seconds To Verify Flow");
delay(10000);
Serial.println("Verify Flow");
ReadFlow();
Serial.println("There Is Still No Flow Detected");
for (counter = 0; counter < 10; ++counter) {
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
delay(7500);
digitalWrite(6, HIGH);
delay(10000); // Wait for 10 seconds
digitalWrite(7, HIGH);
digitalWrite(8, LOW);
delay(7500);
delay(30000); // Wait for 30 Seconds
ReadFlow();
if (flowRate >= 0.25) {
return;
} else {
delay(18000000);
}
}
}
}
void UpdateDisplays() {
rlcd1();
Serial1.print("Gal Per Minute");
Serial1.write(0xFE);
Serial1.write(0x80 + 64);
Serial1.print(flowRate);
Serial.println("Gal Per Minute");
Serial.println(flowRate);
rlcd2();
Serial2.print("Feet of Water");
Serial2.write(0xFE);
Serial2.write(0x80 + 64);
Serial2.print(Level);
Serial.println("Feet of Water");
Serial.println(Level);
delay(10000);
rlcd2();
Serial2.print("Level Change");
Serial2.write(0xFE);
Serial2.write(0x80 + 64);
Serial2.print(ChangeLevel);
Serial.println("Change In Level");
Serial.println(ChangeLevel);
rlcd1();
Serial1.print("Total Gallons");
Serial1.write(0xFE);
Serial1.write(0x80 + 64);
Serial1.print(totalFlow);
Serial.println("Total Gallons");
Serial.println(totalFlow);
}
void rpm () {
NbTopsFan++;
}
void ReadFlow() {
noInterrupts();
NbTopsFan = 0;
interrupts();
delay (1000);
noInterrupts();
flowRate = (NbTopsFan / 11 * 0.2642);
interrupts();
totalFlow += flowRate * 60;
DisplayFlow();
}
void DisplayFlow() {
Serial.println("Display Flow");
rlcd1();
Serial1.print("Gal Per Minute");
Serial1.write(0xFE);
Serial1.write(0x80 + 64);
Serial1.print(flowRate, DEC);
Serial.println("Gal Per Minute");
Serial.println(flowRate);
delay(10000);
rlcd1();
Serial1.print("Total Gallons");
Serial1.write(0xFE);
Serial1.write(0x80 + 64);
Serial1.print(totalFlow, DEC);
Serial.println("Total Gallons");
Serial.println(totalFlow);
}
void ReadLevel() {
PCalc = (((analogRead(A3) / 5) - 0.1) / 0.75) * 145.038;
Level = ((PCalc * 27.71) / 12);
Serial.println("Calculating Level");
Serial.println(Level);
ChangeLevel = Level - PrevLevel;
Serial.println("Calculating Change In Level");
Serial.println(ChangeLevel);
Level = PrevLevel;
Serial.println("Display Level");
DisplayLevel();
}
void DisplayLevel() {
rlcd2();
Serial2.print("Feet of Water");
Serial2.write(0xFE);
Serial2.write(0x80 + 64);
Serial2.print(Level);
Serial.println("Feet of Water");
Serial.println(Level);
delay(10000);
rlcd2();
Serial2.print("Level Change");
Serial2.write(0xFE);
Serial2.write(0x80 + 64);
Serial2.print(ChangeLevel);
Serial.println("Change In Level");
Serial.println(ChangeLevel);
}
void rlcd1() {
Serial1.write(0xFE);
delay(10);
Serial1.write(0x01);
delay(10);
}
void rlcd2() {
Serial2.write(0xFE);
delay(10);
Serial2.write(0x01);
delay(10);
}