Page 1 of 1

Why is this function causing the RA to restart?

Posted: Mon Dec 29, 2014 1:17 pm
by gaberosenfield
Hi everyone,

I wrote some code to carry out an automatic water change (AWC) with a bunch of checks built in. My AWC() function returns a boolean that = true if the AWC succeeded and = false if it failed for any reason. The problem is that whenever I manually call this function on my RA using a custom menu option, the RA just goes blank for about one second and then appears to reboot. Here's the code code for the function:

Code: Select all

//This function will carry out an automatic water change.
boolean AWC()
{ 
  if (!ReefAngel.Relay.Status(returnPump))  //If the return pump is not on, don't run an AWC and return 0 (false).
  {
    return 0;
  }
  
  if (!ReefAngel.HighATO.IsActive())
  {
    AWCTimer = now();  
    
    ReefAngel.Relay.On(ATOPump);  //Fill the return section to the high float with RODI.
    
    while (true)
    {
      if (ReefAngel.HighATO.IsActive())
      {
        ReefAngel.Relay.Off(ATOPump);  //Turn off the RODI pump once the high float is triggered.
      
        break;
      }
      else if ((now() - AWCTimer) >= ATOTimeOut)
      {
        ReefAngel.Relay.Off(ATOPump);  //Turn off the RODI pump.
        
        bitSet(ReefAngel.AlertFlags,ATOTimeOutFlag);  //Trigger ATO timeout flag.
        
        return 0;  //Return 0 (false) if the ATO times out.
      }
    }
  }
  
  ATOOn = false;
  
  AWCTimer = now();
  
  ReefAngel.Relay.On(AWCDrainPump);  //Turn on the AWC drain pump.
  
  while (true)
  {
    if (ReefAngel.LowATO.IsActive())
    {
      ReefAngel.Relay.Off(AWCDrainPump);  //Turn off the AWC drain pump.
      
      break;
    }
    else if ((now() - AWCTimer) >= AWCDrainSecs)
    {
      ReefAngel.Relay.Off(AWCDrainPump);  //Turn off the AWC drain pump.
      
      bitSet(ReefAngel.AlertFlags,ATOTimeOutFlag);  //Trigger ATO timeout flag.
      
      ATOOn = true;
      
      return 0;  //Return 0 (false) if the AWC times out.
    }
  }
  
  AWCTimer = now();
  
  ReefAngel.Relay.On(AWCFillPump);  //Turn on the AWC fill pump.
  
  while (true)
  {
    if (ReefAngel.HighATO.IsActive())
    {
      ReefAngel.Relay.Off(AWCFillPump);  //Turn off the AWC fill pump.
      
      break;
    }
    else if ((now() - AWCTimer) >= AWCFillSecs)
    {
      ReefAngel.Relay.Off(AWCFillPump);  //Turn off the AWC fill pump.
      
      bitSet(ReefAngel.AlertFlags,ATOTimeOutFlag);  //Trigger ATO timeout flag.
      
      ATOOn = true;
      
      return 0;  //Return 0 (false) if the AWC times out.
    }
  }
  
  ATOOn = true;
  
  return 1;  //If the AWC ran successfully, return 1 (true).
}
Any ideas? I can also post my entire INO, but I am pretty sure I declared the variables used in this function earlier in my code correctly and I don't know what else in my code this would be interfering with that would cause a reboot.

Thanks in advance for the help!

Re: Why is this function causing the RA to restart?

Posted: Mon Dec 29, 2014 3:54 pm
by lnevo
You can't run things in a while loop. You'll have to change the way you do things to be stateful. What is happening is your locking up the RA in your loop and triggering the watchdog timer. The main RA function loop() is already doing a loop so you just need flags and statuses to see where in the WC process you are. Code looks good otherwise just needs a little adaptation to how the RA functions.

Re: Why is this function causing the RA to restart?

Posted: Wed Dec 31, 2014 12:56 am
by gaberosenfield
Thanks for the tip Inevo!

I rewrote my code such that the only loop is the main RA loop() function. Now my AWC function works perfectly. Sadly, my code is now much uglier; but function over form I always say!

Best,
Gabe

Re: Why is this function causing the RA to restart?

Posted: Wed Dec 31, 2014 6:51 am
by lnevo
Start a thread I'd like to see what you've done. Can prob offer some tips to clean it up as well :)