#include <SPI.h>
#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
const int gSensorPin =2;
const int olSensorPin =3;
const int ohSensorPin =4;
const int MotorPin =13;
const int ManualStartPin =6;
int GroundTankState;
int OLowTankState;
int OHighTankState;
int ManualOverride;
const int ledPin = LED_BUILTIN;// the number of the LED pin
int ledState; //Using for blink of filler
unsigned long previousMillis = 0;
const long interval = 1000;
unsigned long currentMillis = millis();
const unsigned char myBitmap [] 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, 0x55, 0x55, 0x55, 0x50, 0x2a, 0xaa, 0xaa, 0xa0, 0x55, 0x55, 0x55, 0x50,
0x2a, 0xaa, 0xaa, 0xa0, 0x55, 0x55, 0x55, 0x50, 0x2a, 0xaa, 0xaa, 0xa0, 0x55, 0x55, 0x55, 0x50,
0x2a, 0xaa, 0xaa, 0xa0, 0x55, 0x55, 0x55, 0x50, 0x00, 0x00, 0x00, 0x00
};
// 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);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(gSensorPin, INPUT);
pinMode(olSensorPin, INPUT);
pinMode(ohSensorPin, INPUT);
pinMode(ManualStartPin, INPUT);
pinMode(MotorPin, OUTPUT);
//pinMode(ManualStartPin, INPUT);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
//UI on Setup
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.drawLine(64, 17, 64, 64, WHITE); // Centre partition
display.drawLine(63, 17, 63, 64, WHITE); // Centre partition
display.setTextColor(SSD1306_WHITE);
display.setCursor(14,57);
display.println(F("GROUND")); //Ground Tank Label
display.setCursor(74,57);
display.println(F("OVERHEAD")); //Overhead Tank Label
//Ground Tank Empty placeholder
display.drawLine(47, 53, 17, 53, WHITE);
display.drawLine(17, 34, 17, 53, WHITE);
display.drawLine(47, 34, 47, 53, WHITE);
display.drawLine(17, 34, 14, 34, WHITE);
display.drawLine(50, 34, 47, 34, WHITE);
//Overhead Tank Empty placeholder
display.drawLine(111, 53, 81, 53, WHITE);
display.drawLine(81, 34, 81, 53, WHITE);
display.drawLine(111, 34, 111, 53, WHITE);
display.drawLine(114, 34, 111, 34, WHITE);
display.drawLine(81, 34, 78, 34, WHITE);
// Tank filler (Upon motor ON, Black patch would cover the fillers)
//display.drawBitmap(18, 34, myBitmap, 29, 19, WHITE);
//display.drawBitmap(18, 26, myBitmap, 29, 19, WHITE);
//display.drawBitmap(82, 34, myBitmap, 29, 19, WHITE);
//display.drawBitmap(82, 26, myBitmap, 29, 19, WHITE);
display.display();
}
void loop() {
// put your main code here, to run repeatedly:
GroundTankState = digitalRead(gSensorPin);
OLowTankState = digitalRead(olSensorPin);
OHighTankState = digitalRead(ohSensorPin);
ManualOverride = digitalRead(ManualStartPin);
if (OHighTankState == HIGH && OLowTankState == LOW)
{
Serial.println("ERROR IN OVHEAD SENSOR, CHECK");
digitalWrite(MotorPin, HIGH);
CaseE1(); //Error 1
}
if (GroundTankState == LOW && OHighTankState == HIGH && OLowTankState == HIGH)
{
Serial.println("Ground Tank Empty");
digitalWrite(MotorPin, HIGH);
Case1(); //Ground Tank Empty
}
if (GroundTankState == HIGH && OHighTankState == HIGH && OLowTankState == HIGH)
{
Serial.println("Ground Tank Full");
digitalWrite(MotorPin, HIGH);
Case9(); //Ground Tank Full
}
if (OHighTankState == HIGH && OLowTankState == HIGH)
{
Serial.println("Overhead Tank is full");
digitalWrite(MotorPin, HIGH);
Case2(); //Overhead Tank is full
}
if (GroundTankState == LOW && OHighTankState == LOW && OLowTankState == HIGH)
{
Serial.println("Ground Tank Empty & Overhead Tank is Half");
digitalWrite(MotorPin, HIGH);
Case3(); // Ground Tank Empty & Overhead tank is Half
}
if (GroundTankState == HIGH && OHighTankState == LOW && OLowTankState == HIGH)
{
Serial.println("Overhead Tank is Half");
digitalWrite(MotorPin, HIGH);
Case4(); //Overhead Tank is Half
Case9(); // Ground Tank is Full
}
if (GroundTankState == HIGH && OHighTankState == LOW && OLowTankState == HIGH && ManualOverride == HIGH)
{
do {
Serial.println("Manual Override, Motor Start");
OHighTankState = digitalRead(ohSensorPin); // check the sensors
GroundTankState = digitalRead(gSensorPin);
digitalWrite(MotorPin, LOW);
Case5(); //Manual Override, Motor ON
}
while (OHighTankState != HIGH && GroundTankState != LOW);
}
if (GroundTankState == HIGH && OHighTankState == LOW && OLowTankState == LOW)
{
do {
Serial.println("Motor Start");
OHighTankState = digitalRead(ohSensorPin); // check the sensors
GroundTankState = digitalRead(gSensorPin);
digitalWrite(MotorPin, LOW);
Case6(); //Auto - Motor ON
}
while (OHighTankState != HIGH && GroundTankState != LOW);
}
if (GroundTankState == LOW && OHighTankState == LOW && OLowTankState == LOW)
{
Serial.println("Ground Tank Empty & Overhead Tank Empty");
digitalWrite(MotorPin, HIGH);
Case7(); //Both tanks are empty
}
}
void Case1(void) //Ground Tank Empty
{
display.fillRect(0,17,62, 12, WHITE);
display.setTextSize(1);
display.setCursor(18,19);
display.setTextColor(SSD1306_BLACK);
display.println(F("EMPTY"));
//display.fillRect(66,17,62, 12, WHITE);
display.fillRect(0,0,128, 16, BLACK), // MOTOR Status bar
display.setCursor(39,4);
display.setTextColor(SSD1306_WHITE);
display.println(F("MOTOR OFF"));
//display.drawBitmap(18, 34, myBitmap, 29, 19, WHITE);
//display.drawBitmap(18, 26, myBitmap, 29, 19, WHITE);
display.fillRect(18,34,29,19, BLACK);
//display.fillRect(82,34,29,11, WHITE);
display.display();
}
void Case2(void) //Overhead Tank is full
{
display.fillRect(66,17,62, 12, WHITE);
display.setTextSize(1);
display.setCursor(86,19);
display.setTextColor(SSD1306_BLACK);
display.println(F("FULL"));
display.fillRect(0,0,128, 16, BLACK), // MOTOR Status bar
display.setCursor(39,4);
display.setTextColor(SSD1306_WHITE);
display.println(F("MOTOR OFF"));
//display.drawBitmap(18, 34, myBitmap, 29, 19, WHITE);
//display.drawBitmap(18, 26, myBitmap, 29, 19, WHITE);
//display.fillRect(18,34,29,19, WHITE);
display.drawBitmap(82, 34, myBitmap, 29, 19, WHITE);
display.drawBitmap(82, 26, myBitmap, 29, 19, WHITE);
//display.fillRect(82,34,29,11, WHITE);
display.display();
}
void Case3(void) // Ground tank is Empty & Overhead tank is Half
{
display.fillRect(0,17,62, 12, WHITE);
display.setTextSize(1); // Normal 1:1 pixel scale
display.setCursor(18,19);
display.setTextColor(SSD1306_BLACK); // Draw white text
display.println(F("EMPTY"));
display.fillRect(66,17,62, 12, WHITE);
display.setTextSize(1); // Normal 1:1 pixel scale
display.setCursor(86,19);
display.setTextColor(SSD1306_BLACK); // Draw white text
display.println(F("HALF"));
display.fillRect(0,0,128, 16, BLACK), // MOTOR Status bar
display.setTextSize(1);
display.setCursor(39,4);
display.setTextColor(SSD1306_WHITE);
display.println(F("MOTOR OFF"));
//display.drawBitmap(18, 34, myBitmap, 29, 19, WHITE);
//display.drawBitmap(18, 26, myBitmap, 29, 19, WHITE);
//display.fillRect(18,34,29,19, WHITE);
display.fillRect(18,34,29,19, BLACK);
//display.drawBitmap(82, 26, myBitmap, 29, 19, WHITE);
display.drawBitmap(82, 34, myBitmap, 29, 19, WHITE);
display.fillRect(82,34,29,9, BLACK);
display.display();
}
void Case4(void) //Overhead Tank is Half
{
display.fillRect(66,17,62, 12, WHITE); // OHTANK Status bar
display.setTextSize(1);
display.setCursor(86,19);
display.setTextColor(SSD1306_BLACK);
display.println(F("HALF"));
display.fillRect(0,0,128, 16, BLACK), // MOTOR Status bar
display.setTextSize(1);
display.setCursor(39,4);
display.setTextColor(SSD1306_WHITE);
display.println(F("MOTOR OFF"));
//display.drawBitmap(18, 34, myBitmap, 29, 19, WHITE);
//display.drawBitmap(18, 26, myBitmap, 29, 19, WHITE);
//display.fillRect(18,34,29,19, WHITE);
//display.fillRect(18,34,29,19, BLACK);
//display.drawBitmap(82, 26, myBitmap, 29, 19, WHITE);
display.drawBitmap(82, 34, myBitmap, 29, 19, WHITE);
display.fillRect(82,34,29,9, BLACK);
display.display();
}
void Case5(void) //Manual Override, Motor ON//Auto - Motor ON
{
display.fillRect(66,17,62, 12, WHITE); // OHTANK Status bar
display.fillRect(0,0,128, 16, WHITE), // MOTOR Status bar
display.setTextSize(1);
display.setCursor(12,4);
display.setTextColor(SSD1306_BLACK);
display.println(F("MANUAL - MOTOR ON"));
display.setCursor(76,19);
display.setTextColor(SSD1306_BLACK);
display.println(F("FILLING"));
//display.drawBitmap(18, 34, myBitmap, 29, 19, WHITE);
//display.drawBitmap(18, 26, myBitmap, 29, 19, WHITE);
//display.fillRect(18,34,29,19, WHITE);
//display.fillRect(18,34,29,19, BLACK);
//display.drawBitmap(82, 26, myBitmap, 29, 19, WHITE);
display.drawBitmap(82, 34, myBitmap, 29, 19, WHITE);
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == BLACK) {
ledState = WHITE;
} else {
ledState = BLACK;
}
// set the LED with the ledState of the variable:
display.drawBitmap(82, 26, myBitmap, 29, 17, ledState);
}
display.display();
}
void Case6(void) //Auto - Motor ON
{
display.fillRect(66,17,62, 12, WHITE);
display.fillRect(0,0,128, 16, WHITE), // MOTOR Status bar
display.setCursor(39,4);
display.setTextColor(SSD1306_BLACK);
display.println(F("MOTOR ON"));
display.setTextSize(1);
display.setCursor(18,19);
display.setTextColor(SSD1306_WHITE);
display.println(F("EMPTY"));
display.fillRect(66,17,62, 12, WHITE);// OHTANK Status bar
display.setTextSize(1);
display.setCursor(76,19);
display.setTextColor(SSD1306_BLACK);
display.println(F("FILLING"));
display.drawBitmap(18, 34, myBitmap, 29, 19, WHITE);
display.drawBitmap(18, 26, myBitmap, 29, 19, WHITE);
//display.fillRect(18,34,29,19, BLACK);
//display.drawBitmap(82, 26, myBitmap, 29, 19, WHITE);
//display.drawBitmap(82, 34, myBitmap, 29, 19, WHITE);
//display.fillRect(82,34,29,11, BLACK);
display.drawBitmap(82, 34, myBitmap, 29, 19, WHITE);
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == BLACK) {
ledState = WHITE;
} else {
ledState = BLACK;
}
// set the LED with the ledState of the variable:
display.drawBitmap(82, 26, myBitmap, 29, 17, ledState);
}
display.display();
}
void Case7(void) //Both tanks are empty
{
display.fillRect(0,17,62, 12, WHITE); // GTANK Status bar
display.fillRect(0,0,128, 16, BLACK); // MOTOR Status bar
display.setCursor(39,4);
display.setTextColor(SSD1306_WHITE);
display.println(F("MOTOR OFF"));
display.setTextSize(1);
display.setCursor(18,19);
display.setTextColor(SSD1306_BLACK);
display.println(F("EMPTY"));
display.fillRect(66,17,62, 12, WHITE); // OHTANK Status bar
display.setCursor(83,19);
display.setTextColor(SSD1306_BLACK);
display.println(F("EMPTY"));
//display.drawBitmap(18, 34, myBitmap, 29, 19, WHITE);
//display.drawBitmap(18, 26, myBitmap, 29, 19, WHITE);
display.fillRect(18,34,29,19, BLACK);
//display.drawBitmap(82, 26, myBitmap, 29, 19, WHITE);
//display.drawBitmap(82, 34, myBitmap, 29, 19, WHITE);
display.fillRect(82,34,29,19, BLACK);
display.display();
}
void Case9(void) //Ground Tank FULL
{
display.fillRect(0,0,128, 16, BLACK); // MOTOR Status bar
display.setCursor(39,4);
display.setTextColor(SSD1306_WHITE);
display.println(F("MOTOR OFF"));
display.fillRect(0,17,62, 12, WHITE); // GTANK Status bar
display.setTextSize(1); // Normal 1:1 pixel scale
display.setCursor(22,19);
display.setTextColor(SSD1306_WHITE); // Draw white text
display.println(F("EMPTY"));
display.drawBitmap(18, 34, myBitmap, 29, 19, WHITE);
display.drawBitmap(18, 26, myBitmap, 29, 19, WHITE);
//display.fillRect(18,34,29,19, WHITE);
//display.drawBitmap(82, 26, myBitmap, 29, 19, WHITE);
//display.drawBitmap(82, 34, myBitmap, 29, 19, WHITE);
//display.fillRect(82,34,29,11, BLACK);
display.display();
}
void CaseE1(void) //Error 1
{
display.fillRect(0,17,62, 12, BLACK); // GTANK Status bar
display.fillRect(66,17,62, 12, BLACK); // OHTANK Status bar
display.fillRect(0,0,128, 16, BLACK); // MOTOR Status bar
display.setCursor(36,4);
display.setTextColor(SSD1306_WHITE);
display.println(F("ERROR OH-H"));
//display.drawBitmap(18, 34, myBitmap, 29, 19, WHITE);
//display.drawBitmap(18, 26, myBitmap, 29, 19, WHITE);
display.fillRect(18,34,29,19, BLACK);
//display.drawBitmap(82, 26, myBitmap, 29, 19, WHITE);
//display.drawBitmap(82, 34, myBitmap, 29, 19, WHITE);
display.fillRect(82,34,29,19, BLACK);
display.display();
}