Well, got some floats and a pump, here is my ATO logic so far.
This creates an interrupt port that can trigger events:
Code:
static InputPort SumpLevel1 = new InterruptPort(Pins.GPIO_PIN_D8,false,Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
And then on startupI assign an event to happen if the interrupt fires (water drops) and check to see if the float is already down, if it is, the event won't fire as it fires when it goes down, not while down, so I have to manually fire the event:
Code:
SumpLevel1.OnInterrupt += new NativeEventHandler(waterLowEvent);
if (SumpLevel1.Read()==false) // check for low water on start up, if so fire the event manually.
{
waterLowEvent(0, 0, DateTime.Now);
}
Thread.Sleep(Timeout.Infinite);
Now, the event that is being fired, I replaced turning on / off relays with Debug.Prints of what is going on:
Code:
static void waterLowEvent(uint data1, uint data2, DateTime time)
{
if((DateTime.Now - _lastFire).Seconds < 30 || _doneFilling == false) return;
else
{
_lastFire = DateTime.Now;
_doneFilling = false;
}
ledOnboard.DutyCycle = 1;
Debug.Print("Pump Running");
while(SumpLevel1.Read()==false) //while still too low
{
Thread.Sleep(1000); //give it a 1 second pause so it doesn't flutter on / off
Debug.Print("still low on water, pump running!");
}
//finished filling, sensor says full.
Debug.Print("Pump Turns Off");
ledOnboard.DutyCycle = .25;
_doneFilling = true;
}
As you can see, I check to see if the event has fired in the last 30 seconds, and to make sure it's done filling (can't fire two events at once).
Then, I set a flag to say it's filling, and set the last fire time to right now. I light up an LED to tell me that the ATO is running, then go into a while loop. The while loop says basically "While the float stays down, wait for one second, then check again" once it's up, the pump turns off, i shut off the LED and set my variable of _doneFilling to true.
I'll add some additional floats for backup/etc and monitor the level of the ATO container as well eventually but it's a start to test the float.