Page 2 of 2

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

Posted: Wed Jul 27, 2016 9:17 am
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.

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

Posted: Wed Jul 27, 2016 10:21 am
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.

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

Posted: Sun Aug 07, 2016 3:17 pm
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.

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

Posted: Sun Aug 07, 2016 9:22 pm
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;

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

Posted: Mon Aug 08, 2016 7:29 am
by Smotz
Yes, thanks. I took it out for some reason. I put it on top of the function.