#include #include "RTClib.h" #include #include #include #include #define DHTPIN 9 // what pin we're connected to #define DHTTYPE DHT11 // DHT 11 #define OLED_RESET 4 #define OLED_WIDTH 128 #define OLED_HEIGHT 32 #define OLED_YELLOW_HEIGHT 8 Adafruit_SSD1306 display(OLED_RESET); // Initialize DHT sensor for normal 16mhz Arduino DHT dht(DHTPIN, DHTTYPE); RTC_DS3231 rtc; char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; int potPin = A0; int lampPin = 3; int moisturePin = A1; int downButtonPin = 10; int leftButtonPin = 8; int rightButtonPin = 7; int upButtonPin = 6; int configButtonPin = 5; int selectButtonPin = 2; int pumpControlPin = 12; int lampMin = 83; bool upSt, downSt, leftSt, rightSt, selectSt, configSt = false; int moistAddr = 0; int sunAddr = 1; int onHour = 8; int offHour = 20; //int sunPerc = 45; int sunPerc = EEPROM.read(sunAddr);//percent //int moistPerc = 15; int moistPerc = EEPROM.read(moistAddr);//percent void clearRect(int x0, int y0, int x1, int y1){ for (int x = x0; x < x1; x++){ for (int y = y0; y < y1; y++){ display.drawPixel(x, y, BLACK); } } //display.display(); } int vals[OLED_WIDTH] = {};/* 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };//*/ class Graph { int width; int height; int tlPosX; int tlPosY; //int vals[width]; int numFilled = 0; //int vals[]; public: Graph(int w, int h, int x, int y) { width = w; height = h; tlPosX = x; tlPosY = y; //int vals[width]; } void drawGraph(){ //clearRect(tlPosX, tlPosY, tlPosX + width, tlPosY + height); for (int i = tlPosX; i < width; i++){ for (int h = tlPosY; h < height; h++){ display.drawPixel(i, h, BLACK); } display.drawPixel(i, vals[i], WHITE); } display.display(); } }; Graph boi = Graph(OLED_WIDTH, OLED_YELLOW_HEIGHT, 0, 0); void setup() { pinMode(lampPin, OUTPUT); analogWrite(lampPin, 0); pinMode(downButtonPin, INPUT); pinMode(leftButtonPin, INPUT); pinMode(rightButtonPin, INPUT); pinMode(upButtonPin, INPUT); pinMode(selectButtonPin, INPUT); pinMode(moisturePin, INPUT); pinMode(configButtonPin, INPUT); pinMode(pumpControlPin, OUTPUT); pinMode(potPin, INPUT); Wire.begin(); dht.begin(); // initialize dht display.begin(SSD1306_SWITCHCAPVCC, 0x3C);// initialize with the I2C addr 0x3C (for the 128x32)(initializing the display) Serial.begin(9600); display.clearDisplay(); // clearing the display display.setTextColor(WHITE); //setting the color display.setTextSize(1); //set the font size delay(1000); if (!rtc.begin()){ Serial.println("Couldn't find RTC"); }else{ Serial.println("Setting date and time"); rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); } Serial.println("Startup finished, activating device"); analogWrite(lampPin, 0); //Serial.println("boi"); delay(1000); } bool state = false; int pix = 0; void makeRect(int x0, int y0, int x1, int y1, bool filled = false){ if (!filled){ for (int x = x0; x < x1; x++){ display.drawPixel(x, y0, WHITE); display.drawPixel(x, y1, WHITE); } for (int y = y0; y <= y1; y++){ display.drawPixel(x0, y, WHITE); display.drawPixel(x1, y, WHITE); } }else{ for (int x = x0; x < x1; x++){ for (int y = y0; y <= y1; y++){ display.drawPixel(x, y, WHITE); } } } //display.display(); } void makeText(int x, int y, String text, bool inv = false, bool memer = false){ if (memer){ clearRect(x,y,x + text.length()*6 - 1, y+7); } if (!inv){ display.setTextColor(WHITE); display.setCursor(x, y); display.print(text); }else{ display.setTextColor(BLACK); display.setCursor(x, y); display.print(text); display.setTextColor(WHITE); } display.display(); } void makeBoxedText(int x, int y, String text, bool selected = false){ int offset = 3;//spacing between text and walls. the space above the text should be half of this. clearRect(x, y, x + 2*offset + 5*text.length() + text.length() - 1, y + 7 + offset); makeRect(x, y, x + 2*offset + 5*text.length() + text.length() - 1, y + 7 + offset, selected); makeText(x + offset, y + offset - 1, text, selected); display.display(); } int tearXCoords[] = { 5, 4, 6, 3, 7, 2, 8, 1, 9, 0, 10, 0, 10, 0, 10, 1, 9,// a //2, 3, 4, 5, 6, 7, 8, 3, 5, 7, }; int tearYCoords[] = { 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, // a //8, 8, 8, 8, 8, 8, 8, 9, 9, 9, //9, 9, 9, 9, }; void pixOn(int x, int y, bool w = true){ if (w){ display.drawPixel(x, y, WHITE); }else{ display.drawPixel(x, y, BLACK); } } void pixHor(int x0, int x1, int y, bool fill){ for (int i = x0; i < x1; i++){ pixOn(i,y, fill); } } void makeDrop(int x, int y, bool fill = false){ clearRect(x, y, x+10, y+12); for (int i = 1; i < 17; i+=2){ pixHor(tearXCoords[i], tearXCoords[i+1], y + tearYCoords[i], fill); } for (int i = 0; i < 30; i ++){ pixOn(x + tearXCoords[i], y + tearYCoords[i]); // Serial.println(String(tearXCoords[i]) + ", " + String(tearYCoords[i])); } } String mode = "default"; int option = 0; bool selecting = false; //String moistureLevel; int lastTime = 0; int dhtTemp; int dhtHum; bool takeReadings = true; int moistureLev; int senseDelay = 4; int curSec = 0; int drawDelay = 10;//ms ////////////////////////////////////////////////////////////////////////IMPLEMENT ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void loop() { //moistPerc = 10; DateTime now = rtc.now(); int curSec = now.second(); if (lastTime + senseDelay >= 60 && ((lastTime + senseDelay) - 60) >= curSec){ takeReadings = true; lastTime = curSec; } if ((curSec - lastTime >= senseDelay) && (lastTime + senseDelay <= 60)){ takeReadings = true; lastTime = curSec; } //Serial.println(curSec); String h = String(now.hour()); String m = String(now.minute()); String s = String(now.second()); if (h.length() < 2){ h = "0" + h; } if (m.length() < 2){ m = "0" + m; } if (s.length() < 2){ s = "0" + s; } String timestr = (h + ":" + m + ":" + s); //makeBoxedText(0,0,(String(now.hour()) + ":" + String(now.minute()) + ":" + String(now.second()))); makeText(0,0, timestr, false, true); if (takeReadings){ makeText(0,0, timestr); takeReadings = false; Serial.println("Reading"); dhtTemp = dht.readTemperature(true); makeText(0,0, timestr); dhtHum = dht.readHumidity(); moistureLev = analogRead(moisturePin); } //* float vboi = analogRead(potPin); int conVal = map(vboi, 0, 1020, 0, 7); for (int i = 0; i < OLED_WIDTH - 1; i++){ vals[i] = vals[i+1]; } vals[OLED_WIDTH - 1] = 7-conVal; //Serial.print((vboi/1023.0)*7.0); ////Serial.print(","); ////Serial.print(8.0*(analogRead(phPin)-700.0)/300.0);//(analogRead(phPin)/1023.0)*7.0); //Serial.print(","); //Serial.println(conVal); //boi.drawGraph();//////////////////// int pwmVal = map(vboi, 0, 1023, 0, 255); //*/ //makeDrop(94, 8, true); int moistLevelRead = int(100 - moistureLev/10.24); if (moistPerc > moistLevelRead){ digitalWrite(pumpControlPin, HIGH); makeDrop(94, 8); }else{ //makeDrop(94, 8); clearRect(94, 8, 108, 25); digitalWrite(pumpControlPin, LOW); } //digitalWrite(11, HIGH); pwmVal = map(pwmVal, 0, 255, lampMin, 255); //analogWrite(lampPin, pwmVal); //Serial.println(String(pwmVal) + ", " + String(sunPerc*255/100)); if (now.hour() > onHour && now.hour() < offHour){ analogWrite(lampPin, 255*sunPerc/100); }else{ analogWrite(lampPin, 0); } //Serial.println(255*sunPerc/100); /* Serial.print(dhtTemp); Serial.print(","); Serial.print(dhtHum); Serial.print(","); Serial.print(moistPerc); Serial.print(","); Serial.println(moistLevelRead); //*/ makeBoxedText(0, 8, "T", option == 0 && mode == "config"); makeBoxedText(14, 8, (String((dhtTemp))) + (char)247);//B11011111); makeBoxedText(0, 21, "H", option == 1 && mode == "config"); makeBoxedText(14, 21, (String((dhtHum))) + "%"); int rPos = 43; makeBoxedText(rPos, 8, "M", option == 2 && mode == "config"); String moistureLevel = String(int(100 - moistureLev/10.24)); if (selecting && option == 2){ moistureLevel = " " + String(moistPerc); } if (moistureLevel.length() < 3){ for (int i = 0; i < (4 - moistureLevel.length()); i++){ moistureLevel = " " + moistureLevel; //Serial.println("oof"); } } makeBoxedText(rPos + 14, 8, moistureLevel + "%"); makeBoxedText(rPos, 21, "S", option == 3 && mode == "config"); String sunPercStr = String(sunPerc); if (sunPercStr.length() < 3){ for (int i = 0; i < (4 - sunPercStr.length()); i++){ sunPercStr = " " + sunPercStr; } } makeBoxedText(rPos + 14, 21, sunPercStr + "%"); //makeDrop(94, 8); if (digitalRead(configButtonPin) == HIGH && !configSt){ configSt = true; if (mode != "config"){ mode = "config"; option = 0; }else{ mode = "default"; } }else if (digitalRead(configButtonPin) == LOW && configSt){ configSt = false; } if (mode == "config"){ if (digitalRead(upButtonPin) == HIGH && !upSt && !selecting){ upSt = true; if (option == 1){ option = 0; }else if (option == 3){ option = 2; } }else if (digitalRead(upButtonPin) == LOW && upSt){ upSt = false; } if (digitalRead(downButtonPin) == HIGH && !downSt && !selecting){ downSt = true; if (option == 0){ option = 1; }else if (option == 2){ option = 3; } }else if (digitalRead(downButtonPin) == LOW && downSt){ downSt = false; } if (digitalRead(leftButtonPin) == HIGH && !leftSt && !selecting){ leftSt = true; if (option == 2){ option = 0; }else if (option == 3){ option = 1; } }else if (digitalRead(leftButtonPin) == LOW && leftSt){ leftSt = false; } if (digitalRead(rightButtonPin) == HIGH && !rightSt && !selecting){ rightSt = true; if (option == 0){ option = 2; }else if (option == 1){ option = 3; } }else if (digitalRead(rightButtonPin) == LOW && rightSt){ rightSt = false; } if (digitalRead(selectButtonPin) == HIGH && !selectSt){ selectSt = true; selecting = !selecting; }else if (digitalRead(selectButtonPin) == LOW && selectSt){ selectSt = false; } if (selecting){ if (option == 2){ if (digitalRead(leftButtonPin) == HIGH && !leftSt){ leftSt = true; if (moistPerc > 0){ moistPerc -= 1; } }else if (digitalRead(leftButtonPin) == LOW && leftSt){ leftSt = false; } if (digitalRead(rightButtonPin) == HIGH && !rightSt){ rightSt = true; if (moistPerc < 25){ moistPerc += 1; } }else if (digitalRead(rightButtonPin) == LOW && rightSt){ rightSt = false; } EEPROM.write(moistAddr, moistPerc); } if (option == 3){ if (digitalRead(leftButtonPin) == HIGH && !leftSt){ leftSt = true; if (sunPerc >= 5){ sunPerc -= 5; }else{ sunPerc = 0; } }else if (digitalRead(leftButtonPin) == LOW && leftSt){ leftSt = false; } if (digitalRead(rightButtonPin) == HIGH && !rightSt){ rightSt = true; if (sunPerc <= 95){ sunPerc += 5; }else{ sunPerc = 100; } }else if (digitalRead(rightButtonPin) == LOW && rightSt){ rightSt = false; } EEPROM.write(sunAddr, sunPerc); } } }else{ selecting = false; } }