Help with Code

Share you PDE file with our community
Post Reply
najluni15
Posts: 60
Joined: Sat Mar 10, 2012 6:47 pm

Help with Code

Post by najluni15 »

Hi there,

I am having a couple of issues with my code -

1. My ATO is not working properly. I have two floats in my sump, one high, and one low. I bought a new 5 gallon ATO reservoir so I would not need to deal with refilling all the time and it doesn't seem to be working. Is there something wrong with my code that makes it so the ATO is not triggered?

2. A while back my ph probe didn't seem to be working. I manually calibrated it by putting in values into the code. Now I got calibration fluid and got true readings but when I calibrate it with the reef angel head unit, my ph is reading way off....am I doing something wrong in my code that I did before and cant remember?

Here is my code

// RA_111312_2128.ino
//
// This version designed for v0.9.0 or later

/* The following features are enabled for this File:
#define DisplayImages
#define ATOSetup
#define DisplayLEDPWM
#define RelayExp
#define InstalledRelayExpansionModules 8
#define PWMEXPANSION
*/


#include <ReefAngel_Features.h>
#include <Globals.h>
#include <RA_Wifi.h>
#include <Wire.h>
#include <OneWire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <InternalEEPROM.h>
#include <RA_NokiaLCD.h>
#include <RA_ATO.h>
#include <RA_Joystick.h>
#include <LED.h>
#include <RA_TempSensor.h>
#include <Relay.h>
#include <RA_PWM.h>
#include <Timer.h>
#include <Memory.h>
#include <PH.h>
#include <ReefAngel.h>

byte x,y;
char text[10];

void DrawCustomMain()
{
// the graph is drawn/updated when we exit the main menu &
// when the parameters are saved
ReefAngel.LCD.Clear(BtnActiveColor,5,0,127,11);
ReefAngel.LCD.DrawText(DefaultBGColor,BtnActiveColor,30,3,"My Reef Angel");
ReefAngel.LCD.DrawDate(6, 122);
pingSerial();
ReefAngel.LCD.DrawMonitor(15, 20, ReefAngel.Params,
ReefAngel.PWM.GetDaylightValue(), ReefAngel.PWM.GetActinicValue());
pingSerial();
ReefAngel.LCD.Clear(DefaultFGColor,5,52,127,52);
ReefAngel.LCD.DrawText(COLOR_DARKGOLDENROD,DefaultBGColor,30,55,"PWM Expansion");
x=15;
y=68;
for (int a=0;a<6;a++)
{
if (a>2) x=75;
if (a==3) y=68;
ReefAngel.LCD.DrawText(COLOR_DARKGOLDENROD,DefaultBGColor,x,y,"Ch :");
ReefAngel.LCD.DrawText(COLOR_DARKGOLDENROD,DefaultBGColor,x+12,y,a);
ConvertNumToString(text, ReefAngel.PWM.GetChannelValue(a), 1);
strcat(text," ");
ReefAngel.LCD.DrawText(COLOR_DARKGOLDENROD,DefaultBGColor,x+24,y,text);
y+=10;
}
pingSerial();
byte TempRelay = ReefAngel.Relay.RelayData;
TempRelay &= ReefAngel.Relay.RelayMaskOff;
TempRelay |= ReefAngel.Relay.RelayMaskOn;
ReefAngel.LCD.DrawOutletBox(12, 103, TempRelay);
}

void DrawCustomGraph()
{
}



void setup()
{
ReefAngel.Init(); //Initialize controller
ReefAngel.AddDateTimeMenu();
ReefAngel.PHMin=298;// 298=PH7.0
ReefAngel.PHMax=580;// 580=PH10.0
ReefAngel.FeedingModePorts = Port1Bit |Port2Bit | Port4Bit | Port5Bit | Port6Bit;
ReefAngel.WaterChangePorts = Port1Bit|Port2Bit | Port4Bit | Port5Bit |Port6Bit | Port7Bit;
ReefAngel.LightsOnPorts = 0;

// Ports that are always on
ReefAngel.Relay.On(Port2);
ReefAngel.Relay.On(Port4);
ReefAngel.Relay.On(Port5);
ReefAngel.Relay.On(Port6);
}

void loop()
{

ReefAngel.PWM.SetChannel(0, ReefCrestMode(60,30,true) ); // ReefCrest at 60% +/- 20% on sync mode
ReefAngel.PWM.SetChannel(1, ReefCrestMode(60,30,false) ); // ReefCrest at 60% +/- 20% on anti-sync mode
ReefAngel.PWM.SetChannel(2,PWMSlope(11,00,22,00,0,70,180,0));
ReefAngel.PWM.SetChannel(3,PWMSlope(11,30,21,30,0,60,180,0));
ReefAngel.PWM.SetChannel(4,PWMSlope(11,30,21,30,0,60,180,0));
ReefAngel.PWM.SetChannel(5,PWMSlope(11,30,21,30,0,60,180,0));

ReefAngel.StandardLights(Port1,8,0,22,0);
ReefAngel.StandardHeater(Port3,778,782);
ReefAngel.StandardATO(Port7,15);
ReefAngel.StandardLights(Port8,21,0,8,0);
ReefAngel.Portal("najluni15");
ReefAngel.ShowInterface();
}

byte ShortPulseMode(byte PulseMinSpeed, byte PulseMaxSpeed, int PulseDuration, boolean PulseSync)
{
byte tspeed=0;
PulseMinSpeed=constrain(PulseMinSpeed,30,100);
PulseMaxSpeed=constrain(PulseMaxSpeed,30,100);
tspeed=(millis()%(PulseDuration*2)<PulseDuration?PulseMinSpeed:PulseMaxSpeed);
if (PulseSync)
return tspeed;
else
return (tspeed==PulseMinSpeed)?PulseMaxSpeed:PulseMinSpeed;
}
byte LongPulseMode(byte PulseMinSpeed, byte PulseMaxSpeed, int PulseDuration, boolean PulseSync)
{
byte tspeed=0;
PulseMinSpeed=constrain(PulseMinSpeed,30,100);
PulseMaxSpeed=constrain(PulseMaxSpeed,30,100);
tspeed=(now()%(PulseDuration*2)<PulseDuration?PulseMinSpeed:PulseMaxSpeed);
if (PulseSync)
return tspeed;
else
return (tspeed==PulseMinSpeed)?PulseMaxSpeed:PulseMinSpeed;
}
byte SineMode(byte PulseMinSpeed, byte PulseMaxSpeed, int PulseDuration, boolean PulseSync) {
double x,y;

x=double(now()%(PulseDuration));
x/=PulseDuration;
x*=2.0*PI;
if (!PulseSync) x+=PI; // shift the sine wave for the right pump

y=sin(x);// y is now between -1 and 1
y+=1.0; // y is now between 0 and 2
y/=2.0; // y is now between 0 and 1

// now compute the tunze speed
y*=double(PulseMaxSpeed-PulseMinSpeed);
y+=double(PulseMinSpeed);

y+=0.5; // for proper rounding

// y is now between PulseMinSpeed and PulseMaxSpeed, constrain for safety
return constrain(byte(y),30,100);
}
byte ReefCrestMode(byte WaveSpeed, byte WaveOffset, boolean PulseSync)
{
static unsigned long lastwavemillis=millis();
static int newspeed=WaveSpeed;
if ((millis()-lastwavemillis) > 5000)
{
if (random(100)<50) newspeed--; else newspeed++;
newspeed=constrain(newspeed,WaveSpeed-WaveOffset,WaveSpeed+WaveOffset);
newspeed=constrain(newspeed,0,100);
lastwavemillis=millis();
}
if (PulseSync)
return newspeed;
else
return WaveSpeed-(newspeed-WaveSpeed);
}
byte NutrientTransportMode(byte PulseMinSpeed, byte PulseMaxSpeed, int PulseDuration, boolean PulseSync)
{
static unsigned long lastwavemillis=millis();
static byte WavePhase=0;
static time_t WaveStart=0;
static byte speed=PulseMinSpeed;
static byte anti_speed=PulseMinSpeed;

if (WavePhase==0)
{
WavePhase++;
WaveStart=now();
}
else if (WavePhase==1)
{
if (now()-WaveStart>2700)
{
WavePhase++;
}
if ((millis()-lastwavemillis) > PulseDuration)
{
if (speed==PulseMinSpeed)
{
speed=PulseMaxSpeed;
anti_speed=PulseMinSpeed;
}
else
{
speed=PulseMinSpeed;
anti_speed=PulseMaxSpeed;
}
lastwavemillis=millis();
}
}
else if (WavePhase==2)
{
if (now()-WaveStart>4500) WavePhase++;
if (now()-WaveStart<=2760)
speed=PulseMinSpeed;
else
speed=PulseMaxSpeed;
if (now()-WaveStart<=3300)
anti_speed=PulseMinSpeed;
else
anti_speed=PulseMaxSpeed*sin(radians(map(now()-WaveStart,3300,4500,0,180)));
}
else if (WavePhase==3)
{
if (now()-WaveStart>7200) WavePhase++;
if ((millis()-lastwavemillis) > PulseDuration)
{
if (speed==PulseMinSpeed)
{
speed=PulseMaxSpeed;
anti_speed=PulseMinSpeed;
}
else
{
speed=PulseMinSpeed;
anti_speed=PulseMaxSpeed;
}
lastwavemillis=millis();
}
}
else if (WavePhase==4)
{
if (now()-WaveStart>9000) WavePhase=0;
if (now()-WaveStart<=7260)
speed=PulseMinSpeed;
else
speed=PulseMaxSpeed;
if (now()-WaveStart<=8400)
anti_speed=PulseMaxSpeed*sin(radians(map(now()-WaveStart,7200,8400,0,180)));
else
anti_speed=0;
}
if (PulseSync)
return speed;
else
return anti_speed;
}
byte TidalSwellMode(byte WaveMaxSpeed, boolean PulseSync)
{
static unsigned long lastwavemillis=millis();
static byte WavePhase=0;
static time_t WaveStart=0;
static byte speed=0;
static byte anti_speed=0;

if (WavePhase==0)
{
WavePhase++;
WaveStart=now();
}
else if (WavePhase==1)
{
if (now()-WaveStart>900) WavePhase++;
speed=(WaveMaxSpeed*sin(radians(map(now()-WaveStart,0,900,0,90))))/10;
speed+=WaveMaxSpeed/2;

anti_speed=(WaveMaxSpeed*2*sin(radians(map(now()-WaveStart,0,900,0,90))))/5;
anti_speed+=WaveMaxSpeed/2;
}
else if (WavePhase==2)
{
if (now()-WaveStart>1800) WavePhase++;
speed=(WaveMaxSpeed*sin(radians(map(now()-WaveStart,900,1800,90,180))))/20;
speed+=WaveMaxSpeed/2;
speed+=WaveMaxSpeed/20;

anti_speed=(WaveMaxSpeed*3*sin(radians(map(now()-WaveStart,900,1800,90,180))))/20;
anti_speed+=WaveMaxSpeed/2;
anti_speed+=WaveMaxSpeed/4;

}
else if (WavePhase==3)
{
if (now()-WaveStart>2700) WavePhase++;
speed=(WaveMaxSpeed*3*sin(radians(map(now()-WaveStart,1800,2700,0,90))))/20;
speed+=WaveMaxSpeed/2;
speed+=WaveMaxSpeed/20;

anti_speed=(WaveMaxSpeed*sin(radians(map(now()-WaveStart,1800,2700,0,90))))/20;
anti_speed+=WaveMaxSpeed/2;
anti_speed+=WaveMaxSpeed/4;
}
else if (WavePhase==4)
{
if (now()-WaveStart>3600) WavePhase++;
speed=(WaveMaxSpeed*sin(radians(map(now()-WaveStart,2700,3600,90,180))))/20;
speed+=WaveMaxSpeed/2;
speed+=(WaveMaxSpeed*3)/20;

anti_speed=(WaveMaxSpeed*3*sin(radians(map(now()-WaveStart,2700,3600,90,180))))/20;
anti_speed+=WaveMaxSpeed/2;
anti_speed+=(WaveMaxSpeed*3)/20;
}
else if (WavePhase==5)
{
if (now()-WaveStart>4500) WavePhase++;
speed=(WaveMaxSpeed*3*sin(radians(map(now()-WaveStart,3600,4500,0,90))))/20;
speed+=WaveMaxSpeed/2;
speed+=(WaveMaxSpeed*3)/20;

anti_speed=(WaveMaxSpeed*sin(radians(map(now()-WaveStart,3600,4500,0,90))))/20;
anti_speed+=WaveMaxSpeed/2;
anti_speed+=(WaveMaxSpeed*3)/20;
}
else if (WavePhase==6)
{
if (now()-WaveStart>5400) WavePhase++;
speed=(WaveMaxSpeed*sin(radians(map(now()-WaveStart,4500,5400,90,180))))/20;
speed+=WaveMaxSpeed/2;
speed+=(WaveMaxSpeed*5)/20;

anti_speed=(WaveMaxSpeed*3*sin(radians(map(now()-WaveStart,4500,5400,90,180))))/20;
anti_speed+=WaveMaxSpeed/2;
anti_speed+=WaveMaxSpeed/20;
}
else if (WavePhase==7)
{
if (now()-WaveStart>6300) WavePhase++;
speed=(WaveMaxSpeed*3*sin(radians(map(now()-WaveStart,5400,6300,0,90))))/20;
speed+=WaveMaxSpeed/2;
speed+=(WaveMaxSpeed*5)/20;

anti_speed=(WaveMaxSpeed*sin(radians(map(now()-WaveStart,5400,6300,0,90))))/20;
anti_speed+=WaveMaxSpeed/2;
anti_speed+=WaveMaxSpeed/20;
}
else if (WavePhase==8)
{
if (now()-WaveStart>7200) WavePhase++;
speed=(WaveMaxSpeed*sin(radians(map(now()-WaveStart,6300,7200,90,180))))/20;
speed+=WaveMaxSpeed/2;
speed+=(WaveMaxSpeed*7)/20;

anti_speed=(WaveMaxSpeed*sin(radians(map(now()-WaveStart,6300,7200,90,180))))/10;
anti_speed+=WaveMaxSpeed/2;
}
else if (WavePhase==9)
{
if (now()-WaveStart>8100) WavePhase++;
speed=(WaveMaxSpeed*3*sin(radians(map(now()-WaveStart,7200,8100,0,90))))/20;
speed+=WaveMaxSpeed/2;
speed+=(WaveMaxSpeed*7)/20;

anti_speed=(WaveMaxSpeed*sin(radians(map(now()-WaveStart,7200,8100,0,90))))/2;
anti_speed+=WaveMaxSpeed/2;
}
else if (WavePhase==10)
{
if (now()-WaveStart>9000) WavePhase=0;
speed=(WaveMaxSpeed*sin(radians(map(now()-WaveStart,8100,9000,90,180))))/2;
speed+=WaveMaxSpeed/2;

anti_speed=speed;
}

if (PulseSync)
return speed;
else
return anti_speed;
}
byte TideMode(byte WaveSpeed, byte minOffset, byte maxOffset)
{
// Contribution of lnevo
double moonOffset; // gap between high and low
double amplitude; // tide curve
double wavelength=12*SECS_PER_HOUR;

// Calculate the gap between high and low tide based on MoonPhase()
moonOffset=cos(((2*PI)/100)*MoonPhase());
moonOffset=((moonOffset+1)/2)*100; // Convert to percentage

// Find out the current tidal height
amplitude=sin(((2*PI)/wavelength)*now());

moonOffset=map(moonOffset,0,100,minOffset,maxOffset);
amplitude=amplitude*moonOffset;

// Adjust the calculate speed to be in our adjusted range
return constrain(WaveSpeed+amplitude,0,100);
}
Thanks
binder
Posts: 2871
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
Contact:

Re: Help with Code

Post by binder »

Your PH calibration issue is here:

Code: Select all

ReefAngel.PHMin=298;// 298=PH7.0
ReefAngel.PHMax=580;// 580=PH10.0
Remove those lines from your setup(). They override any calibration that is stored in the Internal Memory. So no matter what values you get from calibration, they get overridden by the ones hard coded.

With your ATO problem, you are using the StandardATO function which requires the use of both float switches. The ATO will not be triggered until the bottom switch is activated. Once activated, it will run until the top switch is activated. See this thread for the discussion: http://forum.reefangel.com/viewtopic.php?f=7&t=240

What exactly do you mean by not working? You said you bought a 5 gallon reservoir to deal with not having to refill but it's not working. Is it filling too much or not frequently enough? Is the water not dropping down far enough before it kicks on? Please elaborate more. Thanks.
najluni15
Posts: 60
Joined: Sat Mar 10, 2012 6:47 pm

Re: Help with Code

Post by najluni15 »

Hi there,

Thank you for the help...

The ato is not being activated when it is supposed to. Both floats are installed, but when the bottom float drops, the ATO pump is not being turned on. As I understood it, the left side ATO float port of the head unit is the ATO low switch, and the right side is the high switch...is that correct? When looking directly at the head unit from the fron
binder
Posts: 2871
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
Contact:

Re: Help with Code

Post by binder »

najluni15 wrote: As I understood it, the left side ATO float port of the head unit is the ATO low switch, and the right side is the high switch...is that correct? When looking directly at the head unit from the fron
Yes, the Low port is the port on the outside and the High port is on the inside (next to the PH probe port).
najluni15
Posts: 60
Joined: Sat Mar 10, 2012 6:47 pm

Re: Help with Code

Post by najluni15 »

Ok...Thanks for the help....

PH probes are recalibrated but my ph is reading very low (6.8).

ATO seems to be working...I followed the wires and actually had them reversed (I have an electrical cabinet that houses the controller so it was hard to follow the wires)

I will try to test the tank ph against another test kit
najluni15
Posts: 60
Joined: Sat Mar 10, 2012 6:47 pm

Re: Help with Code

Post by najluni15 »

Ok...So I calibrated the ph probe and am now getting a reading of 6.8...I compared it to a cheap API kit and my api kit is reading ph at 8.1

Is there something in my code that I am missing or is my ph probe flawed?
binder
Posts: 2871
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
Contact:

Re: Help with Code

Post by binder »

najluni15 wrote:Ok...So I calibrated the ph probe and am now getting a reading of 6.8...I compared it to a cheap API kit and my api kit is reading ph at 8.1

Is there something in my code that I am missing or is my ph probe flawed?
That's out of my area now. Roberto or some others will have to chime in for that help.
User avatar
cosmith71
Posts: 1437
Joined: Fri Mar 29, 2013 3:51 pm
Location: Oklahoma City

Re: Help with Code

Post by cosmith71 »

najluni15 wrote:Ok...So I calibrated the ph probe and am now getting a reading of 6.8...I compared it to a cheap API kit and my api kit is reading ph at 8.1

Is there something in my code that I am missing or is my ph probe flawed?
Take a cup of water out of the tank and take the pH in the cup. If it works correctly, you may have stray voltage.

--Colin
Post Reply