Timing Issues

I am trying to have a fan trigger at an value stored in a array. After the 3rd hour and starts to through a negative number. Any ideas?

int FanTimeArray[9] = {
  24,24, 23, 22, 21, 20,19,18,24};

void FanRelay(){

  relayTriggerInterval=86400000/FanTimeArray[Day];
 
    if(Day<8){     
      if(millis() - relayMillis > relayTriggerInterval){
      relayMillis=millis();
      }
}

I am trying to have a fan trigger at an value stored in a array. After the 3rd hour starts it begins to give me a negative number. Any ideas?

int FanTimeArray[9] = {
  24,24, 23, 22, 21, 20,19,18,24};

void FanRelay(){

  relayTriggerInterval=86400000/FanTimeArray[Day];
 
    if(Day<8){     
      if(millis() - relayMillis > relayTriggerInterval){
      relayMillis=millis();
      }
}

Post more of your code please. How is relayTriggerInterval defined?

int DayCount;
float relayMillis;
float Hour,Minute,Second,ms;
float Day;
unsigned long start, finished, elapsed;

int FanTimeArray[9] = {
  0,24, 23, 22, 21, 20,19,18,24}; // In Hours

void setup() {

  pinMode(12,OUTPUT);

  Serial.begin(9600);

}


void loop() {

  intiatClock();
  FanRelay();

}


void intiatClock()
{

  unsigned long over;
  start=millis();
  elapsed=finished-start;
  Hour=1+int(start/3600000);
  over=start%3600000;
  Minute=1+int(over/60000);
  over=over%60000;
  Second=int(over/1000);
  ms=over%1000;
  Day=1+int(start/86400000);
}




void FanRelay(){

  long relayTriggerInterval;
  long relayTriggerInterval_after8;
  int intDay;
  
  intDay=Day; //Change to int
  
  relayTriggerInterval=86400000/FanTimeArray[intDay];


    if(intDay<8){
      if(millis() - relayMillis > relayTriggerInterval){
        relayMillis = millis(); 
      }
     else{
       //Do nothing
     } 
    
    }
}
  relayTriggerInterval=86400000/FanTimeArray[Day];

In the absence of directives to the contrary, literal constants are interpreted as ints. That doesn't look like a value that would fit in an int.

You might try
relayTriggerInterval=86400000UL/FanTimeArray[Day];

I don't know if this is your bug, but it is curious that relayMillis is declared float. What happens if you make it unsigned long?

-br

Edit: Hour, minute, second, etc could all be int, too, yes?

Cross-posted ... please delete one or the other.
http://arduino.cc/forum/index.php/topic,148841.0/topicseen.html

Cross-posted ... please delete one or the other.
http://arduino.cc/forum/index.php/topic,148838.0.html

How is this different from your post in Project Guidance?

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

Threads merged.

  • Moderator

Thank you for merging the w posts. It will not happen again.

What does 'finished' do? It never changes.

PaulS:

  relayTriggerInterval=86400000/FanTimeArray[Day];

In the absence of directives to the contrary, literal constants are interpreted as ints. That doesn't look like a value that would fit in an int.

You might try
relayTriggerInterval=86400000UL/FanTimeArray[Day];

Not if they won't fit into an int.

Test:

void setup ()
  {
  Serial.begin (115200);
  unsigned long relayTriggerInterval=86400000;
  Serial.println (relayTriggerInterval);
  }  // end of setup

void loop () { }

Output:

86400000

In this example, though, what PaulS said applies:

void setup ()
  {
  Serial.begin (115200);
  unsigned long relayTriggerInterval=864 * 100 * 1000;
  Serial.println (relayTriggerInterval);
  }  // end of setup

void loop () { }

Output:

23552

Since each number in the expression fits into an int, the whole thing is evaluated as an int, giving incorrect results.

This is backwards too:

start=millis();
elapsed=finished-start;

Should always be this format:
elapsed time = (most recently captured millis/micros) - (earlier capture millis/micros);

Then when millis/micros roll over, the result be expected number, for example
millis after rollover - millis before rollover = elapsed millis :

0x00000100 - 0xFFFFFF00 = 0x00000200

One other thing I failed to mention, Day 1 should stay the same for 24 hours and should point to the second position in the array. As soon as it puts out a negative number (at the 3rd hour) it also starts to point to a different position in the Fan array. As you can tell I am new to this so I apologize if the answer is completely obvious.

if the time related variables are all type unsigned long, there will be no negative numbers.
just comes down to logic flaws at that point.