Page 1 of 2

Re: Turn on relay when WL is at 100% or >?

Posted: Sun Aug 03, 2014 3:47 pm
by lnevo
Replace it with static unsigned long. Forgot to put the static in before. And i think for the test you just want to do see if now()<stopTime

You just need to protect the assignment of stopTime to make sure its only run once or it will contnue updating as long as the reservoir is full.

Re: Turn on relay when WL is at 100% or >?

Posted: Sun Aug 03, 2014 5:25 pm
by Sacohen
This is what I put in and I'm still getting the same error message...

Code: Select all

static unsigned long Kalk_Mixing = 0;

  time_t stopTime = 0;
if (ReefAngel.WaterLevel.GetLevel(1)<=100) stopTime=now()+3600;

if (stopTime-now() < 3600) { ReefAngel.Relay.On(Port3); } else ReefAngel.Relay.Off(Port3); }
Error message is ...
expected unqualified-id before 'if'

Re: Turn on relay when WL is at 100% or >?

Posted: Sun Aug 03, 2014 6:07 pm
by lnevo
The error is not in that code IMO. Try going back...

Re: Turn on relay when WL is at 100% or >?

Posted: Sun Aug 03, 2014 6:08 pm
by lnevo
And theres still logic errors there, let me write you up something more comprehensive tomorrow.

Re: Turn on relay when WL is at 100% or >?

Posted: Sun Aug 03, 2014 6:17 pm
by Sacohen
The error is not in that code.

It comes up in the next line of code for my Auto Feeder. The part I may Blue.

//Mix Kalkwasser
static unsigned long Kalk_Mixing = 0;

time_t stopTime = 0;
if (ReefAngel.WaterLevel.GetLevel(1)<=100) stopTime=now()+3600;

if (stopTime-now() < 3600) { ReefAngel.Relay.On(Port3); } else ReefAngel.Relay.Off(Port3); }

//AutoFeeder
static unsigned long autofeeding = 0;

if ((now()%SECS_PER_DAY==64800)) //if it is 6 pm
{
ReefAngel.FeedingModeStart(); //START FEEDING MODE
}

if (ReefAngel.DisplayedMenu==FEEDING_MODE)
{
if ( autofeeding == 0 ) {
autofeeding = now(); //set the time of the start of feeding to variable feeding
}

if ((now()-autofeeding>=60) && (now()-autofeeding<=61)) //if between 60 and 61 seconds has past
{
ReefAngel.Relay.On(Feeder); //TURN FEEDER RELAY ON
}
else
{
ReefAngel.Relay.Off(Feeder); //TURN FEEDER RELAY OFF
}
} else {
if ( autofeeding > 0 ) {
autofeeding = 0;
}
}

Re: Turn on relay when WL is at 100% or >?

Posted: Mon Aug 04, 2014 4:28 am
by lnevo
Ok, the error is in my code

Code: Select all

if (stopTime-now() < 3600) { ReefAngel.Relay.On(Port3); } else ReefAngel.Relay.Off(Port3); } 
to

Code: Select all

if (stopTime-now() < 3600) { ReefAngel.Relay.On(Port3); } else { ReefAngel.Relay.Off(Port3); } 
But here's your better code :

Code: Select all

//Mix Kalkwasser
static unsigned long Kalk_Mixing = 0;

static unsigned long stopTime = 0;
static byte prevWL=0;

if (ReefAngel.WaterLevel.GetLevel(1)>=100 && preWL<100) {
  stopTime=now()+3600; // Get time one hour from now.
}
prevWL = WaterLevel.GetLevel(1); // Store the water level

if (now() < 3600) { ReefAngel.Relay.On(Port3); } else { ReefAngel.Relay.Off(Port3); } 

//AutoFeeder
static unsigned long autofeeding = 0;

if ((now()%SECS_PER_DAY==64800)) //if it is 6 pm
{
ReefAngel.FeedingModeStart(); //START FEEDING MODE
}

if (ReefAngel.DisplayedMenu==FEEDING_MODE)
{
if ( autofeeding == 0 ) {
autofeeding = now(); //set the time of the start of feeding to variable feeding
}

if ((now()-autofeeding>=60) && (now()-autofeeding<=61)) //if between 60 and 61 seconds has past
{
ReefAngel.Relay.On(Feeder); //TURN FEEDER RELAY ON
}
else 
{
ReefAngel.Relay.Off(Feeder); //TURN FEEDER RELAY OFF
}
} else {
if ( autofeeding > 0 ) {
autofeeding = 0;
}
}
I added a variable for the previous water level to help make sure this only runs once it's been filled. Hopefully it works.

Re: Turn on relay when WL is at 100% or >?

Posted: Mon Aug 04, 2014 4:31 am
by Sacohen
Thanks. I'll try it tonight.

Re: Turn on relay when WL is at 100% or >?

Posted: Mon Aug 04, 2014 11:36 am
by Sacohen
Lee;

I just tried the code you put up yesterday and the error message I'm getting now is...

'preWL' was not declared in this scope

but I see static byte prevWL=0; in the code.
I tried it as static byte prevWL = 0; and got the same message.

Re: Turn on relay when WL is at 100% or >?

Posted: Mon Aug 04, 2014 1:22 pm
by lnevo
Because the error is preWL without the V... I made a typo somewhere else...

Change this:

Code: Select all

if (ReefAngel.WaterLevel.GetLevel(1)>=100 && preWL<100) {
to

Code: Select all

if (ReefAngel.WaterLevel.GetLevel(1)>=100 && prevWL<100) {

Re: Turn on relay when WL is at 100% or >?

Posted: Mon Aug 04, 2014 4:22 pm
by Sacohen
Now I'm getting 'WaterLevel' was not declared in this scope.

The line in Blue is the one that is highlighted...

//Mix Kalkwasser
static unsigned long Kalk_Mixing = 0;

static unsigned long stopTime = 0;
static byte prevWL=0;

if (ReefAngel.WaterLevel.GetLevel(1)>=100 && prevWL<100) {
stopTime=now()+3600; // Get time one hour from now.
}
prevWL = WaterLevel.GetLevel(1); // Store the water level

if (now() < 3600) { ReefAngel.Relay.On(Kalkwasser); } else { ReefAngel.Relay.Off(Kalkwasser); }

I changed it to this and it compiled with now errors.
Is this the way it should be?

prevWL = ReefAngel.WaterLevel.GetLevel(1); // Store the water level

Re: Turn on relay when WL is at 100% or >?

Posted: Mon Aug 04, 2014 5:04 pm
by Sacohen
It doesn't seem to be working.
I have the ATO reservoir filled and I re-calibrated the sensor because even full it was reading 95%
It's now reading 100% on the RA menu and on my phone, but is reading 0 on the Portal and Port3 has not turned on.

Image

Image

Re: Turn on relay when WL is at 100% or >?

Posted: Mon Aug 04, 2014 5:11 pm
by lnevo
Yes you got the code right. Here's the scenario. The water level needs to be below the 100 percent mark and then hit 100 and it will then set stopTime to 3600 seconds in the future. Also found and error. it should be now()<stopTime not 3600. Carryover from the initial example. Sorry for always clutzing up on you. I code on my phone...

Re: Turn on relay when WL is at 100% or >?

Posted: Mon Aug 04, 2014 5:37 pm
by Sacohen
That looks like it did it after I fixed that last coding issue.

No problem with the mess ups, I just appreciate the help.

Besides I did figured some things (well 1) out on my own.

I'm starting to pick this up, but still have a long way to go.

Re: Turn on relay when WL is at 100% or >?

Posted: Tue Aug 05, 2014 5:25 am
by binder
Sacohen wrote:That looks like it did it after I fixed that last coding issue.

No problem with the mess ups, I just appreciate the help.

Besides I did figured some things (well 1) out on my own.

I'm starting to pick this up, but still have a long way to go.
no worries there. it is a continuous learning experience. i even have issues at times too! :-)

glad you were able to get things figured out.


Sent from my iPad mini

Re: Turn on relay when WL is at 100% or >?

Posted: Tue Aug 05, 2014 5:48 am
by Sacohen
Thanks. Curt. It was Lee that did it, but I was proud that I was able to figure out what was wrong when he mis-coded something. That's 1000x better then when I started in here.

Re: Turn on relay when WL is at 100% or >?

Posted: Wed Aug 06, 2014 5:53 am
by Sacohen
The final working code is...

Code: Select all

//Mix Kalkwasser
static unsigned long Kalk_Mixing = 0;

static unsigned long stopTime = 0;
static byte prevWL=0;

if (ReefAngel.WaterLevel.GetLevel(1)>=100 && prevWL<100) {
  stopTime=now()+3600; // Get time one hour from now.
}
prevWL = ReefAngel.WaterLevel.GetLevel(1); // Store the water level

if (now() < stopTime) { ReefAngel.Relay.On(Kalkwasser); } else { ReefAngel.Relay.Off(Kalkwasser); } 

Re: Turn on relay when WL is at 100% or >?

Posted: Sun Sep 14, 2014 6:18 am
by Sacohen
Lee thanks again for this code.
There seems to be some problems with it still.

When the Water Level reaches 0 the Kalkwasser pump (Port4) turns on.

That shouldn't happen because there is not enough water in the Reservoir to have it run without cavitating.

Also when the water is below 0 or at 0 for too long it starts reporting the WL at 200%.
This is not problem with the code I think it's an issue with the design of the WL sensors or when the ATO pulls more water and actually falls below the calibrated 0 level.

This causes another problem with the line of code that says and previous WL is less than 100, because it doesn't stay at less than 100.

I tried changing the code to equal to 100 and got "value required as left operand of assignment"

Code: Select all

static unsigned long Kalk_Mixing = 0;

static unsigned long stopTime = 0;
static byte prevWL=0;

if (ReefAngel.WaterLevel.GetLevel(1)=100 && prevWL<100) {   
  stopTime=now()+3600; // Get time one hour from now.
}
prevWL = ReefAngel.WaterLevel.GetLevel(1); // Store the water level

if (now() < stopTime) { ReefAngel.Relay.On(Kalkwasser); } else { ReefAngel.Relay.Off(Kalkwasser); } 

Re: Turn on relay when WL is at 100% or >?

Posted: Sun Sep 14, 2014 6:39 am
by lnevo
You need == that causes the error you had.

Its an issue with the water level sensor code. I'll have to check but yeah it wraps around to 200...and at 0 its probably jumping to 200 where you can't see it.

Re: Turn on relay when WL is at 100% or >?

Posted: Sun Sep 14, 2014 7:13 am
by Sacohen
OK. That did it.
I don't think it's jumping to 200% right away, but that would make sense because it gets to 0, jumps to 200 and turn on the port since it is above 100 like the code says to.

That 200% thing is going to cause an issue since it will not start the hour stirring process if the last level was not below 100.

This is the first step in automating my RO/DI and SW container refills.
My thought was when WL reaches 0% the RO/DI water turns on and starts filling whichever reservoir is low and calling for it to be turned on.

When it reaches 100% it shuts of the RO/DI maker and turns on the pump for an hour (Maybe 2 for the SW) and then the water in each reservoir is ready to use.

Obviously I would try to build in something that disables the ATO or AWC while the reservoir is refiling and mixing.

Re: Turn on relay when WL is at 100% or >?

Posted: Sun Sep 14, 2014 7:43 am
by lnevo
I would not start anything at the 0% mark :) i'd even suggest recalibrating so that 0% has some amount of liquid in it. But yeah it's like debouncing any other sensors, you may not see it because the screen refreshes every 5 seconds i believe but the sensor definitely has some bounce in it so it could be bouncing to 200 and you wouldnt se it necessarily. We need to fix that part. Maybe need to change the result to an int so we can get a negative percent.

Another thing we can do is read the raw value from the sensor and check if we're lower than the calibrated minimum...i'm not sure that function exists, but I've wanted it a few times...

Re: Turn on relay when WL is at 100% or >?

Posted: Sun Sep 14, 2014 7:49 am
by Sacohen
Ok. Right now. I'm testing an issue I had with the multi WL dropping faster then it actually is. Roberto wanted me swap my single with my multi and see if it moves with the swap or it it stays.
I put that off while working on the dev libraries with you guys.

Re: Turn on relay when WL is at 100% or >?

Posted: Sun Sep 14, 2014 8:01 am
by lnevo
No problem. Have to figure out the best way to solve the wrap around...constrain/negative number/something else...

Re: Turn on relay when WL is at 100% or >?

Posted: Sun Sep 14, 2014 8:01 am
by Sacohen
Ok. Thanks

Re: Turn on relay when WL is at 100% or >?

Posted: Sun Sep 14, 2014 11:10 am
by lnevo
So there is a function to get the raw value.

ReefAngel.WaterLevel.Read() and ReefAngel.WaterLevel.Read(byte Channel)

I took a quick look at the code and it's first doing a map and then a constrain, so I think the wrapping is happening first int the map function, because the constrain is from 0 to 200. So if it were less than 0 then it would be 0..

Re: Turn on relay when WL is at 100% or >?

Posted: Sun Sep 14, 2014 1:14 pm
by Sacohen
OK. You're speaking greek to me Lee, but it sounds like you can do something about it.
Again nothing major. When you can get to it.

Re: Turn on relay when WL is at 100% or >?

Posted: Sun Sep 14, 2014 2:11 pm
by lnevo
I dont have a solution yet. Although maybe I have an idea...just as I wrote that..

But for now you can do the read function above and compare it to the InternalMemory.WaterLevelMin_read() function to see if the raw uncalculated value is less than "0".

Re: Turn on relay when WL is at 100% or >?

Posted: Mon Sep 15, 2014 7:59 am
by lnevo
Ok, I figured out the issue!!!

Roberto, in the WaterLevel::Convert function... why is t an "unsigned" long. Here's the issue. First we do a map of the value read from the sensor to the min/max calibrated values. We assign this to t (which is unsigned...) So, if the value read is less than 0 we will get a negative number. Since t is unsigned, this causes the number to wrap around and thus we get an extremely large number instead of a negative. Then when we do the constrain so that the number gets held within the 0-200 range it gets set to 200.

So the solution is to either convert the unsigned long t to just long t or we have to wrap the first map with the constrain so that it's constrained to a positive number before assigning it to t. If we do that, then t could really just be a byte instead of an unsigned long.

Re: Turn on relay when WL is at 100% or >?

Posted: Mon Sep 15, 2014 8:05 am
by lnevo
Ok, I know why t is an unsigned long, because your using t to also de-bounce the sensor and taking 20 readings in a row. Therefore to not change this behavior and not introduce any other unexpected behavior, I recommend we do:

t=constrain(map(t, WLmin,WLmax,0,100),0,200);

That should prevent having the negative wrap during the assignment to t since t will always be constrained. Also I would propose changing the range to 0-255 just because we can :) and in code would look better because level[x] is a byte. So the constrain rather than be an arbitrary range is ensuring we stay within the confines of our destination variable.

If you agree, I'll create a pull request tonight.

Re: Turn on relay when WL is at 100% or >?

Posted: Mon Sep 15, 2014 8:15 am
by rimai
0-255 range is fine :)

Re: Turn on relay when WL is at 100% or >?

Posted: Mon Sep 15, 2014 8:21 am
by lnevo
Sounds good. I'll patch it up tonight! Anyone able to help test it? My reservoir is still half full and I don't feel like siphoning all the water out... :)