More troubles.. Why didn't it go in to feeding mode?

Basic / Standard Reef Angel hardware
re76
Posts: 14
Joined: Tue Jul 19, 2016 7:55 am

Re: More troubles.. Why didn't it go in to feeding mode?

Post by re76 »

Smotz wrote:here is my updated code with the else's incorporated

Code: Select all

//FEEDING MODE
// Begin Feeding Mode
    if (((hour() == 7)) && (minute() == 30) || (hour() == 18) && (minute() == 00)) {
      ReefAngel.FeedingModeStart(); // FEEDING MODE @ 7:30 AM or 6:00 PM
      NoteTime = millis(); 
      unsigned long DeltaTS = millis() - NoteTime;
      if ( DeltaTS >= 130000 ) ReefAngel.Relay.Off(Feeder); // turn off 2 minutes 10 secs 
      else
      if ( DeltaTS >= 120000 ) ReefAngel.Relay.On(Feeder); // turn on after 2 minutes
      else
      if ( DeltaTS >= 70000 ) ReefAngel.Relay.Off(Feeder); // turn off 1 min 10 secs 
      else
      if ( DeltaTS >= 60000 ) ReefAngel.Relay.On(Feeder); // turn on after 1 minute
      }
You are definitely on the right track. This will almost work, but you are only giving everything 1 minute to complete, even though you have 2 minute and 10 seconds worth of events programmed. Here is how I would change it:

Code: Select all


// Static bool to track if we are currently feeding or not
static boolean feedingActive = false;

//FEEDING MODE
// Begin Feeding Mode
      if (((hour() == 7)) && (minute() == 30) || (hour() == 18) && (minute() == 00)) {
          ReefAngel.FeedingModeStart(); // FEEDING MODE @ 7:30 AM or 6:00 PM
          NoteTime = millis(); // Grab millis value of when feeding mode started
          feedingActive = true; // We are entering feeding mode, so set to true
      }

      if (feedingActive) {
          unsigned long DeltaTS = millis() - NoteTime;
          if ( DeltaTS >= 130000 ) {
              ReefAngel.Relay.Off(Feeder); // turn off 2 minutes 10 secs
              feedingActive = false; // Feeding mode is now over, set to false
          } else
          if ( DeltaTS >= 120000 ) ReefAngel.Relay.On(Feeder); // turn on after 2 minutes
          else
          if ( DeltaTS >= 70000 ) ReefAngel.Relay.Off(Feeder); // turn off 1 min 10 secs 
          else
          if ( DeltaTS >= 60000 ) ReefAngel.Relay.On(Feeder); // turn on after 1 minute
      }
The code above would even allow you to use feedingActive elsewhere in your code, if you wanted to do things based on whether or not you are in feeding mode.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: More troubles.. Why didn't it go in to feeding mode?

Post by lnevo »

For the record we don't need sub second tracking here. You could assign NoteTime=now() and DeltaTS being now()-NoteTime.

Then your comparisons would be clearer in seconds versus milli.

As I said earlier examples are in my code already to do this.
Smotz
Posts: 412
Joined: Sat Mar 30, 2013 5:02 pm
Location: CT, USA

Re: More troubles.. Why didn't it go in to feeding mode?

Post by Smotz »

re76 wrote:
Smotz wrote:here is my updated code with the else's incorporated

Code: Select all

//FEEDING MODE
// Begin Feeding Mode
    if (((hour() == 7)) && (minute() == 30) || (hour() == 18) && (minute() == 00)) {
      ReefAngel.FeedingModeStart(); // FEEDING MODE @ 7:30 AM or 6:00 PM
      NoteTime = millis(); 
      unsigned long DeltaTS = millis() - NoteTime;
      if ( DeltaTS >= 130000 ) ReefAngel.Relay.Off(Feeder); // turn off 2 minutes 10 secs 
      else
      if ( DeltaTS >= 120000 ) ReefAngel.Relay.On(Feeder); // turn on after 2 minutes
      else
      if ( DeltaTS >= 70000 ) ReefAngel.Relay.Off(Feeder); // turn off 1 min 10 secs 
      else
      if ( DeltaTS >= 60000 ) ReefAngel.Relay.On(Feeder); // turn on after 1 minute
      }
You are definitely on the right track. This will almost work, but you are only giving everything 1 minute to complete, even though you have 2 minute and 10 seconds worth of events programmed. Here is how I would change it:

Code: Select all


// Static bool to track if we are currently feeding or not
static boolean feedingActive = false;

//FEEDING MODE
// Begin Feeding Mode
      if (((hour() == 7)) && (minute() == 30) || (hour() == 18) && (minute() == 00)) {
          ReefAngel.FeedingModeStart(); // FEEDING MODE @ 7:30 AM or 6:00 PM
          NoteTime = millis(); // Grab millis value of when feeding mode started
          feedingActive = true; // We are entering feeding mode, so set to true
      }

      if (feedingActive) {
          unsigned long DeltaTS = millis() - NoteTime;
          if ( DeltaTS >= 130000 ) {
              ReefAngel.Relay.Off(Feeder); // turn off 2 minutes 10 secs
              feedingActive = false; // Feeding mode is now over, set to false
          } else
          if ( DeltaTS >= 120000 ) ReefAngel.Relay.On(Feeder); // turn on after 2 minutes
          else
          if ( DeltaTS >= 70000 ) ReefAngel.Relay.Off(Feeder); // turn off 1 min 10 secs 
          else
          if ( DeltaTS >= 60000 ) ReefAngel.Relay.On(Feeder); // turn on after 1 minute
      }
The code above would even allow you to use feedingActive elsewhere in your code, if you wanted to do things based on whether or not you are in feeding mode.

So I was looking at this code and doing some testing and learned that it doesn't compile because the second part declares "unsigned long DeltaTS = millis() - NoteTime;" based on the if/then condition above it. It says that NoteTime was not declared.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: More troubles.. Why didn't it go in to feeding mode?

Post by lnevo »

You said you had NoteTime declared globally earlier. If not, just put unsigned long NoteTime either in the top of the function or in a global declaration.

unsigned long NoteTime;
Smotz
Posts: 412
Joined: Sat Mar 30, 2013 5:02 pm
Location: CT, USA

Re: More troubles.. Why didn't it go in to feeding mode?

Post by Smotz »

Yes, thanks. I took it out for some reason. I put it on top of the function.
Post Reply