Basically this function will return a speed value you can pass to your WaveMaker or Vortech RF functions or whatever else you would like to do based on a tidal effect. You pass the minimum speed you want, the maximum speed you want and also the minimum gap you want between high and low tide.
Currently the wavelength is set to 12 hours so you get a high-tide and low-tide every 12 hours. The amplitude of the wave is affected by MoonPhase as well, so over the course of the lunar month, the difference between high and low tide will increase and decrease.
For future plans, I want to add some random variation to the wavelength and amplitude as well so that it's not as constant. I also plan on adding a helper function to track if current is incoming, or outgoing so I can set my pumps to switch when the current switches as well.
I have not yet tested from the Arduino yet as it will take a lot of reconfiguration for me to use it, but I've compiled the functions standalone with gcc and charted them in Excel and they do make a nice variabe wave output. I'll try and paste a screenshot of a 28 day cycle.
Anyway, here's the code:
Code: Select all
#define PI 3.141593
/*
Spring tides occur during the full moon and the new moon.
At these times, the high tides are very high and the low tides are very low.
Neap tides occur during quarter moons.
The result is a smaller difference between high and low tides
MoonPhase 0-25 = Spring
MoonPhase 26-74 = Neap
MoonPhase 75-100 = Spring
So, the effect of the Moon will be a cosine wave.
*/
Code: Select all
int calcTide(byte minSpeed, byte maxSpeed, byte minGap) {
int speed;
int range = maxSpeed-minSpeed-minGap;
double wl=60*60*12; // wavelength (12 hours) (to be randomized..)
double gap; // tide gap between high and low
double result; // tide
// Calculate the gap between high and low tide based on MoonPhase()
gap=.5+(cos(((2*PI)/100)*MoonPhase())/2);
gap=minGap+(range*gap);
// Find out the current tidal height
result=.5+(sin(((2*PI)/wl)*now())/2);
// Adjust the calculate speed to be in our adjusted range
speed=minSpeed+(result*gap)+(range-(gap/2));
return speed;
}