/* Tide_calculator.ino
Copyright (c) 2019 Luke Miller
This code calculates the current tide height for the
pre-programmed site. It requires a real time clock
(DS1307 or DS3231 chips) to generate a time for the calculation.
The site is set by the name of the included library (see line 44 below)
Written under version 1.6.4 of the Arduino IDE.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTIBILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/
The harmonic constituents used here were originally derived from
the Center for Operational Oceanic Products and Services (CO-OPS),
National Ocean Service (NOS), National Oceanic and Atmospheric
Administration, U.S.A.
The data were originally processed by David Flater for use with XTide,
available at http://www.flaterco.com/xtide/files.html
As with XTide, the predictions generated by this program should
NOT be used for navigation, and no accuracy or warranty is given
or implied for these tide predictions. The chances are pretty good
that the tide predictions generated here are completely wrong.
It is highly recommended that you verify the output of these predictions
against the relevant NOAA tide predictions online.
*/
//--------------------------------------------------------------
//Initial setup
//Header files for talking to real time clock
#include <Wire.h> // Required for RTClib
#include <SPI.h> // Required for RTClib to compile properly
#include <RTClib.h> // From https://github.com/millerlp/RTClib
// Real Time Clock setup
RTC_DS1307 RTC; // Uncomment when using this chip
// RTC_DS3231 RTC; // Uncomment when using this chip
// Tide calculation library setup.
// Change the library name here to predict for a different site.
#include "TidelibHydeParkHudsonRiverNewYork.h"
// Other sites available at http://github.com/millerlp/Tide_calculator
TideCalc myTideCalc; // Create TideCalc object called myTideCalc
int currMinute;
float results;
void printTime(DateTime now); // Function declaration
DateTime findNextHighTide(DateTime startTime); // Custom function
void setup(void)
{
Wire.begin();
RTC.begin();
Serial.begin(57600);
DateTime now = RTC.now();
currMinute = now.minute();
printTime(now);
Serial.println("Calculating tides for: ");
Serial.print(myTideCalc.returnStationID());
Serial.print(" ");
Serial.println(myTideCalc.returnStationIDnumber());
DateTime nextHigh = findNextHighTide(now);
Serial.println("Next high tide at: ");
printTime(nextHigh);
Serial.println();
delay(2000);
}
void loop(void)
{
DateTime now = RTC.now();
if (now.minute() != currMinute) {
currMinute = now.minute();
Serial.println();
printTime(now);
DateTime nextHigh = findNextHighTide(now);
Serial.println("Next high tide at: ");
printTime(nextHigh);
Serial.println();
}
}
//**********************************************************
// Finds the next high tide by checking when tide stops rising
DateTime findNextHighTide(DateTime startTime) {
const int interval = 5; // minutes between checks
const int maxLookaheadHours = 48; // stop if no high tide found
DateTime prevTime = startTime;
DateTime currTime = startTime + TimeSpan(0, 0, interval, 0);
float prevTide = myTideCalc.currentTide(prevTime);
float currTide = myTideCalc.currentTide(currTime);
while ((currTime - startTime).totalseconds() < maxLookaheadHours * 3600) {
if (prevTide < currTide) {
// Rising: keep looking
prevTime = currTime;
prevTide = currTide;
currTime = currTime + TimeSpan(0, 0, interval, 0);
currTide = myTideCalc.currentTide(currTime);
} else {
// Tide is now falling, so previous time was the peak
return prevTime;
}
}
// If no high tide found in time range, return start time
return startTime;
}
//**********************************************************
void printTime(DateTime now) {
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(":");
if (now.minute() < 10) Serial.print("0");
Serial.print(now.minute()); Serial.print(":");
if (now.second() < 10) Serial.print("0");
Serial.println(now.second());
}