// Pump control Set off time to zero for continuous run.
// Minimum run time is 30 seconds. Minimum off time is 5 seconds Maximum is 60
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ezButton.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)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO: A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO: 2(SDA), 3(SCL), ...
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Decalre global variables
unsigned long myTime, old_mytime; // create variable to track time change
int relay_pin = 7; // set relay_pin to Digital pin 7
bool Pumpoff = false; // variable to track if pump is on or off
unsigned long run_time = 30; // Default 30 second run time
unsigned long off_time = 30; // Default 30 second off time
ezButton button1(5); // create ezButton object that attach to pin 6;
ezButton button2(6); // create ezButton object that attach to pin 7;
void setup() {
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
button1.setDebounceTime(20); // set debounce time to 50 milliseconds
button2.setDebounceTime(20); // set debounce time to 50 milliseconds
pinMode(relay_pin, OUTPUT); // set pin to output mode
digitalWrite(relay_pin,HIGH); // set pin low to turn off relay on program start
myTime = millis(); // set myTime to the number of miliseconds since program start
//Serial.println("Pump on Time ");
//Serial.println(myTime);
}
void loop() {
button1.loop(); // MUST call the loop() function first
button2.loop(); // MUST call the loop() function first
// int btn1State = button1.getState();
// Serial.println("button 1 state: ");
// Serial.println(btn1State);
if(button1.isPressed()) {
//Serial.print("Run time is ");
run_time = run_time + 5;
//Serial.println (run_time);
if (run_time > 60)
run_time = 30;
}
if(button2.isPressed()) {
// Serial.print("Off time is ");
off_time = off_time + 5;
//Serial.println (off_time);
if (off_time > 60) {
off_time = 0;
old_mytime = myTime;
}
if (off_time > 0 and off_time < 10 and old_mytime > 0) {
myTime = old_mytime;
old_mytime = 0;
}
}
// Serial.print (millis());
// Serial.print (" ");
// Serial.print (myTime);
// Serial.print (" ");
// Serial.println(run_time*1000);
if (millis() >= (myTime + (off_time*1000)) and Pumpoff) { //turn pump on
myTime = millis(); // set myTime to the number of miliseconds since program start
// Serial.println("Pump on Time ");
// Serial.println(myTime);
Pumpoff = false;
digitalWrite(relay_pin,HIGH); // set pin high to turn on relay
}
if (off_time == 0) myTime = millis();
if (millis() >= (myTime + (run_time*1000)) and not Pumpoff) { //turn pump off
myTime = millis(); // set myTime to the number of miliseconds since program start
// Serial.println("Pump off Time ");
// Serial.println(myTime);
Pumpoff = true;
digitalWrite(relay_pin,LOW); // set pin low to turn off relay
}
// Clear the buffer
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(20,0);
display.print("Pump Time");
// display.setCursor(70,0);
// display.print("Time");
display.setCursor(35,30);
display.print("Off");
display.setCursor(85,30);
display.print("ON");
display.setCursor(35,50);
display.print(off_time);
display.setCursor(85,50);
display.print(run_time);
display.display();
}