Arduino Forum

Using Arduino => Programming Questions => Topic started by: ScottCaval on Feb 14, 2013, 01:06 am

Title: Timing Issues
Post by: ScottCaval on Feb 14, 2013, 01:06 am
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?

Code: [Select]

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();
     }
}


Title: Timing issues
Post by: ScottCaval on Feb 14, 2013, 01:20 am
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?

Code: [Select]

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();
     }
}


Title: Re: Timing issues
Post by: nickgammon on Feb 14, 2013, 01:26 am
Post more of your code please. How is relayTriggerInterval defined?
Title: Re: Timing issues
Post by: ScottCaval on Feb 14, 2013, 01:32 am
Code: [Select]


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
     }
   
    }
}


















Title: Re: Timing Issues
Post by: PaulS on Feb 14, 2013, 01:33 am
Code: [Select]
  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];
Title: Re: Timing issues
Post by: billroy on Feb 14, 2013, 01:36 am
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?
Title: Re: Timing Issues
Post by: JChristensen on Feb 14, 2013, 01:40 am
Cross-posted ... please delete one or the other.
http://arduino.cc/forum/index.php/topic,148841.0/topicseen.html
Title: Re: Timing issues
Post by: JChristensen on Feb 14, 2013, 01:41 am
Cross-posted ... please delete one or the other.
http://arduino.cc/forum/index.php/topic,148838.0.html
Title: Re: Timing issues
Post by: PaulS on Feb 14, 2013, 01:41 am
How is this different from your post in Project Guidance?
Title: Re: Timing Issues
Post by: nickgammon on Feb 14, 2013, 01:43 am
Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

Threads merged.

- Moderator
Title: Re: Timing Issues
Post by: ScottCaval on Feb 14, 2013, 01:45 am
Thank you for merging the w posts. It will not happen again.
Title: Re: Timing Issues
Post by: nickgammon on Feb 14, 2013, 01:48 am
What does 'finished' do? It never changes.
Title: Re: Timing Issues
Post by: nickgammon on Feb 14, 2013, 01:50 am

Code: [Select]
  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:

Code: [Select]

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

void loop () { }


Output:

Code: [Select]

86400000
Title: Re: Timing Issues
Post by: nickgammon on Feb 14, 2013, 01:53 am
In this example, though, what PaulS said applies:

Code: [Select]

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

void loop () { }


Output:

Code: [Select]

23552


Since each number in the expression fits into an int, the whole thing is evaluated as an int, giving incorrect results.
Title: Re: Timing Issues
Post by: CrossRoads on Feb 14, 2013, 01:57 am
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
Title: Re: Timing Issues
Post by: ScottCaval on Feb 14, 2013, 02:06 am
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.
Title: Re: Timing Issues
Post by: CrossRoads on Feb 14, 2013, 02:25 am
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.