#include <Keypad.h> //for key pad
#include "U8glib.h" //for display
#include <Stepper.h>
#include <NewPing.h>
#include "U8glib.h"
#include <Keypad.h>
#include <SD.h>
#include <SPI.h>
// display initiation
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST);
//---------------------------- making the key pad---------------------------------
const byte ROWS = 4; // four rows
const byte COLS = 4; // three columns
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
byte rowPins[ROWS] = { 40, 38, 36, 34 }; // connect to the row pinouts of the keypad
byte colPins[COLS] = { 41, 39, 37, 35 }; // connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
//----------------------------key pad is made------------------------------------
//stepper motors
const int stepsPerRevolution = 2048; // change this to fit the number of steps per revolution
Stepper stepper_1(stepsPerRevolution, 46, 48, 50, 52);
Stepper stepper_2(stepsPerRevolution, 47, 49, 51, 53);
// defining valve power inputs
#define valve1 22
#define valve2 23
#define valve3 24
#define valve4 25
#define valve5 26
#define valve6 27
#define valve7 28
#define valve8 29
#define valve9 30
#define valve10 31
// pump power input
#define pump1 32
#define pump2 33
// flowrate sensor
#define flowrate 18
// water level sensors' signals from tanks
#define tank1 A0
#define tank2 A1 // water level sensor
#define tank3 A2
#define tank4 A3
#define tank5 A4
#define filter_wl A5
#define backup_wl A6
// ultra sonic sensors
#define tank2_US_echo 3
#define tank2_US_trig 4
#define tank4_US_echo 5
#define tank4_US_trig 6
// filter failure incaders
#define backUp 12
#define filter 11
#define PH_OFFSET -3.00 //if there have offset
#define SensorPin A6 // the pH meter Analog output is connected with the Arduino’s Analog
float changedPhValue = 0;
unsigned long int avgValue; //Store the average value of the sensor feedback
int buf[10], temp;
void setup() {
// assigning pins
// to power the valves
pinMode(valve1, OUTPUT);
pinMode(valve2, OUTPUT);
pinMode(valve3, OUTPUT);
pinMode(valve4, OUTPUT);
pinMode(valve5, OUTPUT);
pinMode(valve6, OUTPUT);
pinMode(valve7, OUTPUT);
pinMode(valve8, OUTPUT);
pinMode(valve9, OUTPUT);
//ph pin
pinMode(13, OUTPUT);
// to power the pump
pinMode(pump1, OUTPUT);
pinMode(pump2, OUTPUT);
// sensor inputs from the tanks and filters
pinMode(tank1, INPUT);
pinMode(tank2, INPUT);
pinMode(tank3, INPUT);
pinMode(tank4, INPUT);
pinMode(filter_wl, INPUT);
pinMode(backup_wl, INPUT);
// display settings
u8g.setFont(u8g_font_tpssb);
u8g.setColorIndex(1); //white
// filters
// pinMode(backUp, INPUT);
// pinMode(filter, INPUT);
// serial monitor
Serial.begin(9600);
//shut all the valves at the begining
// digitalWrite(valve1, HIGH);
// digitalWrite(valve2, LOW);
// digitalWrite(valve3, LOW);
// digitalWrite(valve4, LOW);
// digitalWrite(valve5, LOW);
// digitalWrite(valve6, LOW);
// digitalWrite(valve7, LOW);
// digitalWrite(valve8, LOW);
// digitalWrite(valve9, LOW);
// digitalWrite(valve10, LOW);
u8g.setFont(u8g_font_ncenB18);
u8g.setColorIndex(1); // set the color to white(blue in our case)
Serial.begin(9600);
SD.begin();
}
//*************************** functions ************************************
// 'Water level ss', 128x64px
const unsigned char epd_bitmap_Water_level_ss[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x0c, 0x20, 0x00, 0x00, 0x00, 0x00, 0x07, 0x80, 0x00, 0x00, 0x07, 0x80, 0x00, 0x00,
0x00, 0x00, 0x0c, 0x20, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00,
0x00, 0x00, 0x0d, 0xa3, 0xc3, 0xe3, 0xc6, 0x60, 0x01, 0x83, 0xc6, 0x23, 0xc1, 0x80, 0x00, 0x00,
0x00, 0x00, 0x0d, 0xa4, 0x61, 0x86, 0x66, 0xe0, 0x01, 0x86, 0x66, 0x26, 0x61, 0x80, 0x00, 0x00,
0x00, 0x00, 0x0d, 0xa0, 0x61, 0x86, 0x67, 0x00, 0x01, 0x86, 0x66, 0x26, 0x61, 0x80, 0x00, 0x00,
0x00, 0x00, 0x0d, 0xa3, 0xe1, 0x87, 0xe6, 0x00, 0x01, 0x87, 0xe6, 0x27, 0xe1, 0x80, 0x00, 0x00,
0x00, 0x00, 0x0d, 0xa6, 0x61, 0x86, 0x06, 0x00, 0x01, 0x86, 0x06, 0x26, 0x01, 0x80, 0x00, 0x00,
0x00, 0x00, 0x0d, 0xa6, 0x61, 0x86, 0x26, 0x00, 0x01, 0x86, 0x23, 0x46, 0x21, 0x80, 0x00, 0x00,
0x00, 0x00, 0x06, 0xc3, 0xe0, 0xe3, 0xc6, 0x00, 0x07, 0xe3, 0xc1, 0x83, 0xc7, 0xe0, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x07, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x0c, 0x40, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x0c, 0x40, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x0c, 0x47, 0x83, 0x0c, 0xdd, 0x87, 0x80, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x0c, 0x4c, 0xc3, 0x0c, 0xda, 0xcc, 0xc0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x0c, 0x4c, 0xc3, 0x0c, 0xda, 0xcc, 0xc0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x0c, 0x4c, 0xc3, 0x0c, 0xda, 0xcf, 0xc0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x0c, 0x4c, 0xc3, 0x0c, 0xda, 0xcc, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x06, 0x8c, 0xc3, 0x0c, 0xd8, 0xcc, 0x40, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x03, 0x07, 0x8f, 0xc7, 0x98, 0xc7, 0x80, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x07, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// 'pH level ss', 128x64px
const unsigned char epd_bitmap_pH_level_ss[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x78, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x18, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x7c, 0x66, 0x00, 0x18, 0x3c, 0x62, 0x3c, 0x18, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x00, 0x18, 0x66, 0x62, 0x66, 0x18, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x66, 0x7e, 0x00, 0x18, 0x66, 0x62, 0x66, 0x18, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x00, 0x18, 0x7e, 0x62, 0x7e, 0x18, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x00, 0x18, 0x60, 0x62, 0x60, 0x18, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x00, 0x18, 0x62, 0x34, 0x62, 0x18, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x7c, 0x66, 0x00, 0x7e, 0x3c, 0x18, 0x3c, 0x7e, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// 'New Menu', 128x64px
const unsigned char epd_bitmap_New_Menu[] PROGMEM = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x80, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x80, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x80, 0x01, 0x80, 0x00, 0x00, 0x00, 0x66, 0x00, 0x78, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x01,
0x83, 0xc1, 0x80, 0x00, 0x00, 0x00, 0x66, 0x00, 0x18, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x01,
0x86, 0x61, 0x80, 0x00, 0x00, 0x7c, 0x66, 0x00, 0x18, 0x3c, 0x62, 0x3c, 0x18, 0x00, 0x00, 0x01,
0x86, 0x61, 0x80, 0x00, 0x00, 0x66, 0x66, 0x00, 0x18, 0x66, 0x62, 0x66, 0x18, 0x00, 0x00, 0x01,
0x86, 0x61, 0x80, 0x00, 0x00, 0x66, 0x7e, 0x00, 0x18, 0x66, 0x62, 0x66, 0x18, 0x00, 0x00, 0x01,
0x86, 0x61, 0x80, 0x00, 0x00, 0x66, 0x66, 0x00, 0x18, 0x7e, 0x62, 0x7e, 0x18, 0x00, 0x00, 0x01,
0x87, 0xe1, 0x80, 0x00, 0x00, 0x66, 0x66, 0x00, 0x18, 0x60, 0x62, 0x60, 0x18, 0x00, 0x00, 0x01,
0x86, 0x61, 0x80, 0x00, 0x00, 0x66, 0x66, 0x00, 0x18, 0x62, 0x34, 0x62, 0x18, 0x00, 0x00, 0x01,
0x86, 0x61, 0x80, 0x00, 0x00, 0x7c, 0x66, 0x00, 0x7e, 0x3c, 0x18, 0x3c, 0x7e, 0x00, 0x00, 0x01,
0x86, 0x61, 0x80, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x80, 0x01, 0x80, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x80, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x80, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x80, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x80, 0x01, 0x80, 0x00, 0x03, 0xc6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x60, 0x00, 0x01,
0x87, 0xc1, 0x80, 0x00, 0x06, 0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x60, 0x00, 0x01,
0x86, 0x61, 0x80, 0x00, 0x06, 0x07, 0xc3, 0xc7, 0xc3, 0xe3, 0xc0, 0x07, 0xc6, 0x60, 0x00, 0x01,
0x86, 0x61, 0x80, 0x00, 0x06, 0x06, 0x64, 0x66, 0x66, 0x66, 0x60, 0x06, 0x66, 0x60, 0x00, 0x01,
0x86, 0x61, 0x80, 0x00, 0x06, 0x06, 0x60, 0x66, 0x66, 0x66, 0x60, 0x06, 0x67, 0xe0, 0x00, 0x01,
0x87, 0xc1, 0x80, 0x00, 0x06, 0x06, 0x63, 0xe6, 0x66, 0x67, 0xe0, 0x06, 0x66, 0x60, 0x00, 0x01,
0x86, 0x61, 0x80, 0x00, 0x06, 0x06, 0x66, 0x66, 0x66, 0x66, 0x00, 0x06, 0x66, 0x60, 0x00, 0x01,
0x86, 0x61, 0x80, 0x00, 0x06, 0x26, 0x66, 0x66, 0x63, 0xe6, 0x20, 0x06, 0x66, 0x60, 0x00, 0x01,
0x86, 0x61, 0x80, 0x00, 0x03, 0xc6, 0x63, 0xe6, 0x60, 0x63, 0xc0, 0x07, 0xc6, 0x60, 0x00, 0x01,
0x87, 0xc1, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x60, 0x00, 0x06, 0x00, 0x00, 0x00, 0x01,
0x80, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x06, 0x00, 0x00, 0x00, 0x01,
0x80, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x80, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x80, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x80, 0x01, 0x80, 0x0c, 0x20, 0x00, 0x00, 0x00, 0x00, 0x07, 0x80, 0x00, 0x00, 0x07, 0x80, 0x01,
0x83, 0xc1, 0x80, 0x0c, 0x20, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x01,
0x86, 0x21, 0x80, 0x0d, 0xa3, 0xc3, 0xe3, 0xc6, 0x60, 0x01, 0x83, 0xc6, 0x23, 0xc1, 0x80, 0x01,
0x86, 0x01, 0x80, 0x0d, 0xa4, 0x61, 0x86, 0x66, 0xe0, 0x01, 0x86, 0x66, 0x26, 0x61, 0x80, 0x01,
0x86, 0x01, 0x80, 0x0d, 0xa0, 0x61, 0x86, 0x67, 0x00, 0x01, 0x86, 0x66, 0x26, 0x61, 0x80, 0x01,
0x86, 0x01, 0x80, 0x0d, 0xa3, 0xe1, 0x87, 0xe6, 0x00, 0x01, 0x87, 0xe6, 0x27, 0xe1, 0x80, 0x01,
0x86, 0x01, 0x80, 0x0d, 0xa6, 0x61, 0x86, 0x06, 0x00, 0x01, 0x86, 0x06, 0x26, 0x01, 0x80, 0x01,
0x86, 0x01, 0x80, 0x0d, 0xa6, 0x61, 0x86, 0x26, 0x00, 0x01, 0x86, 0x23, 0x46, 0x21, 0x80, 0x01,
0x86, 0x21, 0x80, 0x06, 0xc3, 0xe0, 0xe3, 0xc6, 0x00, 0x07, 0xe3, 0xc1, 0x83, 0xc7, 0xe0, 0x01,
0x83, 0xc1, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x80, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x80, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x80, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x80, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x80, 0x01, 0x80, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x01,
0x87, 0xc1, 0x80, 0x00, 0x06, 0x60, 0x00, 0x00, 0x00, 0x00, 0x60, 0x01, 0x80, 0x00, 0x00, 0x01,
0x86, 0x61, 0x80, 0x00, 0x06, 0x66, 0x63, 0xc0, 0x00, 0x03, 0xe3, 0xc3, 0xe3, 0xc0, 0x00, 0x01,
0x86, 0x61, 0x80, 0x00, 0x06, 0x66, 0xe6, 0x60, 0x00, 0x06, 0x64, 0x61, 0x84, 0x60, 0x00, 0x01,
0x86, 0x61, 0x80, 0x00, 0x07, 0xc7, 0x06, 0x60, 0x00, 0x06, 0x60, 0x61, 0x80, 0x60, 0x00, 0x01,
0x86, 0x61, 0x80, 0x00, 0x06, 0x06, 0x07, 0xe0, 0x00, 0x06, 0x63, 0xe1, 0x83, 0xe0, 0x00, 0x01,
0x86, 0x61, 0x80, 0x00, 0x06, 0x06, 0x06, 0x00, 0x00, 0x06, 0x66, 0x61, 0x86, 0x60, 0x00, 0x01,
0x86, 0x61, 0x80, 0x00, 0x06, 0x06, 0x06, 0x20, 0x00, 0x06, 0x66, 0x61, 0x86, 0x60, 0x00, 0x01,
0x86, 0x61, 0x80, 0x00, 0x06, 0x06, 0x03, 0xc1, 0x80, 0x03, 0xe3, 0xe0, 0xe3, 0xe0, 0x00, 0x01,
0x87, 0xc1, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x80, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x80, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};
// ph value changing screen---------------------------------------
String displayChangePhValue() {
char phValue = keypad.getKey();
//changedPhValue = phValue.toInt();
String phValueStr = String(phValue);
u8g.firstPage();
do {
u8g.drawBitmapP(0, 0, 128 / 8, 64, epd_bitmap_pH_level_ss);
u8g.drawStr(46, 48, phValueStr.c_str()); // 0,0 codinates of the printing point
} while (u8g.nextPage());
}
// showing water level--------------------------------------------
void displayWaterLevel(float waterVolume) {
int height = round(waterVolume / 20); // divided by area
u8g.firstPage();
do {
//u8g.drawBitmapP();
char volumeChar[10];
dtostrf(waterVolume, 3, 2, volumeChar);
u8g.drawStr(52, 45, volumeChar);
// char heightChar[10];
// dtostrf(height, 3, 2, heightChar);
u8g.drawBitmapP(0, 0, 128/8, 64, epd_bitmap_Water_level_ss);
int y_pos = (64 - height) - 11
u8g.drawBox(23, y_pos, 14, height);
} while (u8g.nextPage());
}
void displayPreDatabase() {
}
void dbWrite(String var, double value) {
File dataFile;
String file = var + ".txt";
// Create/Open file
dataFile = SD.open(var, FILE_WRITE);
// if the file opened okay, write to it:
if (dataFile) {
dataFile.println(value);
dataFile.close(); // close the file
Serial.println("Done.");
}
// if the file didn't open, print an error:
else {
Serial.println("error opening filePh.txt");
}
}
void dbRead(String var) {
File dataFile_1;
String file = var + ".txt";
dataFile_1 = SD.open(file);
if (dataFile_1) {
Serial.println("Ph readings:");
// Reading the whole file
while (dataFile_1.available()) {
Serial.write(dataFile_1.read());
}
dataFile_1.close();
} else {
Serial.println("error opening file");
}
}
void dbPrint(String var) {
File dataFile_2;
String file = var + ".txt";
dataFile_2 = SD.open(file);
if (dataFile_2) {
Serial.println("Volume readings:");
// Reading the whole file
while (dataFile_2.available()) {
Serial.write(dataFile_2.read());
dataFile_2 = SD.open(file, FILE_READ);
u8g.setPrintPos(0, 20);
u8g.print(dataFile_2.readString());
delay(100);
dataFile_2.close();
}
dataFile_2.close();
} else {
Serial.println("error opening Vol.txt");
}
}
// to turn on the valves
void valveON(int valve) {
// turning on the valve
digitalWrite(valve, HIGH);
}
// to turn off the valves
void valveOFF(int valve) {
// turning on the valve
digitalWrite(valve, LOW);
}
void disToSurface(int trigPin, int echoPin, int maxDistance) {
NewPing sonar(trigPin, echoPin, maxDistance); // NewPing setup of pins and maximum distance.
while (disToSurface > 3) {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
int distance = sonar.ping_cm(); // Send ping, get distance in cm and print result (0 = outside set distance range)
Serial.print("Distance: ");
Serial.print(distance);
Serial.println("cm");
}
}
// checking if tanks are full
bool isFull(int item) {
if (analogRead(item) > 450) {
Serial.print(true);
return true;
}
else {
return false;
}
}
// indicating filter failure
bool onBackUp(int pH) {
if ((pH < 5) || (pH > 6.5)) {
return true;
}
else {
return false;
}
}
// choosing the filter
void selectFilter(int pH) {
// flows through backup filter
if (onBackUp(pH)) {
valveON(valve5);
valveOFF(valve4);
}
// flows through initial filter
else {
valveON(valve4);
valveOFF(valve5);
}
}
// code for despenser
void feeder(int pH, int givenPh) {
if (pH < givenPh) {
while (pH == givenPh) {
stepper_1.setSpeed(60);
stepper_1.step(4);
}
}
if (pH > givenPh) {
while (pH == givenPh) {
stepper_2.setSpeed(60);
stepper_2.step(4);
}
}
}
// for displaying the pH values
void displayPH(float realTimePh) {
u8g.firstPage();
do {
u8g.drawBitmapP(0, 0, 128/8, 64, epd_bitmap_pH_level_ss);
char charValue[10];
dtostrf(realTimePh, 4, 2, charValue);
u8g.drawStr(46, 48, charValue); // 0,0 codinates of the printing point
} while (u8g.nextPage());
}
void mainMenu() {
u8g.firstPage();
do {
u8g.drawBitmapP(0, 0, 128/8, 64, epd_bitmap_New_Menu);
} while(u8g.nextPage());
}
void display() {
if (keypad.getKey() == "A") {
displayPH(7.5);
if (keypad.getKey() == "#") {
mainMenu();
}
}
if (keypad.getKey() == "B") {
displayChangePhValue();
if (keypad.getKey() == "#") {
mainMenu();
}
}
if (keypad.getKey() == "C") {
displayWaterLevel(400);
if (keypad.getKey() == "#") {
mainMenu();
}
}
if (keypad.getKey() == "D") {
displayPreDatabase();
if (keypad.getKey() == "#") {
mainMenu();
}
}
}
void adjustPH() {
changedPhValue = keypad.getKey();
}
float pHvalue() {
for (int i = 0; i < 10; i++) //Get 10 sample value from the sensor for smooth the value
{
buf[i] = analogRead(SensorPin);
delay(10);
}
for (int i = 0; i < 9; i++) //sort the analog from small to large
{
for (int j = i + 1; j < 10; j++) {
if (buf[i] > buf[j]) {
temp = buf[i];
buf[i] = buf[j];
buf[j] = temp;
}
}
}
avgValue = 0;
for (int i = 2; i < 8; i++) //take the average value of 6 center sample
avgValue += buf[i];
float phValue = (float)avgValue * 5.0 / 1024 / 6; //convert the analog into millivolt
phValue = 3.5 * phValue; //convert the millivolt into pH value
return phValue = phValue + PH_OFFSET;
}
// values
int pH = 7;
int filter_st = analogRead(filter_wl);
int backup_st = analogRead(backup_wl);
// *************************main loop ***********************
void loop() {
mainMenu();
delay(5000);
displayWaterLevel(40);
delay(5000);
// if (isFull(filter)) {
// valveON(valve2);
// valveOFF(valve1);
// valveOFF(valve3);
// }
// else {
// valveON(valve1);
// valveON(valve3);
// valveOFF(valve2);
// }
// while (not isFull(tank2)) {
// valveOFF(valve4);
// }
// valveON(valve4);
// digitalWrite(pump, HIGH);
// if (onBackUP) {
// valveON(valve6);
// valveOFF(valve5)
// }
// else {
// valveOFF(valve6);
// valveON(valve5);
// }
// while (not isFull(tank3)) {
// valveOFF(valve7);
// }
// valveON(valve7);
// feeder(phValue(), changedPhValue);
// valveON(valve8);
}