1#include <SPI.h>
2#include <Wire.h>
3#include <Adafruit_GFX.h>
4#include <Adafruit_SSD1306.h>
5#include <OneWire.h>
6#include <DallasTemperature.h>
7#include <Servo.h>
8#include "RTClib.h"
9
10#define pButton 2
11#define pPowerStrip 8
12#define pTemp 10
13#define pStanServo 11
14#define pLED 12
15
16#define stanServoPos 0
17#define stanServoPos2 180
18
19#define daysToNext25Change 14
20
21Servo stanServo;
22
23
24Adafruit_SSD1306 display(-1);
25
26
27OneWire oneWire(pTemp);
28
29
30DallasTemperature sensors(&oneWire);
31
32RTC_DS3231 rtc;
33
34char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
35
36int buttonPresses;
37int futureDay, futureMonth, futureYear;
38int changePercent;
39
40void setup() {
41
42 Serial.begin(9600);
43
44 stanServo.attach(pStanServo, 500, 2500);
45
46
47 sensors.begin();
48
49
50 pinMode(pButton, INPUT);
51 pinMode(pPowerStrip, OUTPUT);
52 pinMode(pTemp, INPUT);
53 pinMode(pStanServo, OUTPUT);
54 pinMode(pLED, OUTPUT);
55
56
57 display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
58
59
60 display.clearDisplay();
61
62 delay(3000);
63
64 if (! rtc.begin()) {
65 Serial.println("Couldn't find RTC");
66 while (1);
67 }
68
69
70
71 rtc.adjust(DateTime(2022, 10, 9, 7, 23, 10));
72
73
74
75
76
77}
78
79void loop() {
80
81 if (sensors.getTempFByIndex(0) < 76) {
82
83 digitalWrite(pPowerStrip, HIGH);
84
85 }
86
87 DateTime now = rtc.now();
88
89 display.setTextColor(WHITE);
90 display.clearDisplay();
91
92
93 display.setTextSize(1);
94 display.setCursor(0,0);
95 display.print(daysOfTheWeek[now.dayOfTheWeek()]);
96 char currentDate [16];
97 uint8_t thisDay, thisMonth ;
98 thisDay = now.day();
99 thisMonth = now.month();
100 sprintf (currentDate, "%02d/%02d/", thisMonth, thisDay);
101 display.setTextSize(1);
102 display.setCursor(62,0);
103 display.print(currentDate);
104 display.setTextSize(1);
105 display.setCursor(102,0);
106 display.print(now.year(), DEC);
107
108
109 char buffer [16];
110 uint8_t thisSec, thisMin, thisHour;
111 thisSec = now.second();
112 thisMin = now.minute();
113 thisHour = now.hour();
114 sprintf (buffer, "%02d:%02d:%02d", thisHour, thisMin, thisSec);
115 display.setTextSize(2);
116 display.setCursor(15,12);
117 display.print(buffer);
118
119
120 sensors.requestTemperatures();
121 display.setCursor(7,30);
122 display.setTextSize(1);
123 display.print("Water Temp: ");
124 display.print(sensors.getTempFByIndex(0));
125 display.print((char)247);
126 display.print("F");
127
128 if (digitalRead(pButton) == HIGH) {
129
130 DateTime future(now + TimeSpan(14,0,0,0));
131
132 futureDay = future.day();
133 futureMonth = future.month();
134 futureYear = future.year();
135
136 buttonPresses++;
137 digitalWrite(pLED, LOW);
138
139 }
140
141 if (buttonPresses % 52 == 0 && buttonPresses != 0) {
142
143 changePercent = 100;
144
145 }
146
147 else if (buttonPresses % 2 == 0 && buttonPresses != 0) {
148
149 changePercent = 50;
150
151 }
152
153 else if (buttonPresses != 0) {
154
155 changePercent = 25;
156
157 }
158
159
160 display.setCursor(22, 40);
161 display.print(changePercent);
162 display.print("% change on:");
163 char futureDate [16];
164 sprintf (futureDate, "%02d/%02d/", futureMonth, futureDay);
165 display.setCursor(35, 50);
166 display.print(futureDate);
167 display.print(futureYear);
168
169 if (now.year() == futureYear && thisMonth == futureMonth && thisDay == futureDay) {
170
171 digitalWrite(pLED, HIGH);
172
173 }
174
175 if (sensors.getTempFByIndex(0) > 78) {
176
177 digitalWrite(pPowerStrip, LOW);
178
179 }
180
181 if (thisHour == 12 && thisMin == 0 && thisSec < 5) {
182
183 stanServo.write(stanServoPos);
184
185 }
186
187 else {
188
189 stanServo.write(stanServoPos2);
190
191 }
192
193 display.display();
194
195}