#include <Servo.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int switchPin = 4; // Switch input pin
const int floatSwitchPin = 6; // Float switch input pin
const int potPin = A1; // Potentiometer analog pin
const int servoPin = 3; // Servo control pin
const int autoPin = 8;
const int overflowPin = 12;
bool overflow = false;
bool paused = false;
unsigned long buttonPressTime = 0; // Variable to store the time when the button is pressed
unsigned long buttonReleaseTime = 0; // Variable to store the time when the button is released
Servo servo;
void setup() {
pinMode(switchPin, INPUT_PULLUP);
pinMode(floatSwitchPin, INPUT_PULLUP);
pinMode(potPin, INPUT); // Potentiometer pin
pinMode(autoPin, INPUT_PULLUP);
pinMode(overflowPin, INPUT_PULLUP);
servo.attach(servoPin,500,2450);
servo.write(0);
Serial.begin(115200);
// Initialize the OLED display with Adafruit library
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(1000); // Pause for 2 seconds
while (digitalRead(switchPin)==HIGH){
startOLED();
}
Serial.println("button pressed");
while (digitalRead(switchPin) == LOW) {
}
Serial.println("button released");
Serial.println("passed start");
delay(50);
}
void loop() {
Serial.println(overflow);
Serial.println(paused);
if (digitalRead(overflowPin) == LOW){
//if (digitalRead(overflowPin)== LOW || (overflow)){
Serial.println("entered overflow state");
overflowed();
paused = false;
}else{
if (digitalRead(autoPin) == HIGH){
Serial.println("manual");
if (digitalRead(switchPin) == HIGH){
if (digitalRead(floatSwitchPin) == HIGH){
fullWaitOLED();
paused = false;
}else if (digitalRead(floatSwitchPin) == LOW && (paused)){
Serial.println("void paused");
pausedOLED();
}else {
fillOLED();
}
} else {
overridecheck();
}
}else if (digitalRead(autoPin) == LOW){
Serial.println("auto");
if (digitalRead(switchPin)==LOW){
// paused=false;
// buttonPressTime = millis();
// while(digitalRead(switchPin)==LOW){
// Serial.println("Waiting");
// }
// buttonReleaseTime = millis();
// unsigned long pressDuration = buttonReleaseTime - buttonPressTime;
//delay(50);
//if (pressDuration > 3000) {
//Serial.println("override");
//overridevalvecontrol();
//}else{
//}
overridecheck();
}
if(digitalRead(floatSwitchPin) == LOW && (paused)){
Serial.println("void paused");
pausedOLED();
}else if (digitalRead(floatSwitchPin) == LOW){
valvecontrol();
}else{
fullWaitOLED();
paused=false;
}
}
delay(100);
}
}
void overridecheck(){
paused=false;
buttonPressTime = millis();
while(digitalRead(switchPin)==LOW){
Serial.println("Waiting");
}
buttonReleaseTime = millis();
unsigned long pressDuration = buttonReleaseTime - buttonPressTime;
delay(50);
if (pressDuration > 3000){
Serial.println("override");
overridevalvecontrol();
}else{
valvecontrol();
}
}
void overflowed(){
while (digitalRead(switchPin)== HIGH){
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(4,5);
display.println("OVERFLOWED");
display.setCursor(16,25);
display.println("Press to");
display.setCursor(36,45);
display.println("Reset");
display.display();
}
while (digitalRead(switchPin) == LOW){
}
delay(100);
if (digitalRead(overflowPin) == HIGH){
Serial.println("overflow set false");
overflow = false;
}
}
void valvecontrol(){
if (digitalRead(floatSwitchPin) == LOW) {
while (digitalRead(floatSwitchPin) == LOW && digitalRead(overflowPin)== HIGH && digitalRead(switchPin)== HIGH) {
Serial.println(paused);
Serial.println("float low empty");
// Check for override with potentiometer
int potValue = map(analogRead(potPin), 0, 1023, 0, 90);
Serial.println(potValue);
servo.write(potValue);
if (potValue < 5)
updateOLED("Closed"); // Update OLED with "Open" if servo is at 0 degrees
else if (potValue > 85)
updateOLED(" Open"); // Update OLED with "Close" if servo is at 90 degrees
else if (potValue >= 43 && potValue <= 47)
updateOLED(" Mid"); // Update OLED with "Mid" if potValue is between 43 and 47
else
updateOLED(potValue); // Update OLED with actual servo angle
delay(50);
}
if (digitalRead(overflowPin)== LOW){
Serial.println("overflow set true");
overflow = true;
} else if (digitalRead(floatSwitchPin) == HIGH){
Serial.println("float full triggered");
fullWaitOLED();
} else if (digitalRead(switchPin)==LOW){
paused = true;
Serial.println("paused");
while (digitalRead(switchPin)==LOW){
}
delay(100);
}
}
delay(50); // Delay for stability
servo.write(0);
//fullWait();
}
void overridevalvecontrol(){
while (digitalRead(overflowPin)== HIGH && digitalRead(switchPin)== HIGH) {
Serial.println("float low empty");
// Check for override with potentiometer
int potValue = map(analogRead(potPin), 0, 1023, 0, 90);
Serial.println(potValue);
servo.write(potValue);
if (potValue < 5)
overrideupdateOLED("Closed"); // Update OLED with "Open" if servo is at 0 degrees
else if (potValue > 85)
overrideupdateOLED(" Open"); // Update OLED with "Close" if servo is at 90 degrees
else if (potValue >= 43 && potValue <= 47)
overrideupdateOLED(" Mid"); // Update OLED with "Mid" if potValue is between 43 and 47
else
overrideupdateOLED(potValue); // Update OLED with actual servo angle
delay(50);
}
if (digitalRead(overflowPin)== LOW){
Serial.println("overflow set true");
overflow = true;
} else if (digitalRead(switchPin)==LOW){
paused = true;
Serial.println("paused");
while (digitalRead(switchPin)==LOW){
}
delay(100);
}
delay(50); // Delay for stability
servo.write(0);
}
void startOLED(){
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(34,5);
display.println("Press");
display.setCursor(12,25);
display.println("Button to");
display.setCursor(5,45);
display.println("Initialize");
display.display();
}
void pausedOLED(){
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(30,5);
display.println("PAUSED");
display.setCursor(16,25);
display.println("Press To");
display.setCursor(25,45);
display.println("Restart");
display.display();
}
void fillOLED(){
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(9,5);
display.println("Water LOW");
display.setCursor(16,25);
display.println("Press To");
display.setCursor(40,45);
display.println("Fill");
display.display();
}
void fullWaitOLED() {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(6,5);
display.println("Water FULL");
display.setCursor(18,25);
display.println("Wait For");
display.setCursor(36,45);
display.println("Empty");
display.display();
}
void updateOLED(const char* position) {
display.clearDisplay();
display.setCursor(34,5);
display.println("Valve");
display.setCursor(30,25);
display.println("Angle:");
display.setCursor(29,45);
display.println(position);
display.display();
}
void updateOLED(int angle) {
display.clearDisplay();
display.setCursor(34,5);
display.println("Valve");
display.setCursor(30,25);
display.println("Angle:");
display.setCursor(2,45);
display.print(angle);
display.println(" degrees");
display.display();
}
void overrideupdateOLED(const char* position) {
display.clearDisplay();
display.setCursor(15,5);
display.println("OVERRIDE");
display.setCursor(30,25);
display.println("Angle:");
display.setCursor(29,45);
display.println(position);
display.display();
}
void overrideupdateOLED(int angle) {
display.clearDisplay();
display.setCursor(15,5);
display.println("OVERRIDE");
display.setCursor(30,25);
display.println("Angle:");
display.setCursor(2,45);
display.print(angle);
display.println(" degrees");
display.display();
}