//#include <Wire.h>
#include <RTClib.h>
#include <LedControl.h>
//MATRIX DISPLAY
LedControl lc = LedControl(12, 11, 10, 4); //DIN, CLK, CS, count of displays
RTC_DS3231 rtc;
float tPC;
unsigned long initialHighTide; // Initial high tide timestamp
int oldColumnsToLight;
void setup() {
Serial.begin(9600);
// Start Wire library for I2C
//Wire.begin();
//we have already set the number of devices when we created the LedControl
int devices=lc.getDeviceCount();
//we have to init all devices in a loop
for(int address=0;address<devices;address++) {
/*The MAX72XX is in power-saving mode on startup*/
lc.shutdown(address,false);
/* Set the brightness to a medium values */
lc.setIntensity(address,0);
/* and clear the display */
lc.clearDisplay(address);
}
/* Set the brightness to a medium values */
//lc.setIntensity(0,15);
//lc.setIntensity(1,15);
//lc.setIntensity(2,15);
//lc.setIntensity(3,8);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
// Set initial high tide time (replace with actual values)
// Format: Year, Month, Day, Hour, Minute, Second
//initialHighTide = DateTime(2025, 3, 1, 5, 29, 0).unixtime(); //Penzance
initialHighTide = DateTime(2025, 8, 27, 2, 22, 0).unixtime(); //Saltdean
}
void loop() {
DateTime now = rtc.now();
unsigned long currentTime = now.unixtime();
unsigned long elapsedTime = (currentTime - initialHighTide) % 44700;
//unsigned long elapsedTime = 10000;
if (elapsedTime >= 22350 && elapsedTime < 44700) {
tPC = elapsedTime - 22350;
tPC = tPC / 22350;
tPC = tPC * 100;
updateDisplay(tPC, true);
}
if (elapsedTime >= 0 && elapsedTime < 22350) {
tPC = 22350 - elapsedTime;
tPC = tPC / 22350;
tPC = tPC * 100;
updateDisplay(tPC, false);
}
Serial.print("Current time: ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
//Serial.print("Initial High Tide: ");
//Serial.println(initialHighTide);
Serial.print("Tide height: ");
Serial.print(tPC);
Serial.println("%");
Serial.print("Elapsed Time: ");
Serial.println(elapsedTime);
if (elapsedTime >= 41906.5 && elapsedTime < 44700) {
//setLights(true, false, false); // High Tide (Red)
Serial.println("High Tide");
//display.println(F("High Tide"));
}
else if (elapsedTime >= 25143.5 && elapsedTime < 41906.5) {
//setLights(true, true, false); // Rising Tide (Red + Yellow)
Serial.println("Rising Tide");
//display.println(F("Rising Tide"));
}
else if (elapsedTime >= 19556.5 && elapsedTime < 25143.5) {
//setLights(false, false, true); // Low Tide (Green)
Serial.println("Low Tide");
//display.println(F("Low Tide"));
}
else if (elapsedTime >= 2793.5 && elapsedTime < 19556.5) {
//setLights(false, true, true); // Falling Tide (Green + Yellow)
Serial.println("Falling Tide");
//display.println(F("Falling Tide"));
}
else if (elapsedTime >= 0 && elapsedTime < 2793.5) {
//setLights(true, false, false); // High Tide (Red)
Serial.println("High Tide");
//display.println(F("High Tide"));
}
else {
//setLights(false, false, false);
Serial.println("Something's wrong");
//display.println(F("Something's wrong"));
}
delay(1000);
}
void updateDisplay(float percent, bool rising) {
int columnsToLight = round((percent / 100.0) * 32); // Convert percent to column count
if (columnsToLight == 0) {
columnsToLight = 1;
}
if (columnsToLight != oldColumnsToLight) {
// Clear all displays before updating
for (int i = 0; i < 4; i++) {
lc.clearDisplay(i);
}
}
// Light up columns from bottom to top
for (int i = 0; i < columnsToLight; i++) {
int device = i / 8; // Determine which device (0 to 3)
int col = 7 - (i % 8); // Map to correct column (right to left)
lc.setColumn(device, col, 0xFF); // Light entire column on that device
}
oldColumnsToLight = columnsToLight;
// Light the very top column if percentage is rising
if (rising) {
lc.setColumn(3, 0, 0xFF); // Light the topmost column (leftmost column of top device)
//} else {
// lc.setColumn(0, 7, 0xFF); // Light the lowest column (leftmost column of top device)
}
Serial.print("Columns to light: ");
Serial.println(columnsToLight);
Serial.print("Old columns to light: ");
Serial.println(oldColumnsToLight);
}