#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// 'TCPC logo (1)', 128x64px
const unsigned char epd_bitmap_TCPC_logo__1_ [] PROGMEM = {
// Bitmap data (truncated for brevity)
};
const int sumpPumpRelayPin = 2;
const int tankFloatSwitchInputPin = 3;
const int phLowInputPin = 4;
const int phHighInputPin = 5;
const int phUpPin = 6;
const int phDownPin = 7;
const int stirPin = 10;
const int drainPumpRelayPin = 11;
// Cycle count variable
unsigned long cycleCount = 0;
void setup() {
Serial.begin(115200);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.drawBitmap(0, 0, epd_bitmap_TCPC_logo__1_, 128, 64, WHITE);
display.display();
delay(3000); // Display logo for 3 seconds
pinMode(sumpPumpRelayPin, OUTPUT);
pinMode(tankFloatSwitchInputPin, INPUT);
pinMode(phLowInputPin, INPUT);
pinMode(phHighInputPin, INPUT);
pinMode(phUpPin, OUTPUT);
pinMode(phDownPin, OUTPUT);
pinMode(stirPin, OUTPUT);
pinMode(drainPumpRelayPin, OUTPUT);
}
void loop() {
// Increment cycle count at the start of each loop iteration
cycleCount++;
// Tank fill sequence
if (digitalRead(tankFloatSwitchInputPin) == LOW) {
digitalWrite(sumpPumpRelayPin, HIGH);
displayProcessAndCountdown("Filling Tank...", -1); // No countdown needed
while (digitalRead(tankFloatSwitchInputPin) == LOW);
digitalWrite(sumpPumpRelayPin, LOW);
}
// pH read sequence
while (true) {
if (digitalRead(phLowInputPin) == HIGH) {
digitalWrite(phUpPin, HIGH);
displayProcessAndCountdown("Adjusting pH Up...", 10); // 10 seconds countdown for pH adjustment
digitalWrite(phUpPin, LOW);
}
if (digitalRead(phHighInputPin) == HIGH) {
digitalWrite(phDownPin, HIGH);
displayProcessAndCountdown("Adjusting pH Down...", 10); // 10 seconds countdown for pH adjustment
digitalWrite(phDownPin, LOW);
}
if (digitalRead(tankFloatSwitchInputPin) == HIGH) {
digitalWrite(stirPin, HIGH);
displayProcessAndCountdown("Stirring...", 10); // 10 seconds countdown for stirring
digitalWrite(stirPin, LOW);
}
if (digitalRead(phLowInputPin) == LOW && digitalRead(phHighInputPin) == LOW) break; // Exit loop if pH is within desired range
}
displayProcessAndCountdown("Settling Tank...", 10); // 30 minutes countdown for settling
digitalWrite(drainPumpRelayPin, HIGH);
displayProcessAndCountdown("Draining Tank...", 10); // Time to allow draining. Float switch will stop pump until timer runs out.
while (digitalRead(tankFloatSwitchInputPin) == HIGH);
digitalWrite(drainPumpRelayPin, LOW);
}
void displayProcessAndCountdown(const char* process, int seconds) {
if (seconds >= 0) {
for (int count = seconds; count >= 0; count--) {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println(process);
display.setCursor(0, 10);
display.print("Time left: ");
display.print(count);
display.println(" sec");
// Display the cycle count
display.setCursor(0, 20);
display.print("Cycle: ");
display.println(cycleCount);
display.display();
delay(1000);
}
} else {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println(process);
// Display the cycle count even if no countdown is needed
display.setCursor(0, 10);
display.print("Cycle: ");
display.println(cycleCount);
display.display();
delay(2000); // Just display the process for 2 seconds if no countdown
}
}