Projects with Sam
5000 Club Member
View BadgesPartner Member 2024
Hospitality Award
Midwest Reefer
Rock Pool Reef Keepers
My Tank Thread
My Aquarium Showcase
ParlaPHMonitor code:
CSS:
#include <Wire.h>
#include "SPI.h"
#include "TFT_22_ILI9225.h"
#include <SimpleTimer.h>
#include <../fonts/FreeSansBold24pt7b.h>
#include <../fonts/FreeSans12pt7b.h>
#define TFT_RST 8
#define TFT_RS 9
#define TFT_CS 10 // SS
#define TFT_SDI 11 // MOSI
#define TFT_CLK 13 // SCK
#define TFT_LED 3 // LED
#define TFT_BRIGHTNESS 200 //Initial brightness of TFT backlight
TFT_22_ILI9225 tft = TFT_22_ILI9225(TFT_RST, TFT_RS, TFT_CS, TFT_LED, TFT_BRIGHTNESS);
//Variables and constants
#define buzzer 5 //buzzer to arduino pin 5
int16_t x, y, w, h;
SimpleTimer timer;
#define PH_PROBE A1
float calibration_value = 21.5;
float ph_act;
unsigned long int avgval;
int buffer_arr[60], temp;
// Setup
void setup() {
pinMode(buzzer, OUTPUT); // Set buzzer - pin 5 as an output
Serial.begin(9600);
Serial.println("\n************ ParlaPHMonitor Starting ************");
while (!Serial) {} // wait for serial port to connect.
//setup LCD and draw home screen
tft.begin();
tft.clear();
tft.setOrientation(3);
tft.setBacklight(HIGH);
tft.setBackgroundColor(COLOR_BLACK);
tft.drawRectangle(5, 5, tft.maxX() - 5, tft.maxY() - 5, COLOR_WHITE); //draw rectangle around the screen
tft.setGFXFont(&FreeSans12pt7b); // Set title font
tft.drawGFXText(37, 155, "ParlAquatics", COLOR_BLUE); //Print title string
timer.setInterval(250L, updateUI); //We setup the callback function to update the UI.
}
// Loop
void loop() {
//Get the PH from the analog pin. average it over a number of readings.
for (int i = 0; i < 60; i++) { //Take 60 samples
buffer_arr[i] = analogRead(PH_PROBE); //A0
delay(90);
}
for (int i = 0; i < 59; i++) {
for (int j = i + 1; j < 60; j++) {
if (buffer_arr[i] > buffer_arr[j]) { //Sort the values
temp = buffer_arr[i];
buffer_arr[i] = buffer_arr[j];
buffer_arr[j] = temp;
}
}
}
avgval = 0;
for (int i = 29; i < 49; i++) //Use only the middle 20 values
avgval += buffer_arr[i];
float volt = (float)avgval * 5.0 / 1024 / 20;
ph_act = -5.70 * volt + calibration_value, 2;
Serial.println("pH Val: " + String(ph_act,2));
timer.run(); // Initiates SimpleTimer which updates the UI]
//Ring buzzer if Kalkwasser PH is low:
// if ( ph_act > 10 ) {
// noTone(buzzer); //Stop sound
// } else {
// tone(buzzer, 1000, 100); //Send 1KHz sound signal
// }
delay(1000);
}
// Callback to update the UI
void updateUI() {
//Setup screen
tft.drawRectangle(5, 5, tft.maxX() - 5, tft.maxY() - 5, COLOR_WHITE); //draw rectangle around the screen
tft.setGFXFont(&FreeSans12pt7b); // Set title font
if ( ph_act > 10 ) {
tft.drawGFXText(25, 35, "Kalkwasser pH:", COLOR_WHITE); //Print title string
} else {
tft.drawGFXText(25, 35, "Kalkwasser pH:", COLOR_RED); //Print title string
}
//Setup Readings
x=60;
y=70;
String s1 = String(ph_act,2);
Serial.println("UpdateUI with pH Val: " + s1);
tft.setGFXFont(&FreeSansBold24pt7b); // Set current font
tft.getGFXTextExtent(s1, x, y, &w, &h); // Get string extents
y += h; // Set y position to string height
tft.fillRectangle( 10, 45, tft.maxX()-10, 125, COLOR_BLACK);
if ( ph_act > 10 && ph_act != 0) {
tft.drawGFXText(x, y, s1, COLOR_WHITE); // Print string
} else if ( ph_act != 0) {
tft.drawGFXText(x, y, s1, COLOR_RED); // Print string
} else {
tft.setGFXFont(&FreeSans12pt7b); // Set font
tft.drawGFXText(x, y, "calculating", COLOR_GRAY); // Print string
}
tft.setGFXFont(&FreeSans12pt7b); // Set title font
tft.drawGFXText(37, 155, "ParlAquatics", COLOR_BLUE); //Print title string
}