Go Down

Topic: Timing Issues (Read 4452 times) previous topic - next topic

ScottCaval

Feb 14, 2013, 01:06 am Last Edit: Feb 14, 2013, 01:17 am by ScottCaval Reason: 1
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();
     }
}



ScottCaval

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



nickgammon

Post more of your code please. How is relayTriggerInterval defined?
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

ScottCaval

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



















PaulS

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];

billroy

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?

JChristensen


JChristensen


PaulS

How is this different from your post in Project Guidance?

nickgammon

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

Threads merged.

- Moderator
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

ScottCaval

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

nickgammon

What does 'finished' do? It never changes.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

nickgammon


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
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

nickgammon

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.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

CrossRoads

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
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up