// ========================================================================
//Libraries
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/Org_01.h>
#include <Fonts/FreeSansBold18pt7b.h>
//Screen
Adafruit_SSD1306 display (128, 64, &Wire, -1);
// ========================================================================
// System
int mode; // 1 = Set-up, 2 = Start
int set; // 1 = off, 2 = on
int start; // 1 = off, 2 = on
int menu; // 1 = off, 2 = on
// ========================================================================
void setup() {
pinMode(relay, OUTPUT);
pinMode(trig, OUTPUT); // Trigger Pin
pinMode(echo, INPUT); // Echo Pin
pinMode(btn1, INPUT); // button 1
pinMode(btn2, INPUT); // button 2
pinMode(btn3, INPUT); // button 3
Serial.begin(9600);
mode = 1;
set = 1;
start = 1;
menu = 2;
Frame = 0;
bottom = 0;
threshold = 0;
gostop = 1;
onoff= 1;
//Screen
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
}
// ========================================================================
void loop() {
// Clears the trigPin
digitalWrite(trig, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echo, HIGH);
// Calculating the distance
centi = duration*0.034/2;
inches = centi * 0.393701;
// ========================================================================
// Screen
display.clearDisplay();
display.setFont(&Org_01);
display.drawBitmap(0, 0, Border, 128, 64, WHITE);
// ========================================================================
// Set-up
if (set == 2) {
display.drawBitmap(0, 0, SetupTitles, 128, 64, WHITE);
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(92,37);
display.println(bottom);
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(81,53);
display.println(threshold);
// Choice
int press1 = digitalRead(btn1);
if (press1 == HIGH){
mode++;
if (mode > 2) {
mode = 1;
}
}
delay(30);
if (mode == 1) {
display.drawBitmap(0, 0, BotSel, 128, 64, WHITE);
}
else if (mode == 2) {
display.drawBitmap(0, 0, ThSel, 128, 64, WHITE);
}
// Read choice
int press2 = digitalRead(btn2);
if (press2 == HIGH) {
// Set-up
if (mode == 1) {
bottom = inches;
}
// Start
else if (mode == 2) {
threshold++;
if (threshold > 100){
threshold = 0;
}
}
}
// Back button
int press3 = digitalRead(btn3);
if (press3 == HIGH) {
onoff=1;
start = 1;
set = 1;
menu = 2;
}
}
// ========================================================================
// Start
if (start == 2) {
display.setFont(&Org_01);
float convert = (float)percentage/100;
int level = 60 * convert;
int height = 60 - level;
// Water Level Display
display.fillRect(2, height + 12, 124, 60, WHITE);
// Water Animation
display.drawBitmap(2, height, epd_bitmap_allArray[Frame], 124, 12, WHITE);
Frame = (Frame + 1) % 12;
display.fillRect(2, 44, 44, 14, BLACK);
display.fillRect(86, 44, 40, 14, BLACK);
display.fillRect(46, 44, 40, 14, BLACK);
display.fillRect(73, 3, 37, 37, BLACK);
display.drawBitmap(0, 0, WaterSensor, 128, 64, WHITE);
display.setTextSize(1);
display.setTextColor(WHITE, BLACK);
display.setCursor(30,52);
display.println(inches);
display.setCursor(110,52);
display.println(threshold);
display.setFont(&FreeSansBold18pt7b);
display.setTextColor(WHITE, BLACK);
display.fillRect(24, 7, 38, 27, BLACK);
display.setCursor(24,32);
display.println(percentage);
// Computation of Percentage
if (bottom != 0){
space = (float)inches / bottom;
convert = space*100;
percentage = 100-convert;
}
if (percentage < 0){
percentage = 0;
}
// Back button
int press3 = digitalRead(btn3);
if (press3 == HIGH) {
onoff=1;
start = 1;
set = 1;
menu = 2;
}
// Go Stop
int press2 = digitalRead(btn2);
if (press2 == HIGH) {
gostop++;
delay(100);
if (gostop > 2) {
gostop = 1;
}
delay(30);
}
// gostop
if (gostop == 2) {
if (threshold <= percentage){
gostop = 1;
}
else {
digitalWrite(relay, LOW);
display.drawBitmap(0, 0, On, 128, 64, WHITE);
}
}
else if (gostop == 1) {
digitalWrite(relay, HIGH);
display.drawBitmap(0, 0, Off, 128, 64, WHITE);
}
}
// ========================================================================
// Menu
if (menu == 2) {
display.drawBitmap(0, 0, Title, 128, 64, WHITE);
display.drawBitmap(0, 0, MenuOpt, 128, 64, WHITE);
// Choice
int press1 = digitalRead(btn1);
if (press1 == HIGH){
mode++;
delay(100);
if (mode > 2) {
mode = 1;
}
}
if (mode == 1) {
display.drawBitmap(0, 0, SelectStart, 128, 64, WHITE);
}
else if (mode == 2) {
display.drawBitmap(0, 0, SelectSetup, 128, 64, WHITE);
}
// ========================================================================
// Read choice
int press2 = digitalRead(btn2);
if (press2 == HIGH) {
// Set-up
if (mode == 2) {
onoff=1;
set = 2;
start = 1;
menu = 1;
}
if (menu == 2) {
onoff=1;
set = 1;
start = 1;
menu = 2;
}
// Start
if (bottom != 0){
if (mode == 1) {
onoff=1;
start = 2;
set = 1;
menu = 1;
}
}
}
// On Off
int press3 = digitalRead(btn3);
if (bottom != 0){
if (press3 == HIGH) {
onoff++;
delay(100);
if (onoff > 2) {
onoff = 1;
}
}
}
if (onoff == 2) {
digitalWrite(relay, LOW);
}
if (onoff == 1) {
digitalWrite(relay, HIGH);
}
}
// ========================================================================
display.display();
delay(50);
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(inches);
Serial.print("\t");
Serial.print("Mode: ");
Serial.print(mode);
Serial.print("\t");
Serial.print("Set Up: ");
Serial.print(set);
Serial.print("\t");
Serial.print("Start: ");
Serial.print(start);
Serial.print("\t");
Serial.print("menu: ");
Serial.print(menu);
Serial.print("\t");
Serial.print("On Off: ");
Serial.print(onoff);
Serial.print("\t");
Serial.print("Go Stop: ");
Serial.println(gostop);
}