Help with salinity ATO using water level expansion

Expansion modules and attachments
Post Reply
dazza1304
Posts: 154
Joined: Sat Aug 04, 2012 4:22 am

Help with salinity ATO using water level expansion

Post by dazza1304 »

Hi Guys, I have had this working well for a while but want to refine it.

Currently I have basically 2 containers - 1 with salt water and the other with RO water.

I currently use the waterlevel ato function to top up salt water and a salinity measure to top up RO.

These work independently of each other which is for the most part fine, but what happens occasionally is, my skimmer section of my sump might overfill a bit if for example the water level expansion (salt water) has just topped up to "40" and then the salinity is low and so RO water is added.

I have posted my code below for this.

What I would like to do, is change the following -feel free to come up with other suggestions as I may be looking at this wrong!!

1. Only enable waterlevelATO (saltwater) function if my salinity is <33.1ppt
2. Only enable RO top up if water level <40.

I think what i need to do is add the below:

if (SalCompensation<33.1)
ReefAngel.WaterLevelATO(Port2,180,35,40);

and

else
if Reefangel.WaterLevel.Getlevel () <= 40
{
ATOEnabled=true;
ATOPumping=false;
}


Is this correct???

Your help as always is appreciated!!

#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 <InternalEEPROM.h>
#include <RA_Colors.h>
#include <RA_CustomColors.h>
#include <Salinity.h>
#include <RF.h>
#include <IO.h>
#include <ORP.h>
#include <AI.h>
#include <PH.h>
#include <WaterLevel.h>
#include <ReefAngel.h>

////// Place global variable code below here
unsigned long lastLowSal=now();//salinity >35.1 timer
boolean SALduration=true;//salinity >33.1 for 2 minutes flag
boolean ATOEnabled=false;//enable ATO function for 5 seconds
boolean ATOPumping=true;//disable ATO function for 5 minutes
unsigned long ATOStart=now();//ATO 5 sec run timer
double Salinitytest;
////// Place global variable code above here


void setup()
{
    // This must be the first line
    ReefAngel.Init(); //Initialize controller
    ReefAngel.SetTemperatureUnit( Celsius ); // set to Celsius Temperature

    // Ports toggled in Feeding Mode
    ReefAngel.FeedingModePorts = 0;
    ReefAngel.FeedingModePortsE[0] = 0;
    // Ports toggled in Water Change Mode
    ReefAngel.WaterChangePorts = Port1Bit | Port2Bit;
    ReefAngel.WaterChangePortsE[0] = 0;
    // Ports toggled when Lights On / Off menu entry selected
    ReefAngel.LightsOnPorts = 0;
    ReefAngel.LightsOnPortsE[0] = 0;
    // Ports turned off when Overheat temperature exceeded
    ReefAngel.OverheatShutoffPorts = Port6Bit;
    ReefAngel.OverheatShutoffPortsE[0] = 0;
    // Use T1 probe as temperature and overheat functions
    ReefAngel.TempProbe = T1_PROBE;
    ReefAngel.OverheatProbe = T1_PROBE;
    // Set the Overheat temperature setting
    InternalMemory.OverheatTemp_write( 260 );
    InternalMemory.SalTempComp_write( 255); // Change this to compensation temperature


    // Ports that are always on
    ReefAngel.Relay.On( Port3 );
    ReefAngel.Relay.On( Port4 );
    ReefAngel.Relay.On( Port5 );
    ReefAngel.Relay.On( Port8 );
    ReefAngel.Relay.On( Box1_Port1 );
    ReefAngel.Relay.On( Box1_Port2 );
    ReefAngel.Relay.On( Box1_Port4 );
    ReefAngel.Relay.On( Box1_Port6 );
    ReefAngel.Relay.On( Box1_Port7 );
    ReefAngel.Relay.On( Box1_Port8 );
    
    ////// Place additional initialization code below here
    
    ReefAngel.Timer[1].SetInterval(1800); // 30 mins before reactivation of ATO
    
    ////// Place additional initialization code above here
}

void loop()
{
    //ReefAngel.Relay.DelayedOn( Port3,5 );//skimmer delay on switching on
    ReefAngel.StandardHeater( Port6,252,255 );
    ReefAngel.StandardFan( Port7,255,258 );
    ////// Place your custom code below here
    ReefAngel.WaterLevelATO(Port2,180,35,40);// Salinty top up to keep water level correct
    
    // Apply Salinity temp Compensation & salinity calibration
    double SalCompensation;
    if (ReefAngel.TempSensor.unit)
    SalCompensation=ReefAngel.Params.Salinity/(1+((ReefAngel.Params.Temp[T3_PROBE]-InternalMemory.SalTempComp_read())*0.0022));
    else
    SalCompensation=ReefAngel.Params.Salinity/(1+((ReefAngel.Params.Temp[T3_PROBE]-InternalMemory.SalTempComp_read())*0.001165));
    SalCompensation=round(SalCompensation);
    
   // For displaying salinity without temp compensation
   Salinitytest=SalCompensation;
   
    ///// If salinity >33.1 for 120 sec set SALduration flag
    if (SalCompensation<331) lastLowSal=now();
    
    if (now()-lastLowSal>120) SALduration=false;
  
    if (SALduration && ATOPumping)//if salinity>33.1 for 2 minutes enable ATO
  
    {ATOStart=now();
  }
  else
  {
    ATOEnabled=true;
    ATOPumping=false;
  }

  if (now()-ATOStart<5)//ATO pump on time 5 seconds
  {
    if (ATOEnabled) ReefAngel.Relay.On(Port1);
    ReefAngel.Timer[1].Start();//start 5 minute disable of ATO
  }
  else
  {
    ReefAngel.Relay.Off(Port1);
    ATOEnabled=false;
  }
  if (ReefAngel.Timer[1].IsTriggered())//after 30 minute disable, enable ATO
  {
    ATOPumping=true;
    ATOStart=now();
    SALduration=true;
  }


    ///// if skimmer cup full, turn off skimmer
    if (ReefAngel.HighATO.IsActive()) {
    bitClear(ReefAngel.Relay.RelayMaskOff,2);
    }
    else {
    bitSet(ReefAngel.Relay.RelayMaskOff,2);
    }
    
    ////// Place your custom code above here

    // This should always be the last line
    ReefAngel.Portal( "dazza1304" );
    ReefAngel.ShowInterface();
}

void DrawCustomMain()
{
    int x,y;
    char text[10];
    // Parameters
#if defined DisplayLEDPWM && ! defined RemoveAllLights
    ReefAngel.LCD.DrawMonitor( 15, 10, ReefAngel.Params,
    ReefAngel.PWM.GetDaylightValue(), ReefAngel.PWM.GetActinicValue() );
#else // defined DisplayLEDPWM && ! defined RemoveAllLights
    ReefAngel.LCD.DrawMonitor( 15, 10, ReefAngel.Params );
#endif // defined DisplayLEDPWM && ! defined RemoveAllLights
    pingSerial();

    // Salinity
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,15,46, "SAL:" );
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,39,46, ReefAngel.Params.Salinity );
    pingSerial();
    
    //map of adc temp comp salinity
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,15,63, "SAT:" );
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,39,63, (int)Salinitytest );

    // ORP
    ReefAngel.LCD.DrawText( COLOR_PALEVIOLETRED,DefaultBGColor,75,46, "ORP:" );
    ReefAngel.LCD.DrawText( COLOR_PALEVIOLETRED,DefaultBGColor,99,46, ReefAngel.Params.ORP );
    pingSerial();

    // Water Level
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,75,63, "WL:" );
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,99,63, ReefAngel.WaterLevel.GetLevel() );
    pingSerial();

    // Main Relay Box
    byte TempRelay = ReefAngel.Relay.RelayData;
    TempRelay &= ReefAngel.Relay.RelayMaskOff;
    TempRelay |= ReefAngel.Relay.RelayMaskOn;
    ReefAngel.LCD.DrawOutletBox( 12, 79, TempRelay );
    pingSerial();

    // Relay Expansion
    TempRelay = ReefAngel.Relay.RelayDataE[0];
    TempRelay &= ReefAngel.Relay.RelayMaskOffE[0];
    TempRelay |= ReefAngel.Relay.RelayMaskOnE[0];
    ReefAngel.LCD.DrawOutletBox( 12, 98, TempRelay );
    pingSerial();

    // Date and Time
    ReefAngel.LCD.DrawDate( 6, 122 );
    pingSerial();
}

void DrawCustomGraph()
{
}

User avatar
Sacohen
Posts: 1833
Joined: Sun Apr 21, 2013 6:25 am
Location: Davie, FL

Re: Help with salinity ATO using water level expansion

Post by Sacohen »

This is something I was thinking of doing too, but didn't know where to start.
Following along.
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Help with salinity ATO using water level expansion

Post by rimai »

I think it is correct.
Roberto.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Help with salinity ATO using water level expansion

Post by lnevo »

The logic looks good. I can't comment on the use / values of the salinity variable, but it looks like it would work...
dazza1304
Posts: 154
Joined: Sat Aug 04, 2012 4:22 am

Re: Help with salinity ATO using water level expansion

Post by dazza1304 »

Hi Guys, I have changed things around a lot to try to simplify, but it is not behaving as I expect - code below.

Basically, compiles OK, and after loading, with waterlevel > 40 and salinity <33.1, my RO pump (pump 1) starts, which it shouldn't.

I have probably got something slightly wrong, but cant see where, so your help would be appreciated!!!

///// If salinity >33.1 for 120 sec set SALduration flag
if (SalCompensation<331) lastLowSal=now();
if (now()-lastLowSal>120) SALduration=true;

//if salinty >33.1 and waterlevel <=35, top up with RO water
if (SALduration=true && ReefAngel.WaterLevel.GetLevel ()<=35)
{
ReefAngel.WaterLevelATO(Port1,180,35,40);

SALduration=false;
}

//if salinty <33.1 and waterlevel <=35, top up with salt water
if (SALduration=false && ReefAngel.WaterLevel.GetLevel ()<=35)
ReefAngel.WaterLevelATO(Port2,180,35,40);
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Help with salinity ATO using water level expansion

Post by lnevo »

Need the rest of your code. I don't see anything obvious here...
dazza1304
Posts: 154
Joined: Sat Aug 04, 2012 4:22 am

Re: Help with salinity ATO using water level expansion

Post by dazza1304 »

Sorry,

here you go!!
#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 <InternalEEPROM.h>
#include <RA_Colors.h>
#include <RA_CustomColors.h>
#include <Salinity.h>
#include <RF.h>
#include <IO.h>
#include <ORP.h>
#include <AI.h>
#include <PH.h>
#include <WaterLevel.h>
#include <ReefAngel.h>

////// Place global variable code below here
unsigned long lastLowSal=now();//salinity >33.1 timer
boolean SALduration=false;//salinity >33.1 for 2 minutes flag
double Salinitytest;
////// Place global variable code above here


void setup()
{
    // This must be the first line
    ReefAngel.Init(); //Initialize controller
    ReefAngel.SetTemperatureUnit( Celsius ); // set to Celsius Temperature

    // Ports toggled in Feeding Mode
    ReefAngel.FeedingModePorts = 0;
    ReefAngel.FeedingModePortsE[0] = 0;
    // Ports toggled in Water Change Mode
    ReefAngel.WaterChangePorts = Port1Bit | Port2Bit;
    ReefAngel.WaterChangePortsE[0] = 0;
    // Ports toggled when Lights On / Off menu entry selected
    ReefAngel.LightsOnPorts = 0;
    ReefAngel.LightsOnPortsE[0] = 0;
    // Ports turned off when Overheat temperature exceeded
    ReefAngel.OverheatShutoffPorts = Port6Bit;
    ReefAngel.OverheatShutoffPortsE[0] = 0;
    // Use T1 probe as temperature and overheat functions
    ReefAngel.TempProbe = T1_PROBE;
    ReefAngel.OverheatProbe = T1_PROBE;
    // Set the Overheat temperature setting
    InternalMemory.OverheatTemp_write( 260 );
    InternalMemory.SalTempComp_write( 255); // Change this to compensation temperature


    // Ports that are always on
    ReefAngel.Relay.On( Port3 );
    ReefAngel.Relay.On( Port4 );
    ReefAngel.Relay.On( Port5 );
    ReefAngel.Relay.On( Port8 );
    ReefAngel.Relay.On( Box1_Port1 );
    ReefAngel.Relay.On( Box1_Port2 );
    ReefAngel.Relay.On( Box1_Port4 );
    ReefAngel.Relay.On( Box1_Port6 );
    ReefAngel.Relay.On( Box1_Port7 );
    ReefAngel.Relay.On( Box1_Port8 );
    
    // Ports that are always off
    ReefAngel.Relay.Off( Port1 );
    ReefAngel.Relay.Off( Port2 );
    
    ////// Place additional initialization code below here
    
  
    
    ////// Place additional initialization code above here
}

void loop()
{
    ReefAngel.StandardHeater( Port6,252,255 );
    ReefAngel.StandardFan( Port7,255,258 );
    ////// Place your custom code below here
    
    // Apply Salinity temp Compensation & salinity calibration
    double SalCompensation;
    if (ReefAngel.TempSensor.unit)
    SalCompensation=ReefAngel.Params.Salinity/(1+((ReefAngel.Params.Temp[T3_PROBE]-InternalMemory.SalTempComp_read())*0.0022));
    else
    SalCompensation=ReefAngel.Params.Salinity/(1+((ReefAngel.Params.Temp[T3_PROBE]-InternalMemory.SalTempComp_read())*0.001165));
    SalCompensation=round(SalCompensation);
    
    // For displaying salinity without temp compensation
    Salinitytest=SalCompensation;
    
    ///// If salinity >33.1 for 120 sec set SALduration flag
    if (SalCompensation<331) lastLowSal=now();
    if (now()-lastLowSal>120) SALduration=true;
    
    //if salinty >33.1 and waterlevel <=35, top up with RO water
    if (SALduration=true && ReefAngel.WaterLevel.GetLevel ()<=35)
    {
      ReefAngel.WaterLevelATO(Port1,180,35,40);
      SALduration=false;
    }
    
   //if salinty <33.1 and waterlevel <=35, top up with salt water
    if (SALduration=false && ReefAngel.WaterLevel.GetLevel ()<=35)
      ReefAngel.WaterLevelATO(Port2,180,35,40);
      
     
    // if skimmer cup full, turn off skimmer
    if (ReefAngel.HighATO.IsActive()) {
    bitClear(ReefAngel.Relay.RelayMaskOff,2);
    }
    else {
    bitSet(ReefAngel.Relay.RelayMaskOff,2);
    }
    
    ////// Place your custom code above here

    // This should always be the last line
    ReefAngel.Portal( "dazza1304" );
    ReefAngel.ShowInterface();
}

void DrawCustomMain()
{
    int x,y;
    char text[10];
    // Parameters
#if defined DisplayLEDPWM && ! defined RemoveAllLights
    ReefAngel.LCD.DrawMonitor( 15, 10, ReefAngel.Params,
    ReefAngel.PWM.GetDaylightValue(), ReefAngel.PWM.GetActinicValue() );
#else // defined DisplayLEDPWM && ! defined RemoveAllLights
    ReefAngel.LCD.DrawMonitor( 15, 10, ReefAngel.Params );
#endif // defined DisplayLEDPWM && ! defined RemoveAllLights
    pingSerial();

    // Salinity
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,15,46, "SAL:" );
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,39,46, ReefAngel.Params.Salinity );
    pingSerial();
    
    //map of adc temp comp salinity
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,15,63, "SAT:" );
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,39,63, (int)Salinitytest );

    // ORP
    ReefAngel.LCD.DrawText( COLOR_PALEVIOLETRED,DefaultBGColor,75,46, "ORP:" );
    ReefAngel.LCD.DrawText( COLOR_PALEVIOLETRED,DefaultBGColor,99,46, ReefAngel.Params.ORP );
    pingSerial();

    // Water Level
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,75,63, "WL:" );
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,99,63, ReefAngel.WaterLevel.GetLevel() );
    pingSerial();

    // Main Relay Box
    byte TempRelay = ReefAngel.Relay.RelayData;
    TempRelay &= ReefAngel.Relay.RelayMaskOff;
    TempRelay |= ReefAngel.Relay.RelayMaskOn;
    ReefAngel.LCD.DrawOutletBox( 12, 79, TempRelay );
    pingSerial();

    // Relay Expansion
    TempRelay = ReefAngel.Relay.RelayDataE[0];
    TempRelay &= ReefAngel.Relay.RelayMaskOffE[0];
    TempRelay |= ReefAngel.Relay.RelayMaskOnE[0];
    ReefAngel.LCD.DrawOutletBox( 12, 98, TempRelay );
    pingSerial();

    // Date and Time
    ReefAngel.LCD.DrawDate( 6, 122 );
    pingSerial();
}

void DrawCustomGraph()
{
}

rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Help with salinity ATO using water level expansion

Post by rimai »

Your code has no way to stop either port1 nor port2.
You need something like this:

Code: Select all

//if salinty >33.1 and waterlevel <=35, top up with RO water
if (SALduration=true && ReefAngel.WaterLevel.GetLevel ()<=35)
 {
 ReefAngel.WaterLevelATO(Port1,180,35,40);
 SALduration=false;
 }
else
{
 ReefAngel.Relay.Off(Port1);
}

//if salinty <33.1 and waterlevel <=35, top up with salt water
 if (SALduration=false && ReefAngel.WaterLevel.GetLevel ()<=35)
 ReefAngel.WaterLevelATO(Port2,180,35,40);
else
 ReefAngel.Relay.Off(Port2);
Otherwise, if the SALduration changes state, WaterLevelATO never gets called and that's what causes the port to stay in the previous position.
Roberto.
dazza1304
Posts: 154
Joined: Sat Aug 04, 2012 4:22 am

Re: Help with salinity ATO using water level expansion

Post by dazza1304 »

Hi Roberto,

Tried that, but things dont seem to be happening correctly.

First, after uploading code below, with salinity at 32.9 and wl at 42, nothing happens as would expect.

To simulate operation, then raised wl sensor up until wl less than 35, and then straight away, red status light comes on - go in and execute ATO clear, come back out to main screen - try raising wl sensor until wl less than 35, then port 1 is activated (should be port 2!! according to my reading of the code) and this operates until wl =40.

Again, if I then try raising wl sensor again so wl <35, ATO flag is set again as above?

Can you make any sense of this??



#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 <InternalEEPROM.h>
#include <RA_Colors.h>
#include <RA_CustomColors.h>
#include <Salinity.h>
#include <RF.h>
#include <IO.h>
#include <ORP.h>
#include <AI.h>
#include <PH.h>
#include <WaterLevel.h>
#include <ReefAngel.h>

////// Place global variable code below here
unsigned long lastLowSal=now();//salinity >33.1 timer
boolean SALduration=false;//salinity >33.1 for 2 minutes flag
double Salinitytest;
////// Place global variable code above here


void setup()
{
// This must be the first line
ReefAngel.Init(); //Initialize controller
ReefAngel.SetTemperatureUnit( Celsius ); // set to Celsius Temperature

// Ports toggled in Feeding Mode
ReefAngel.FeedingModePorts = 0;
ReefAngel.FeedingModePortsE[0] = 0;
// Ports toggled in Water Change Mode
ReefAngel.WaterChangePorts = Port1Bit | Port2Bit;
ReefAngel.WaterChangePortsE[0] = 0;
// Ports toggled when Lights On / Off menu entry selected
ReefAngel.LightsOnPorts = 0;
ReefAngel.LightsOnPortsE[0] = 0;
// Ports turned off when Overheat temperature exceeded
ReefAngel.OverheatShutoffPorts = Port6Bit;
ReefAngel.OverheatShutoffPortsE[0] = 0;
// Use T1 probe as temperature and overheat functions
ReefAngel.TempProbe = T1_PROBE;
ReefAngel.OverheatProbe = T1_PROBE;
// Set the Overheat temperature setting
InternalMemory.OverheatTemp_write( 260 );
InternalMemory.SalTempComp_write( 255); // Change this to compensation temperature


// Ports that are always on
ReefAngel.Relay.On( Port3 );
ReefAngel.Relay.On( Port4 );
ReefAngel.Relay.On( Port5 );
ReefAngel.Relay.On( Port8 );
ReefAngel.Relay.On( Box1_Port1 );
ReefAngel.Relay.On( Box1_Port2 );
ReefAngel.Relay.On( Box1_Port4 );
ReefAngel.Relay.On( Box1_Port6 );
ReefAngel.Relay.On( Box1_Port7 );
ReefAngel.Relay.On( Box1_Port8 );

// Ports that are always off
ReefAngel.Relay.Off( Port1 );
ReefAngel.Relay.Off( Port2 );

////// Place additional initialization code below here



////// Place additional initialization code above here
}

void loop()
{
ReefAngel.StandardHeater( Port6,252,255 );
ReefAngel.StandardFan( Port7,255,258 );
////// Place your custom code below here

// Apply Salinity temp Compensation & salinity calibration
double SalCompensation;
if (ReefAngel.TempSensor.unit)
SalCompensation=ReefAngel.Params.Salinity/(1+((ReefAngel.Params.Temp[T3_PROBE]-InternalMemory.SalTempComp_read())*0.0022));
else
SalCompensation=ReefAngel.Params.Salinity/(1+((ReefAngel.Params.Temp[T3_PROBE]-InternalMemory.SalTempComp_read())*0.001165));
SalCompensation=round(SalCompensation);

// For displaying salinity without temp compensation
Salinitytest=SalCompensation;

///// If salinity >33.1 for 120 sec set SALduration flag
if (SalCompensation<331) lastLowSal=now();
if (now()-lastLowSal>120) SALduration=true;

//if salinty >33.1 and waterlevel <=35, top up with RO water
if (SALduration=true && ReefAngel.WaterLevel.GetLevel ()<=35)
{
ReefAngel.WaterLevelATO(Port1,30,35,40);
SALduration=false;
}
else
{
ReefAngel.Relay.Off(Port1);
}
//if salinty <33.1 and waterlevel <=35, top up with salt water
if (SALduration=false && ReefAngel.WaterLevel.GetLevel ()<=35)
ReefAngel.WaterLevelATO(Port2,30,35,40);
else
ReefAngel.Relay.Off(Port2);


// if skimmer cup full, turn off skimmer
if (ReefAngel.HighATO.IsActive()) {
bitClear(ReefAngel.Relay.RelayMaskOff,2);
}
else {
bitSet(ReefAngel.Relay.RelayMaskOff,2);
}

////// Place your custom code above here

// This should always be the last line
ReefAngel.Portal( "dazza1304" );
ReefAngel.ShowInterface();
}

void DrawCustomMain()
{
int x,y;
char text[10];
// Parameters
#if defined DisplayLEDPWM && ! defined RemoveAllLights
ReefAngel.LCD.DrawMonitor( 15, 10, ReefAngel.Params,
ReefAngel.PWM.GetDaylightValue(), ReefAngel.PWM.GetActinicValue() );
#else // defined DisplayLEDPWM && ! defined RemoveAllLights
ReefAngel.LCD.DrawMonitor( 15, 10, ReefAngel.Params );
#endif // defined DisplayLEDPWM && ! defined RemoveAllLights
pingSerial();

// Salinity
ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,15,46, "SAL:" );
ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,39,46, ReefAngel.Params.Salinity );
pingSerial();

//map of adc temp comp salinity
ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,15,63, "SAT:" );
ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,39,63, (int)Salinitytest );

// ORP
ReefAngel.LCD.DrawText( COLOR_PALEVIOLETRED,DefaultBGColor,75,46, "ORP:" );
ReefAngel.LCD.DrawText( COLOR_PALEVIOLETRED,DefaultBGColor,99,46, ReefAngel.Params.ORP );
pingSerial();

// Water Level
ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,75,63, "WL:" );
ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,99,63, ReefAngel.WaterLevel.GetLevel() );
pingSerial();

// Main Relay Box
byte TempRelay = ReefAngel.Relay.RelayData;
TempRelay &= ReefAngel.Relay.RelayMaskOff;
TempRelay |= ReefAngel.Relay.RelayMaskOn;
ReefAngel.LCD.DrawOutletBox( 12, 79, TempRelay );
pingSerial();

// Relay Expansion
TempRelay = ReefAngel.Relay.RelayDataE[0];
TempRelay &= ReefAngel.Relay.RelayMaskOffE[0];
TempRelay |= ReefAngel.Relay.RelayMaskOnE[0];
ReefAngel.LCD.DrawOutletBox( 12, 98, TempRelay );
pingSerial();

// Date and Time
ReefAngel.LCD.DrawDate( 6, 122 );
pingSerial();
}

void DrawCustomGraph()
{
}
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Help with salinity ATO using water level expansion

Post by rimai »

I think it is this:
if (SALduration=true && ReefAngel.WaterLevel.GetLevel ()<=35)
It will only be true is wl is less and equal to 35
But your ATO function ReefAngel.WaterLevelATO(Port1,30,35,40);
calls for pump water from 35 to 40.... Well, it will never reach 40 to stop the ATO from pumping and clear the timeout.
You should use Serial.print to debug your code... It will make things a lot easier to understand.
Roberto.
dazza1304
Posts: 154
Joined: Sat Aug 04, 2012 4:22 am

Re: Help with salinity ATO using water level expansion

Post by dazza1304 »

rimai wrote:I think it is this:
if (SALduration=true && ReefAngel.WaterLevel.GetLevel ()<=35)
It will only be true is wl is less and equal to 35
But your ATO function ReefAngel.WaterLevelATO(Port1,30,35,40);
calls for pump water from 35 to 40.... Well, it will never reach 40 to stop the ATO from pumping and clear the timeout.
You should use Serial.print to debug your code... It will make things a lot easier to understand.
Thanks Roberto - silly question now, how do I use serial.print? Is there a guide somewhere?

I know you suggested previously on one of my posts to use it, but never got to grips with it!
dazza1304
Posts: 154
Joined: Sat Aug 04, 2012 4:22 am

Re: Help with salinity ATO using water level expansion

Post by dazza1304 »

Actually looked at this a bit more, and it seems according to the arduino website, that the<= operand should be ok.

I think the problems is using the ATO function - can you remind me where I can find the code for it please?

I think I will make my own code for the function to be sure it functions as I require.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Help with salinity ATO using water level expansion

Post by lnevo »

The reason the <= is not ok in this situation is because once your Water Level reaches 36% then the ATO would be disabled and never reach 40%. This will also cause the WaterLevelATO to leave the port in a potentially on or off state and not what you want...

You will need to set a flag if the waterlevel reaches less than 35% and then do your if with the flag. Once the water level reaches your desired amount you would then reset the flag.
dazza1304
Posts: 154
Joined: Sat Aug 04, 2012 4:22 am

Re: Help with salinity ATO using water level expansion

Post by dazza1304 »

lnevo wrote:The reason the <= is not ok in this situation is because once your Water Level reaches 36% then the ATO would be disabled and never reach 40%. This will also cause the WaterLevelATO to leave the port in a potentially on or off state and not what you want...

You will need to set a flag if the waterlevel reaches less than 35% and then do your if with the flag. Once the water level reaches your desired amount you would then reset the flag.
Thanks Lee, yes I understand now - I thought once the ATO function was called it looked after itself so to speak, but obviously not.

I will write my own code for this which should be pretty simples - I hope!

Will post up my code once I have sorted!

Thanks again,

Daryl
dazza1304
Posts: 154
Joined: Sat Aug 04, 2012 4:22 am

Re: Help with salinity ATO using water level expansion

Post by dazza1304 »

Have redone the code now, without the waterlevel ATO function and all seems to work fine!!
#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 <InternalEEPROM.h>
#include <RA_Colors.h>
#include <RA_CustomColors.h>
#include <Salinity.h>
#include <RF.h>
#include <IO.h>
#include <ORP.h>
#include <AI.h>
#include <PH.h>
#include <WaterLevel.h>
#include <ReefAngel.h>

////// Place global variable code below here
unsigned long lastLowSal=now();//salinity >33.1 timer
boolean SALduration=false;//salinity >33.1 2 minute debounce
double Salinitytest;
unsigned long lastLoWat=now();//WL < 35 timer
boolean Watduration=false;//WL <35 2 minute debounce
boolean ROtopup = false;
boolean SALTtopup = false;
////// Place global variable code above here


void setup()
{
    // This must be the first line
    ReefAngel.Init(); //Initialize controller
    ReefAngel.SetTemperatureUnit( Celsius ); // set to Celsius Temperature

    // Ports toggled in Feeding Mode
    ReefAngel.FeedingModePorts = 0;
    ReefAngel.FeedingModePortsE[0] = 0;
    // Ports toggled in Water Change Mode
    ReefAngel.WaterChangePorts = Port1Bit | Port2Bit;
    ReefAngel.WaterChangePortsE[0] = 0;
    // Ports toggled when Lights On / Off menu entry selected
    ReefAngel.LightsOnPorts = 0;
    ReefAngel.LightsOnPortsE[0] = 0;
    // Ports turned off when Overheat temperature exceeded
    ReefAngel.OverheatShutoffPorts = Port6Bit;
    ReefAngel.OverheatShutoffPortsE[0] = 0;
    // Use T1 probe as temperature and overheat functions
    ReefAngel.TempProbe = T1_PROBE;
    ReefAngel.OverheatProbe = T1_PROBE;
    // Set the Overheat temperature setting
    InternalMemory.OverheatTemp_write( 260 );
    InternalMemory.SalTempComp_write( 255); // Change this to compensation temperature


    // Ports that are always on
    ReefAngel.Relay.On( Port3 );
    ReefAngel.Relay.On( Port4 );
    ReefAngel.Relay.On( Port5 );
    ReefAngel.Relay.On( Port8 );
    ReefAngel.Relay.On( Box1_Port1 );
    ReefAngel.Relay.On( Box1_Port2 );
    ReefAngel.Relay.On( Box1_Port4 );
    ReefAngel.Relay.On( Box1_Port6 );
    ReefAngel.Relay.On( Box1_Port7 );
    ReefAngel.Relay.On( Box1_Port8 );
    
    // Ports that are always off
    ReefAngel.Relay.Off( Port1 );
    ReefAngel.Relay.Off( Port2 );
    
    ////// Place additional initialization code below here
    
  
    
    ////// Place additional initialization code above here
}

void loop()
{
    ReefAngel.StandardHeater( Port6,252,255 );
    ReefAngel.StandardFan( Port7,255,258 );
    ////// Place your custom code below here
    
    // Apply Salinity temp Compensation & salinity calibration
    double SalCompensation;
    if (ReefAngel.TempSensor.unit)
    SalCompensation=ReefAngel.Params.Salinity/(1+((ReefAngel.Params.Temp[T3_PROBE]-InternalMemory.SalTempComp_read())*0.0022));
    else
    SalCompensation=ReefAngel.Params.Salinity/(1+((ReefAngel.Params.Temp[T3_PROBE]-InternalMemory.SalTempComp_read())*0.001165));
    SalCompensation=round(SalCompensation);
    
    // For displaying salinity without temp compensation
    Salinitytest=SalCompensation;
    
    // If salinity >33.1 for 120 sec set SALduration flag;salinity debounce
    if (SalCompensation<331) lastLowSal=now();
    if (now()-lastLowSal>120) SALduration=true;
    
    ///// If WL < 35 for 120 sec set Watduration flag;Waterlevel debounce
    if (ReefAngel.WaterLevel.GetLevel()>35) lastLoWat=now();
    if (now()-lastLoWat>120) Watduration=true;
    
    //if salinty >33.1 and waterlevel <35, top up with RO water until WL =40
    if (SALduration==true && Watduration==true) ROtopup=true;
    if (ROtopup==true) ReefAngel.Relay.On(Port1);
    if (ROtopup==true && ReefAngel.WaterLevel.GetLevel()>40)
    
    {
      ReefAngel.Relay.Off(Port1);
      ROtopup=false;
      SALduration=false;
      Watduration=false;
    }
    
    //if salinty <33.1 and waterlevel < 35, top up with SALT water until WL = 40
    if (SALduration==false && Watduration==true) SALTtopup=true;
    if (SALTtopup==true) ReefAngel.Relay.On(Port2);
    if (SALTtopup==true && ReefAngel.WaterLevel.GetLevel()>40)
    
    {
      ReefAngel.Relay.Off(Port2);
      SALTtopup=false;
      Watduration=false;
    }

    // if skimmer cup full, turn off skimmer
    if (ReefAngel.HighATO.IsActive()) {
    bitClear(ReefAngel.Relay.RelayMaskOff,2);
    }
    else {
    bitSet(ReefAngel.Relay.RelayMaskOff,2);
    }
    
    ////// Place your custom code above here

    // This should always be the last line
    ReefAngel.Portal( "dazza1304" );
    ReefAngel.ShowInterface();
}

void DrawCustomMain()
{
    int x,y;
    char text[10];
    // Parameters
#if defined DisplayLEDPWM && ! defined RemoveAllLights
    ReefAngel.LCD.DrawMonitor( 15, 10, ReefAngel.Params,
    ReefAngel.PWM.GetDaylightValue(), ReefAngel.PWM.GetActinicValue() );
#else // defined DisplayLEDPWM && ! defined RemoveAllLights
    ReefAngel.LCD.DrawMonitor( 15, 10, ReefAngel.Params );
#endif // defined DisplayLEDPWM && ! defined RemoveAllLights
    pingSerial();

    // Salinity
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,15,46, "SAL:" );
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,39,46, ReefAngel.Params.Salinity );
    pingSerial();
    
    //map of adc temp comp salinity
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,15,63, "SAT:" );
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,39,63, (int)Salinitytest );

    // ORP
    ReefAngel.LCD.DrawText( COLOR_PALEVIOLETRED,DefaultBGColor,75,46, "ORP:" );
    ReefAngel.LCD.DrawText( COLOR_PALEVIOLETRED,DefaultBGColor,99,46, ReefAngel.Params.ORP );
    pingSerial();

    // Water Level
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,75,63, "WL:" );
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,99,63, ReefAngel.WaterLevel.GetLevel() );
    pingSerial();

    // Main Relay Box
    byte TempRelay = ReefAngel.Relay.RelayData;
    TempRelay &= ReefAngel.Relay.RelayMaskOff;
    TempRelay |= ReefAngel.Relay.RelayMaskOn;
    ReefAngel.LCD.DrawOutletBox( 12, 79, TempRelay );
    pingSerial();

    // Relay Expansion
    TempRelay = ReefAngel.Relay.RelayDataE[0];
    TempRelay &= ReefAngel.Relay.RelayMaskOffE[0];
    TempRelay |= ReefAngel.Relay.RelayMaskOnE[0];
    ReefAngel.LCD.DrawOutletBox( 12, 98, TempRelay );
    pingSerial();

    // Date and Time
    ReefAngel.LCD.DrawDate( 6, 122 );
    pingSerial();
}

void DrawCustomGraph()
{
}

dazza1304
Posts: 154
Joined: Sat Aug 04, 2012 4:22 am

Re: Help with salinity ATO using water level expansion

Post by dazza1304 »

Hopefully someone will be able to help!!

THought all was working fine, but I have actually caught the unit operating relay 2 when salinity > 33.1 and water level > 35.

I cannot see how this is possible from my code.

Can someone have a look and see what might be going on please??
#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 <InternalEEPROM.h>
#include <RA_Colors.h>
#include <RA_CustomColors.h>
#include <Salinity.h>
#include <RF.h>
#include <IO.h>
#include <ORP.h>
#include <AI.h>
#include <PH.h>
#include <WaterLevel.h>
#include <ReefAngel.h>

////// Place global variable code below here
unsigned long lastLowSal=now();//salinity >33.1 timer
boolean SALduration=false;//salinity >33.1 2 minute debounce
double Salinitytest;
unsigned long lastLoWat=now();//WL < 35 timer
boolean Watduration=false;//WL <35 2 minute debounce
boolean ROtopup = false;
boolean SALTtopup = false;
////// Place global variable code above here


void setup()
{
    // This must be the first line
    ReefAngel.Init(); //Initialize controller
    ReefAngel.SetTemperatureUnit( Celsius ); // set to Celsius Temperature

    // Ports toggled in Feeding Mode
    ReefAngel.FeedingModePorts = 0;
    ReefAngel.FeedingModePortsE[0] = 0;
    // Ports toggled in Water Change Mode
    ReefAngel.WaterChangePorts = Port1Bit | Port2Bit;
    ReefAngel.WaterChangePortsE[0] = 0;
    // Ports toggled when Lights On / Off menu entry selected
    ReefAngel.LightsOnPorts = 0;
    ReefAngel.LightsOnPortsE[0] = 0;
    // Ports turned off when Overheat temperature exceeded
    ReefAngel.OverheatShutoffPorts = Port6Bit;
    ReefAngel.OverheatShutoffPortsE[0] = 0;
    // Use T1 probe as temperature and overheat functions
    ReefAngel.TempProbe = T1_PROBE;
    ReefAngel.OverheatProbe = T1_PROBE;
    // Set the Overheat temperature setting
    InternalMemory.OverheatTemp_write( 260 );
    InternalMemory.SalTempComp_write( 255); // Change this to compensation temperature


    // Ports that are always on
    ReefAngel.Relay.On( Port3 );
    ReefAngel.Relay.On( Port4 );
    ReefAngel.Relay.On( Port5 );
    ReefAngel.Relay.On( Port8 );
    ReefAngel.Relay.On( Box1_Port1 );
    ReefAngel.Relay.On( Box1_Port2 );
    ReefAngel.Relay.On( Box1_Port4 );
    ReefAngel.Relay.On( Box1_Port6 );
    ReefAngel.Relay.On( Box1_Port7 );
    ReefAngel.Relay.On( Box1_Port8 );
     
    
    ////// Place additional initialization code below here
    
  
    
    ////// Place additional initialization code above here
}

void loop()
{
    ReefAngel.StandardHeater( Port6,252,255 );
    ReefAngel.StandardFan( Port7,255,258 );
    ////// Place your custom code below here
    
    // Apply Salinity temp Compensation & salinity calibration
    double SalCompensation;
    if (ReefAngel.TempSensor.unit)
    SalCompensation=ReefAngel.Params.Salinity/(1+((ReefAngel.Params.Temp[T3_PROBE]-InternalMemory.SalTempComp_read())*0.0022));
    else
    SalCompensation=ReefAngel.Params.Salinity/(1+((ReefAngel.Params.Temp[T3_PROBE]-InternalMemory.SalTempComp_read())*0.001165));
    SalCompensation=round(SalCompensation);
    
    // For displaying salinity without temp compensation
    Salinitytest=SalCompensation;
    
    // If salinity >33.1 for 120 sec set SALduration flag;salinity debounce
    if (SalCompensation<331) lastLowSal=now();
    if (now()-lastLowSal>120) SALduration=true;
    
    ///// If WL < 35 for 120 sec set Watduration flag;Waterlevel debounce
    if (ReefAngel.WaterLevel.GetLevel()>35) lastLoWat=now();
    if (now()-lastLoWat>120) Watduration=true;
    
    //if salinty >33.1 and waterlevel <35, top up with RO water until WL =40
    if (SALduration==true && Watduration==true) ROtopup=true;
    if (ROtopup==true) ReefAngel.Relay.On(Port1);
    if (ROtopup==true && ReefAngel.WaterLevel.GetLevel()>40)
    
    {
      ReefAngel.Relay.Off(Port1);
      ROtopup=false;
      SALduration=false;
      Watduration=false;
    }
    
    //if salinty <33.1 and waterlevel < 35, top up with SALT water until WL = 40
    if (SALduration==false && Watduration==true) SALTtopup=true;
    if (SALTtopup==true) ReefAngel.Relay.On(Port2);
    if (SALTtopup==true && ReefAngel.WaterLevel.GetLevel()>40)
    
    {
      ReefAngel.Relay.Off(Port2);
      SALTtopup=false;
      Watduration=false;
    }

    // if skimmer cup full, turn off skimmer
    if (ReefAngel.HighATO.IsActive()) {
    bitClear(ReefAngel.Relay.RelayMaskOff,2);
    }
    else {
    bitSet(ReefAngel.Relay.RelayMaskOff,2);
    }
    
    ////// Place your custom code above here

    // This should always be the last line
    ReefAngel.Portal( "dazza1304" );
    ReefAngel.ShowInterface();
}

void DrawCustomMain()
{
    int x,y;
    char text[10];
    // Parameters
#if defined DisplayLEDPWM && ! defined RemoveAllLights
    ReefAngel.LCD.DrawMonitor( 15, 10, ReefAngel.Params,
    ReefAngel.PWM.GetDaylightValue(), ReefAngel.PWM.GetActinicValue() );
#else // defined DisplayLEDPWM && ! defined RemoveAllLights
    ReefAngel.LCD.DrawMonitor( 15, 10, ReefAngel.Params );
#endif // defined DisplayLEDPWM && ! defined RemoveAllLights
    pingSerial();

    // Salinity
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,15,46, "SAL:" );
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,39,46, ReefAngel.Params.Salinity );
    pingSerial();
    
    //map of adc temp comp salinity
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,15,63, "SAT:" );
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,39,63, (int)Salinitytest );

    // ORP
    ReefAngel.LCD.DrawText( COLOR_PALEVIOLETRED,DefaultBGColor,75,46, "ORP:" );
    ReefAngel.LCD.DrawText( COLOR_PALEVIOLETRED,DefaultBGColor,99,46, ReefAngel.Params.ORP );
    pingSerial();

    // Water Level
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,75,63, "WL:" );
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,99,63, ReefAngel.WaterLevel.GetLevel() );
    pingSerial();

    // Main Relay Box
    byte TempRelay = ReefAngel.Relay.RelayData;
    TempRelay &= ReefAngel.Relay.RelayMaskOff;
    TempRelay |= ReefAngel.Relay.RelayMaskOn;
    ReefAngel.LCD.DrawOutletBox( 12, 79, TempRelay );
    pingSerial();

    // Relay Expansion
    TempRelay = ReefAngel.Relay.RelayDataE[0];
    TempRelay &= ReefAngel.Relay.RelayMaskOffE[0];
    TempRelay |= ReefAngel.Relay.RelayMaskOnE[0];
    ReefAngel.LCD.DrawOutletBox( 12, 98, TempRelay );
    pingSerial();

    // Date and Time
    ReefAngel.LCD.DrawDate( 6, 122 );
    pingSerial();
}

void DrawCustomGraph()
{
}
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Help with salinity ATO using water level expansion

Post by rimai »

You are only turning port2 off when it reaches 40:
if (SALTtopup==true && ReefAngel.WaterLevel.GetLevel()>40)
Roberto.
dazza1304
Posts: 154
Joined: Sat Aug 04, 2012 4:22 am

Re: Help with salinity ATO using water level expansion

Post by dazza1304 »

Thanks Roberto, but that's what I want it to do.

So basically I have 2 flags for salinity and water level.

If salinity <33.1 AND water level <35, then I top up with saltwater until water level > 40, then turn off port.

If salinity >33.1 AND water level <35, then I top up with RO water until water level> 40, then turn off port.

I can't see how with my code it is possible for port 2 (saltwater top up) to be turned on with salinity > 33.1 and water level level > 35?

I have seen this happen whilst watching it - what alerted me to this was my salinity started to rise.

Any ideas?
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Help with salinity ATO using water level expansion

Post by rimai »

This was what you said:
THought all was working fine, but I have actually caught the unit operating relay 2 when salinity > 33.1 and water level > 35.
But in your code, you only turn off port 2 when wl is greater than 40....
So, of course it will run when it it greater and 35....
Here is the code that turns off port2:

Code: Select all

    if (SALTtopup==true && ReefAngel.WaterLevel.GetLevel()>40)
    
    {
      ReefAngel.Relay.Off(Port2); 
      SALTtopup=false;
      Watduration=false;
    }
You can clearly see that the code only turns port2 off when wl is greater than 40
Roberto.
dazza1304
Posts: 154
Joined: Sat Aug 04, 2012 4:22 am

Re: Help with salinity ATO using water level expansion

Post by dazza1304 »

Hi Roberto, maybe Im not explaining myself well, or Im missing something -the latter probably being the case!

Port 2 (saltwater top up) should ONLY be turned on when the salinity <33.1 AND Water level < 35.

Once its on, it then runs until water level> 40, then turns off?

//if salinty <33.1 AND waterlevel < 35, top up with SALT water until WL = 40
if (SALduration==false && Watduration==true) SALTtopup=true;
if (SALTtopup==true) ReefAngel.Relay.On(Port2);
if (SALTtopup==true && ReefAngel.WaterLevel.GetLevel()>40)
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Help with salinity ATO using water level expansion

Post by rimai »

SALduration==false happens all the time....
So, if the reading is less than 33.1 even for a split second, it will turn port 2 on and it will only turn port2 off when wl>40.
Roberto.
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Help with salinity ATO using water level expansion

Post by rimai »

Place some Serial.println in your code to monitor debug and see how things are happening.
Roberto.
dazza1304
Posts: 154
Joined: Sat Aug 04, 2012 4:22 am

Re: Help with salinity ATO using water level expansion

Post by dazza1304 »

rimai wrote:SALduration==false happens all the time....
So, if the reading is less than 33.1 even for a split second, it will turn port 2 on and it will only turn port2 off when wl>40.
Yes, but Watduration==true will only be true if water level < 35 and thus port 2 should only turn on if both conditions are met?

if (SALduration==false && Watduration==true) SALTtopup=true;
if (SALTtopup==true) ReefAngel.Relay.On(Port2);

I am more than happy to try to debug myself, but dont know how to use the Serial.println. I know you tried to explain once, but you lost me to be honest!! If you could point me to a relevant post or tutorial I will see what I can do!!
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Help with salinity ATO using water level expansion

Post by rimai »

Yes, but if wl was <35 and salinity blipped below 33.1 for a micro second, it would turn port2.
Here is some resource for serial debugging:
http://www.ladyada.net/learn/arduino/lesson4.html
http://my.safaribooksonline.com/book/ho ... rom_arduin
RA uses 57600 baud rate.
Roberto.
dazza1304
Posts: 154
Joined: Sat Aug 04, 2012 4:22 am

Re: Help with salinity ATO using water level expansion

Post by dazza1304 »

Got it!!

I understand now - guess its a good idea to "debounce" sensor inputs - I had done this for the water level and salinity high, but not salinity low.

Anyway, re done my code, as I figured I only want to trigger the debounce for salinity high and low when my water level is low.

Anyway, here is my updated code, which works fine!
#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 <InternalEEPROM.h>
#include <RA_Colors.h>
#include <RA_CustomColors.h>
#include <Salinity.h>
#include <RF.h>
#include <IO.h>
#include <ORP.h>
#include <AI.h>
#include <PH.h>
#include <WaterLevel.h>
#include <ReefAngel.h>

////// Place global variable code below here
unsigned long lastLowSal=now();//salinity >33.1 timer
unsigned long lastHighSal=now();//salinity <33.1 timer
boolean SALduration=false;//salinity >33.1 2 minute debounce
boolean SALdurationHigh=false;//salinity <33.1 2 minute debounce
double Salinitytest;
unsigned long lastLoWat=now();//WL < 35 timer
boolean Watduration=false;//WL <35 2 minute debounce
boolean ROtopup = false;
boolean SALTtopup = false;


////// Place global variable code above here


void setup()
{
    // This must be the first line
    ReefAngel.Init(); //Initialize controller
    ReefAngel.SetTemperatureUnit( Celsius ); // set to Celsius Temperature

    // Ports toggled in Feeding Mode
    ReefAngel.FeedingModePorts = 0;
    ReefAngel.FeedingModePortsE[0] = 0;
    // Ports toggled in Water Change Mode
    ReefAngel.WaterChangePorts = Port1Bit | Port2Bit;
    ReefAngel.WaterChangePortsE[0] = 0;
    // Ports toggled when Lights On / Off menu entry selected
    ReefAngel.LightsOnPorts = 0;
    ReefAngel.LightsOnPortsE[0] = 0;
    // Ports turned off when Overheat temperature exceeded
    ReefAngel.OverheatShutoffPorts = Port6Bit;
    ReefAngel.OverheatShutoffPortsE[0] = 0;
    // Use T1 probe as temperature and overheat functions
    ReefAngel.TempProbe = T1_PROBE;
    ReefAngel.OverheatProbe = T1_PROBE;
    // Set the Overheat temperature setting
    InternalMemory.OverheatTemp_write( 260 );
    InternalMemory.SalTempComp_write( 255); // Change this to compensation temperature


    // Ports that are always on
    ReefAngel.Relay.On( Port3 );
    ReefAngel.Relay.On( Port4 );
    ReefAngel.Relay.On( Port5 );
    ReefAngel.Relay.On( Port8 );
    ReefAngel.Relay.On( Box1_Port1 );
    ReefAngel.Relay.On( Box1_Port2 );
    ReefAngel.Relay.On( Box1_Port4 );
    ReefAngel.Relay.On( Box1_Port6 );
    ReefAngel.Relay.On( Box1_Port7 );
    ReefAngel.Relay.On( Box1_Port8 );
    
    // Ports that are always off
    ReefAngel.Relay.Off( Port1 );
    ReefAngel.Relay.Off( Port2 );
     
    
    ////// Place additional initialization code below here
    
  
    
    ////// Place additional initialization code above here
}

void loop()
{
    ReefAngel.StandardHeater( Port6,252,255 );
    ReefAngel.StandardFan( Port7,255,258 );
    ////// Place your custom code below here
    
    // Apply Salinity temp Compensation & salinity calibration
    double SalCompensation;
    if (ReefAngel.TempSensor.unit)
    SalCompensation=ReefAngel.Params.Salinity/(1+((ReefAngel.Params.Temp[T3_PROBE]-InternalMemory.SalTempComp_read())*0.0022));
    else
    SalCompensation=ReefAngel.Params.Salinity/(1+((ReefAngel.Params.Temp[T3_PROBE]-InternalMemory.SalTempComp_read())*0.001165));
    SalCompensation=round(SalCompensation);
    
    // For displaying salinity without temp compensation
    Salinitytest=SalCompensation;
    
    ///// If WL < 35 for 120 sec set Watduration flag;Waterlevel debounce
    if (ReefAngel.WaterLevel.GetLevel()>35) lastLoWat=now();
    if (now()-lastLoWat>120) Watduration=true;
    
    if (Watduration==true)
    { 
    // If salinity >33.1 for 120 sec set SALduration flag;salinity debounce
    if (SalCompensation<331) lastLowSal=now();
    if (now()-lastLowSal>120) SALduration=true;
    if (SalCompensation>331) lastHighSal=now();
    if (now()-lastHighSal>120) SALdurationHigh=true;
    }
    else
    {
      lastLowSal=now();
      lastHighSal=now();
    }
    
    //if salinty >33.1 and waterlevel <35, top up with RO water until WL =40
    if (SALduration==true && Watduration==true) ROtopup=true;
    if (ROtopup==true) ReefAngel.Relay.On(Port1);
    if (ROtopup==true && ReefAngel.WaterLevel.GetLevel()>40)
    
    {
      ReefAngel.Relay.Off(Port1);
      ROtopup=false;
      SALduration=false;
      Watduration=false;
    }
    
    //if salinty <33.1 and waterlevel < 35, top up with SALT water until WL = 40
    if (SALdurationHigh==true && Watduration==true) SALTtopup=true;
    if (SALTtopup==true) ReefAngel.Relay.On(Port2);
    if (SALTtopup==true && ReefAngel.WaterLevel.GetLevel()>40)
    
    {
      ReefAngel.Relay.Off(Port2);
      SALdurationHigh=false;
      SALTtopup=false;
      Watduration=false;
    }

    // if skimmer cup full, turn off skimmer
    if (ReefAngel.HighATO.IsActive()) {
    bitClear(ReefAngel.Relay.RelayMaskOff,2);
    }
    else {
    bitSet(ReefAngel.Relay.RelayMaskOff,2);
    }
    
    ////// Place your custom code above here

    // This should always be the last line
    ReefAngel.Portal( "dazza1304" );
    ReefAngel.ShowInterface();
}

void DrawCustomMain()
{
    int x,y;
    char text[10];
    // Parameters
#if defined DisplayLEDPWM && ! defined RemoveAllLights
    ReefAngel.LCD.DrawMonitor( 15, 10, ReefAngel.Params,
    ReefAngel.PWM.GetDaylightValue(), ReefAngel.PWM.GetActinicValue() );
#else // defined DisplayLEDPWM && ! defined RemoveAllLights
    ReefAngel.LCD.DrawMonitor( 15, 10, ReefAngel.Params );
#endif // defined DisplayLEDPWM && ! defined RemoveAllLights
    pingSerial();

    // Salinity
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,15,46, "SAL:" );
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,39,46, ReefAngel.Params.Salinity );
    pingSerial();
    
    //map of adc temp comp salinity
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,15,63, "SAT:" );
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,39,63, (int)Salinitytest );

    // ORP
    ReefAngel.LCD.DrawText( COLOR_PALEVIOLETRED,DefaultBGColor,75,46, "ORP:" );
    ReefAngel.LCD.DrawText( COLOR_PALEVIOLETRED,DefaultBGColor,99,46, ReefAngel.Params.ORP );
    pingSerial();

    // Water Level
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,75,63, "WL:" );
    ReefAngel.LCD.DrawText( COLOR_BLACK,DefaultBGColor,99,63, ReefAngel.WaterLevel.GetLevel() );
    pingSerial();

    // Main Relay Box
    byte TempRelay = ReefAngel.Relay.RelayData;
    TempRelay &= ReefAngel.Relay.RelayMaskOff;
    TempRelay |= ReefAngel.Relay.RelayMaskOn;
    ReefAngel.LCD.DrawOutletBox( 12, 79, TempRelay );
    pingSerial();

    // Relay Expansion
    TempRelay = ReefAngel.Relay.RelayDataE[0];
    TempRelay &= ReefAngel.Relay.RelayMaskOffE[0];
    TempRelay |= ReefAngel.Relay.RelayMaskOnE[0];
    ReefAngel.LCD.DrawOutletBox( 12, 98, TempRelay );
    pingSerial();

    // Date and Time
    ReefAngel.LCD.DrawDate( 6, 122 );
    pingSerial();
}

void DrawCustomGraph()
{
}

rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Help with salinity ATO using water level expansion

Post by rimai »

Awesome!!!
Roberto.
Post Reply