//OLED Board //SAMD21E17A #include #include #include #include //Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) Adafruit_SSD1306 myOLED(128, 32, &Wire, 0); //Changed reset pin to 0 (from -1) to avoid having to push code to board everytime it was connected to power int days; //Variables to be display time on int hours; int minutes; int seconds; int secondsTotal; //Total time passed since last watered int oldCount = 0; //For saving old word count int newCount; //For updating word count int progress; String message; //Variable to receive serial communication const char endOfNumberDelimiter = '>'; void setup() { Serial.begin(115200); Serial1.begin(115200); if (!myOLED.begin(SSD1306_SWITCHCAPVCC, 0x3C)){ //I2C OLED address 0x3C for 128x64 Serial.println(F("SSD1306 allocation failed")); for(;;); // Don't proceed, loop forever } welcome(); welcome2(); welcome3(); myOLED.clearDisplay(); } void loop() { if (Serial1.available()) { message = Serial1.readStringUntil('>'); //Serial.print("message"); debug //Serial.println(message); debug newCount = message.toInt(); //Display wordcount when input through serial monitor & update last watered if (message!="Button"){ progress = newCount - oldCount; myOLED.clearDisplay(); myOLED.setTextSize(1); myOLED.setTextColor(WHITE); myOLED.setCursor(0, 0); myOLED.println("WordCount "); myOLED.setCursor(0, 8); myOLED.print("Previous: "); myOLED.println(oldCount); myOLED.setCursor(0, 16); myOLED.print("New: "); myOLED.println(newCount); myOLED.setCursor(0, 24); myOLED.print("Progress: "); myOLED.print(progress); myOLED.println(" words"); myOLED.display(); // Show text oldCount = newCount; delay(4000); if (progress > 0) { secondsTotal=0; celebrate(); } else { pressure(); } } //Display feedback when input through button & update last watered else { Serial.print("button"); myOLED.clearDisplay(); myOLED.setTextSize(1); myOLED.setTextColor(WHITE); myOLED.setCursor(0, 0); myOLED.println("Button Pressed"); myOLED.setCursor(0, 8); myOLED.print("Input Recorded"); myOLED.display(); secondsTotal=0; delay(3000); celebrate(); } } //Display last watered else { while (Serial1.available() == 0) { delay (1000); //Adding time in 1 second periods secondsTotal++; days = (((secondsTotal/60)/60)/24)%7; hours = ((secondsTotal/60)/60)%24; minutes = (secondsTotal/60)%60; seconds = secondsTotal%60; //Time since last watered Days:HH:MM:SS myOLED.clearDisplay(); myOLED.setTextColor(WHITE); myOLED.setTextSize(1); myOLED.setCursor(0,0); myOLED.print("Last Watered"); myOLED.setCursor(0,8); myOLED.print("Day:HH:MM:SS"); myOLED.setCursor(0,16); print3digits(days); myOLED.print(":"); print2digits(hours); myOLED.print(":"); print2digits(minutes); myOLED.print(":"); print2digits(seconds); myOLED.display(); } } } void welcome(void) { myOLED.clearDisplay(); myOLED.setTextSize(1); // Normal 1:1 pixel scale myOLED.setTextColor(SSD1306_WHITE); // Draw white text myOLED.setCursor(0,0); // Start at top-left corner myOLED.println(F("PLANT PAL")); myOLED.display(); delay(2000); } void welcome2(void) { myOLED.clearDisplay(); myOLED.setTextSize(2); // Draw 2X-scale text myOLED.setTextColor(SSD1306_WHITE); myOLED.setCursor(10, 0); myOLED.println(F("Plant Pal")); myOLED.display(); // Show initial text delay(100); // Scroll in various directions, pausing in-between: myOLED.startscrollright(0x00, 0x0F); delay(2000); myOLED.stopscroll(); delay(1000); myOLED.startscrollleft(0x00, 0x0F); delay(2000); myOLED.stopscroll(); delay(1000); myOLED.startscrolldiagright(0x00, 0x07); delay(2000); myOLED.startscrolldiagleft(0x00, 0x07); delay(2000); myOLED.stopscroll(); delay(1000); } void welcome3(void) { myOLED.clearDisplay(); myOLED.setTextSize(1); myOLED.setCursor(42,0); myOLED.println(F("Let the")); myOLED.setCursor(25,8); myOLED.println(F("task tending")); myOLED.setCursor(38,16); myOLED.println(F("commence!")); myOLED.display(); delay(3000); } void celebrate(void) { myOLED.clearDisplay(); myOLED.setTextSize(2); // Draw 2X-scale text myOLED.setTextColor(SSD1306_WHITE); myOLED.setCursor(10, 0); myOLED.println(F("GOOD JOB!")); myOLED.display(); // Show initial text delay(1000); // Scroll in various directions, pausing in-between: myOLED.startscrolldiagright(0x00, 0x07); delay(2000); myOLED.startscrolldiagleft(0x00, 0x07); delay(2000); myOLED.stopscroll(); delay(1000); myOLED.clearDisplay(); myOLED.setTextSize(1); // Draw 2X-scale text myOLED.setTextColor(SSD1306_WHITE); myOLED.setCursor(30, 0); myOLED.println(F("Keep Going!")); myOLED.setCursor(12, 16); myOLED.println(F("(Resetting Clock)")); myOLED.display(); // Show initial text delay(3000); } void pressure(void) { myOLED.clearDisplay(); myOLED.setTextSize(1); myOLED.setTextColor(SSD1306_WHITE); myOLED.setCursor(0,0); myOLED.println(F("Umm.. that's okay,")); myOLED.setCursor(0,8); myOLED.println(F("you can do it!")); myOLED.setCursor(0,24); myOLED.println(F("(for my sake?)")); myOLED.display(); delay(3000); } //Function to write two digits in the time counter even if the time to be represented is only one digit void print2digits(int number) { if (number >= 0 && number < 10) { myOLED.write('0'); } myOLED.print(number); } //Function to write three digits in the days counter void print3digits(int number) { if (number >=0 && number < 10) { myOLED.write("00"); } if (number >=99 && number < 1000) { myOLED.write('0'); } myOLED.print(number); }