Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <gfxfont.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #include <EEPROM.h>
- #define OLED_RESET 4
- Adafruit_SSD1306 display(OLED_RESET);
- //Arrays for user input possibilities
- const int ml[11] = {0, 25, 50, 75, 100, 125, 150, 175, 200, 225, 250};
- const int hrs[4] = {6, 12, 18, 24};
- //EEPROM Memory addresses
- const int EEPROMP1ml = 0;
- const int EEPROMP1hrs = 1;
- const int EEPROMP2ml = 2;
- const int EEPROMP2hrs = 3;
- const int EEPROMP3ml = 4;
- const int EEPROMP3hrs = 5;
- const int EEPROMP4ml = 6;
- const int EEPROMP4hrs = 7;
- //Current preset values variables
- int P1ml;
- int P1hrs;
- int P2ml;
- int P2hrs;
- int P3ml;
- int P3hrs;
- int P4ml;
- int P4hrs;
- //Buttons
- int btnLeft = 3;
- int btnRight = 4;
- //Relays
- int pump = 12;
- int sp[7] = {5,6,7,8,9,10,11};
- int runMode = 0;
- int selected = 0;
- int selected2 = 0;
- long int1Last = 0;
- int P1Next;
- int P2Next;
- int P3Next;
- int P4Next;
- long P1End;
- long P2End;
- long P3End;
- long P4End;
- int mlEdit;
- int hrsEdit;
- int runTime = 1; //millis in a second of run time, 1000 is real time
- bool right = false;
- void setup() {
- //Display init
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
- //Assigning current preset values to EEPROM Memory adresses
- P1ml = EEPROM.read(EEPROMP1ml);
- if(P1ml>10){P1ml=0;EEPROM.write(EEPROMP1ml,0);}
- P1hrs = EEPROM.read(EEPROMP1hrs);
- if(P1hrs>3){P1hrs=0;EEPROM.write(EEPROMP1hrs,0);}
- P1Next = hrs[P1hrs];
- P2ml = EEPROM.read(EEPROMP2ml);
- if(P2ml>10){P2ml=0;EEPROM.write(EEPROMP2ml,0);}
- P2hrs = EEPROM.read(EEPROMP2hrs);
- if(P2hrs>3){P2hrs=0;EEPROM.write(EEPROMP2hrs,0);}
- P2Next = hrs[P2hrs];
- P3ml = EEPROM.read(EEPROMP3ml);
- if(P3ml>10){P3ml=0;EEPROM.write(EEPROMP3ml,0);}
- P3hrs = EEPROM.read(EEPROMP3hrs);
- if(P3hrs>3){P3hrs=0;EEPROM.write(EEPROMP3hrs,0);}
- P3Next = hrs[P3hrs];
- P4ml = EEPROM.read(EEPROMP4ml);
- if(P4ml>10){P4ml=0;EEPROM.write(EEPROMP4ml,0);}
- P4hrs = EEPROM.read(EEPROMP4hrs);
- if(P4hrs>3){P4hrs=0;EEPROM.write(EEPROMP4hrs,0);}
- P4Next = hrs[P4hrs];
- //Setup buttons
- pinMode(btnLeft, INPUT_PULLUP);
- pinMode(btnRight, INPUT_PULLUP);
- attachInterrupt(1, BtnLeft, FALLING);
- runMode = 0;
- //Setup outputs to relays
- pinMode(pump, OUTPUT);
- digitalWrite(pump, HIGH);
- pinMode(sp[0], OUTPUT);
- digitalWrite(sp[0], HIGH);
- pinMode(sp[1], OUTPUT);
- digitalWrite(sp[1], HIGH);
- pinMode(sp[2], OUTPUT);
- digitalWrite(sp[2], HIGH);
- pinMode(sp[3], OUTPUT);
- digitalWrite(sp[3], HIGH);
- pinMode(sp[4], OUTPUT);
- digitalWrite(sp[4], HIGH);
- pinMode(sp[5], OUTPUT);
- digitalWrite(sp[5], HIGH);
- pinMode(sp[6], OUTPUT);
- digitalWrite(sp[6], HIGH);
- }
- void BtnLeft() {
- if(millis()-int1Last > 75){
- if(runMode == 0){
- runMode = 1;
- selected = 0;
- } else if(runMode == 1){
- if(selected == 4){
- selected = 0;
- } else {
- selected++;
- }
- } else if(runMode == 2){
- if(selected2 == 3){
- selected2 = 0;
- } else {
- selected2++;
- }
- }
- }
- int1Last = millis();
- }
- void loop() {
- //running
- bool P1Running = false;
- bool P2Running = false;
- bool P3Running = false;
- bool P4Running = false;
- while (runMode == 0) {
- long n = millis()/runTime;
- int h = (n / 3600) % 24;
- int m = (n / 60) % 60;
- int s = n % 60;
- if(not(P1Running or P2Running or P3Running or P4Running)) {
- if((P1Next <= h and P1Next >= 6) or (h < 6 and P1Next <= h)){
- P1Running = true;
- P1Next = P1Next + hrs[P1hrs];
- if(P1Next>=24){P1Next = P1Next % 24;}
- digitalWrite(sp[0], LOW);
- digitalWrite(pump, LOW);
- P1End = n + 1000;
- } else if((P2Next <= h and P2Next >= 6) or (h < 6 and P2Next <= h)){
- P2Running = true;
- P2Next = P2Next + hrs[P2hrs];
- if(P2Next>=24){P2Next = P2Next % 24;}
- digitalWrite(sp[1], LOW);
- digitalWrite(pump, LOW);
- P2End = n + 1000;
- } else if((P3Next <= h and P3Next >= 6) or (h < 6 and P3Next <= h)){
- P3Running = true;
- P3Next = P3Next + hrs[P3hrs];
- if(P3Next>=24){P3Next = P3Next % 24;}
- digitalWrite(sp[2], LOW);
- digitalWrite(pump, LOW);
- P3End = n + 1000;
- } else if((P4Next <= h and P4Next >= 6) or (h < 6 and P4Next <= h)){
- P4Running = true;
- P4Next = P4Next + hrs[P4hrs];
- if(P4Next>=24){P4Next = P4Next % 24;}
- digitalWrite(sp[3], LOW);
- digitalWrite(pump, LOW);
- P4End = n + 1000;
- }
- }
- if(P1Running and n > P1End){
- P1Running = false;
- digitalWrite(pump, HIGH);
- digitalWrite(sp[0], HIGH);
- }
- if(P2Running and n > P2End){
- P2Running = false;
- digitalWrite(pump, HIGH);
- digitalWrite(sp[1], HIGH);
- }
- if(P3Running and n > P3End){
- P3Running = false;
- digitalWrite(pump, HIGH);
- digitalWrite(sp[2], HIGH);
- }
- if(P4Running and n > P4End){
- P4Running = false;
- digitalWrite(pump, HIGH);
- digitalWrite(sp[3], HIGH);
- }
- display.clearDisplay();
- display.setTextSize(1);
- display.setTextColor(WHITE);
- display.setCursor(0,0);
- display.print("Running...");
- display.setCursor(75,0);
- display.print(h);display.print(":");if(m<10){display.print("0");}display.print(m);display.print(":");if(s<10){display.print("0");}display.print(s);
- display.setCursor(0,8);
- if(P1Running == true){display.print(" [1]");}else{display.print(" 1");}
- display.setCursor(32,8);
- if(P2Running == true){display.print(" [2]");}else{display.print(" 2");}
- display.setCursor(64,8);
- if(P3Running == true){display.print(" [3]");}else{display.print(" 3");}
- display.setCursor(96,8);
- if(P4Running == true){display.print(" [4]");}else{display.print(" 4");}
- display.setCursor(0,16);
- display.print(P1Next);display.print(":00");
- display.setCursor(32,16);
- display.print(P2Next);display.print(":00");
- display.setCursor(64,16);
- display.print(P3Next);display.print(":00");
- display.setCursor(96,16);
- display.print(P4Next);display.print(":00");
- display.setCursor(0,24);
- display.setTextColor(BLACK,WHITE);
- display.print("Edit");
- display.display();
- delay(100);
- }
- while (runMode == 1) {
- //Make sure everything is off
- digitalWrite(pump, HIGH);
- digitalWrite(sp[0], HIGH);
- digitalWrite(sp[1], HIGH);
- digitalWrite(sp[2], HIGH);
- digitalWrite(sp[3], HIGH);
- //Read the right button
- right = not(digitalRead(btnRight));
- if(selected == 4 and right == true){
- //check if next is in the past and correct
- if(P1Next <= (millis()/(3600*runTime)) % 24) {P1Next = ((millis()/(3600*runTime)) % 24) + hrs[P1hrs];if(P1Next>=24){P1Next = P1Next % 24;}}
- if(P2Next <= (millis()/(3600*runTime)) % 24) {P2Next = ((millis()/(3600*runTime)) % 24) + hrs[P2hrs];if(P2Next>=24){P2Next = P2Next % 24;}}
- if(P3Next <= (millis()/(3600*runTime)) % 24) {P3Next = ((millis()/(3600*runTime)) % 24) + hrs[P3hrs];if(P3Next>=24){P3Next = P3Next % 24;}}
- if(P4Next <= (millis()/(3600*runTime)) % 24) {P4Next = ((millis()/(3600*runTime)) % 24) + hrs[P4hrs];if(P4Next>=24){P4Next = P4Next % 24;}}
- runMode = 0;
- } else if(right == true){
- selected2 = 0;
- mlEdit = selected;
- hrsEdit = selected;
- runMode = 2;
- }
- display.clearDisplay();
- display.setTextSize(1);
- display.setTextColor(WHITE);
- display.setCursor(0,0);
- display.print("Preset:");
- display.setCursor(96,0);
- if(selected == 4){display.setTextColor(BLACK,WHITE);}display.print(" Run ");display.setTextColor(WHITE);
- display.setCursor(0,8);
- if(selected == 0){display.setTextColor(BLACK,WHITE);}display.print(" 1 ");display.setTextColor(WHITE);
- display.setCursor(32,8);
- if(selected == 1){display.setTextColor(BLACK,WHITE);}display.print(" 2 ");display.setTextColor(WHITE);
- display.setCursor(64,8);
- if(selected == 2){display.setTextColor(BLACK,WHITE);}display.print(" 3 ");display.setTextColor(WHITE);
- display.setCursor(96,8);
- if(selected == 3){display.setTextColor(BLACK,WHITE);}display.print(" 4 ");display.setTextColor(WHITE);
- display.setCursor(0,16);
- display.print(ml[P1ml]);display.print("/");display.print(hrs[P1hrs]);
- display.setCursor(32,16);
- display.print(ml[P2ml]);display.print("/");display.print(hrs[P2hrs]);
- display.setCursor(64,16);
- display.print(ml[P3ml]);display.print("/");display.print(hrs[P3hrs]);
- display.setCursor(96,16);
- display.print(ml[P4ml]);display.print("/");display.print(hrs[P4hrs]);
- display.setCursor(0,24);
- display.setTextColor(BLACK,WHITE);
- display.print("Select");
- display.setCursor(96,24);
- display.print("Enter");
- display.display();
- delay(100);
- }
- while(runMode == 2){
- right = not(digitalRead(btnRight));
- if(right and selected2 == 0){
- runMode = 1;
- }
- if(selected2 == 1 and right) {
- if(mlEdit >= 10){
- mlEdit = 0;
- } else {
- mlEdit++;
- }
- }
- if(selected2 == 2 and right) {
- if(hrsEdit >= 3){
- hrsEdit = 0;
- } else {
- hrsEdit++;
- }
- }
- display.clearDisplay();
- display.setTextSize(1);
- display.setTextColor(WHITE);
- display.setCursor(0,0);
- display.print("Preset ");display.print(selected + 1);
- display.setCursor(58,0);
- if(selected2 == 0){display.setTextColor(BLACK,WHITE);}display.print("Cancel");display.setTextColor(WHITE);
- display.setCursor(96,0);
- if(selected2 == 3){display.setTextColor(BLACK,WHITE);}display.print("Save");display.setTextColor(WHITE);
- display.setCursor(0,8);
- if(selected2 == 1){display.setTextColor(BLACK,WHITE);}display.print(ml[mlEdit]);display.setTextColor(WHITE);display.print(" ml");
- display.setCursor(0,16);
- if(selected2 == 2){display.setTextColor(BLACK,WHITE);}display.print(hrs[hrsEdit]);display.setTextColor(WHITE);display.print(" hrs");
- display.setCursor(0,24);
- display.setTextColor(BLACK,WHITE);
- display.print("Select");
- display.setTextColor(BLACK,WHITE);
- display.setCursor(90,24);
- if(selected2 == 0 or selected2 == 3) {
- display.print("Enter");
- } else {
- display.print("Adjust");
- }
- display.display();
- delay(100);
- }
- }
Add Comment
Please, Sign In to add comment