-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ino
212 lines (176 loc) · 5.24 KB
/
main.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <Arduino.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1351.h>
#include <U8g2lib.h> //fonts https://lopaka.app/
#include <U8x8lib.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include "PHMeter.h"
#include "CTimer.h"
#include "PushButton.h"
//#include "config.h"
#define DEBUG 1
#if DEBUG == 1
#define debug(x) Serial.print(x)
#define debugln(x) Serial.println(x)
#else
#define debug(x)
#define debugln(x)
#endif
#define SCLK 52 //yellow
#define MOSI 51 //blue
#define DC_PIN 9 //green
#define CS_PIN 53 //orange
#define RST_PIN 8 //white
#define PH_PIN A0
#define TDS_PIN A1
#define TEMP_PIN A2
#define I2C_SDA 20
#define I2C_SCL 21
//#define I2nd_SCL 21
//#define I2nd_SDA 20
#define BUTTON_START_STOP 30
#define BUTTON_MIN_PLUS 31
#define BUTTON_MIN_MINUS 32
#define BUTTON_RESET 33
#define BUZZER_PIN 25
#define chipSelect 10
#define BLACK 0x0000
bool calibrationButtonPressed = false;
constexpr int OLED_SCREEN_WIDTH = 128;
constexpr int OLED_SCREEN_HEIGHT = 128;
constexpr int I2C_SCREEN_WIDTH = 128;
constexpr int I2C_SCREEN_HEIGHT = 64;
//U8G2_SSD1309_128X64_NONAME2_F_HW_I2C i2nd(U8G2_R0, U8X8_PIN_NONE, I2nd_SCL, I2nd_SDA);
extern const uint16_t RED, ORANGE, WHITE, BLUE;
const int SEGMENT_HEIGHT = SCREEN_HEIGHT / 4;
const int NUMBER_Y_POS = SCREEN_HEIGHT - 10;
constexpr int VISIBLE_SEGENTS = 5;
const long interval = 1000;
static float lastPH = -1;
const int buttonPins[4] = { BUTTON_START_STOP, BUTTON_MIN_PLUS, BUTTON_MIN_MINUS, BUTTON_RESET };
const unsigned long timeAdjustmentAmount = 60000;
float calibrationSlope = 0.0;
float calibrationIntercept = 0.0;
Adafruit_SSD1351 oled = Adafruit_SSD1351(SCREEN_WIDTH, SCREEN_HEIGHT, &SPI, CS_PIN, DC_PIN, RST_PIN);
PHMeter pH(PH_PIN, calibrationSlope, calibrationIntercept, oled, BUTTON_MIN_PLUS, BUTTON_MIN_MINUS);
U8G2_SSD1309_128X64_NONAME2_F_HW_I2C i2c(U8G2_R0, U8X8_PIN_NONE, I2C_SCL, I2C_SDA);
unsigned long previousMillis = 0;
const int tdsPin = A1;
CTimer timer(60000, i2c);
const int pinSS = 30; // Start/Stop button
const int pinMP = 31; // Minute Plus
const int pinMM = 32; // Minute Minus
const int pinRT = 33; // Reset
PushButton buttons(pinSS, pinMP, pinMM, pinRT, true);
//************************************************************************playBeep
void playBeep() {
for (int i = 0; i < 3; i++) {
tone(BUZZER_PIN, 750 + i * 250, 200);
delay(200);
}
}
void drawScaleSegment(int segment, int startY, bool topRow);
void drawHighlightBox(float pH, int scaleStartY, int scaleRowHeight, int rowGap);
//***************************************************************************Setup
void setup() {
Serial.begin(115200);
Wire.begin();
oled.begin();
oled.fillScreen(BLACK);
i2c.begin();
pH.begin();
buttons.init();
static bool timerStarted = false;
if (!timerStarted) {
timer.start();
timerStarted = true;
}
if (calibrationButtonPressed) {
pH.calibratePH();
}
if (!SD.begin(chipSelect)) {
Serial.println("Initialization failed!");
return;
}
buttons.onSSPressed(onStartStopPressed);
buttons.onMPPressed(onMinutePlusPressed);
buttons.onMMPressed(onMinuteMinusPressed);
buttons.onRTPressed(onResetPressed);
}
//****************************************************************************Loop
void loop() {
static bool beepPlayed = false;
buttons.checkButtons();
timer.update();
updateDisplay();
unsigned long currentMillis = millis();
previousMillis = currentMillis;
float currentPH = pH.readPH();
if (currentPH != lastPH) {
lastPH = currentPH;
pH.displayPH(currentPH);
pH.drawPHScale(currentPH);
}
Serial.print("Calibrated pH: ");
Serial.println(currentPH, 2);
if (!timer.isRunning() && timer.getCountdownTime() == 0 && !beepPlayed) {
playBeep();
beepPlayed = true;
}
if (timer.isRunning() || timer.getCountdownTime() == 60000) {
beepPlayed = false;
}
}
void onStartStopPressed() {
if (timer.isRunning()) {
timer.stop();
Serial.println("Timer Stopped");
} else {
timer.start();
Serial.println("Timer Started");
}
}
void onMinutePlusPressed() {
if (!timer.isRunning()) {
Serial.println("Adding time...");
timer.addTime(60000);
}
}
void onMinuteMinusPressed() {
if (!timer.isRunning() && timer.getCountdownTime() > 60000) {
Serial.println("Subtracting time...");
timer.subtractTime(60000);
}
}
void onResetPressed() {
timer.reset();
}
//*******************************************************************updateDisplay
void updateDisplay() {
i2c.clearBuffer();
unsigned long lastSetTime = timer.getLastSetTime();
int setMinutes = lastSetTime / 60000;
int setSeconds = (lastSetTime % 60000) / 1000;
char buffer[20];
sprintf(buffer, "Set Time: %02d:%02d", setMinutes, setSeconds);
i2c.setFont(u8g2_font_profont15_tr);
i2c.drawStr(5, 10, buffer);
unsigned long currentTime = timer.getCountdownTime();
int currentMinutes = currentTime / 60000;
int currentSeconds = (currentTime % 60000) / 1000;
sprintf(buffer, "%02d:%02d", currentMinutes, currentSeconds);
i2c.setFont(u8g2_font_profont29_tr);
i2c.drawStr(25, 45, buffer);
i2c.sendBuffer();
}
// void tdsMeter(){
// int tdsValue = analogRead(tdsPin);
// Serial.print("TDS: ");
// Serial.print(tdsValue);
// Serial.println(" ppm");
// delay(1000);
//}
// void tempSensor(){
// }