Pages: 1 2 [3]   Go Down
Author Topic: Millis() Timing goes way off over time.  (Read 1249 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 2
Posts: 156
It was all digital
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

This is how I would have done it...

Code:
const unsigned long Sec = 1000L;
const unsigned long Min = 60L * Sec;
const unsigned long Hr = 60L * Min;

//zone 1:
const unsigned long zone_1_Off = 1 * Min; // 1 min
const unsigned long zone_1_On = 5 * Sec; // 5 sec
unsigned long zone_1_Start = 0;
unsigned long zone_1_Stop = zone_1_On;
const int zone_1_Pin = 13;

//zone 2:
const unsigned long zone_2_Off = 30 * Min; // 30 min
const unsigned long zone_2_On = 15 * Sec; // 15 sec
unsigned long zone_2_Start = 0;
unsigned long zone_2_Stop = zone_2_On;
const int zone_2_Pin = 12;


unsigned long myTime = 0;

void setup(){
  pinMode(zone_1_Pin,OUTPUT);
  pinMode(zone_1_Pin,OUTPUT);
}

void loop(){
  myTime = millis();

  // zone 1 logic
  if (zone_1_Start < myTime){digitalWrite(zone_1_Pin,HIGH);}
  if (zone_1_Stop < myTime){
    digitalWrite(zone_1_Pin,LOW);
    zone_1_Start = myTime + zone_1_Off;
    zone_1_Stop = zone_1_Start + zone_1_On;
  }
  
  // zone 2 logic
  if (zone_2_Start < myTime){digitalWrite(zone_2_Pin,HIGH);}
  if (zone_2_Stop < myTime){
    digitalWrite(zone_2_Pin,LOW);
    zone_2_Start = myTime + zone_2_Off;
    zone_2_Stop = zone_2_Start + zone_2_On;
  }
}

-Fletcher
« Last Edit: March 05, 2012, 06:20:27 am by Fletcher Chr » Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4016
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just something to look out for ... What will happen when Millis() overflows --- won't you be waiting another 36 hours or so for previousmillis to catch-up ????

Maybe I am wrong ... has happened before ;-)



Think how it works with a clock. Same way with unsigned numbers. What is 1 pm - 11 am? 2 hours.

Windoze calculator if View is set to Scientific allows math done in HEX, DEC, or BIN, but no unsigned's I see. Still, at times it helps seeing what the bits do.

Logged

Examples can be found in your IDE.

West Des Moines, Iowa USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 428
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Another coding strategy for controlling the four zones independently/asynchronously...

Code:
// uses files attached to http://arduino.cc/forum/index.php/topic,93959.0.html
/*----------------------------------------------------------------------------*/
/* Independent control of zone 1                                              */
/*----------------------------------------------------------------------------*/
time_t z1on,z1off;                     /* Zone 1 on, off durations            */

void zone1off(void)
{  digitalWrite(relayPin1,LOW);        /* Turn off zone flow                  */
   aftr(z1off,zone1on);                /* After off interval, turn it back on */
}                                      /*  end: zone1off()                    */
void zone1on(void);
{  digitalWrite(relayPin1,HIGH);       /* Turn on zone flow                   */
   aftr(z1on,zone1off);                /* After on interval, turn it back off */
}                                      /*  end: zone1on()                     */
void zone1(time_t on,time_t off)
{  z1on = on;                          /* Save on interval                    */
   z1off = off;                        /* Save off interval                   */
   zone1on();                          /* Start with an on cycle              */
}                                      /*  end: zone1()                       */
/*----------------------------------------------------------------------------*/
/* Independent control of zone 2                                              */
/*----------------------------------------------------------------------------*/
time_t z2on,z2off;

void zone2off(void)
{  digitalWrite(relayPin2,LOW);
   aftr(z2off,zone2on);
}
void zone2on(void);
{  digitalWrite(relayPin2,HIGH);
   aftr(z2on,zone2off);
}
void zone2(time_t on,time_t off)
{  z2on = on;
   z2off = off;
   zone2on();
}
/*----------------------------------------------------------------------------*/
/* Independent control of zone 3                                              */
/*----------------------------------------------------------------------------*/
time_t z3on,z3off;

void zone3off(void)
{  digitalWrite(relayPin3,LOW);
   aftr(z3off,zone3on);
}
void zone3on(void);
{  digitalWrite(relayPin3,HIGH);
   aftr(z3on,zone3off);
}
void zone3(time_t on,time_t off)
{  z3on = on;
   z3off = off;
   zone3on();
}
/*----------------------------------------------------------------------------*/
/* Independent control of zone 4                                              */
/*----------------------------------------------------------------------------*/
time_t z4on,z4off;

void zone4off(void)
{  digitalWrite(relayPin4,LOW);
   aftr(z4off,zone4on);
}
void zone4on(void);
{  digitalWrite(relayPin4,HIGH);
   aftr(z4on,zone4off);
}
void zone4(time_t on,time_t off)
{  z4on = on;
   z4off = off;
   zone4on();
}
/*----------------------------------------------------------------------------*/
/* Standard Arduino setup() function                                          */
/*----------------------------------------------------------------------------*/
void setup(void)
{  pinMode(relayPin1,OUTPUT);
   pinMode(relayPin2,OUTPUT);
   pinMode(relayPin3,OUTPUT);
   pinMode(relayPin4,OUTPUT);
   /*-------------------------------------------------------------------------*/
   /* Start the four zones and assign each an 'on' and 'off' duration         */
   /*-------------------------------------------------------------------------*/
   zone1(ss(3),mm(30));                /* 3-second on, 30-minute off          */
   zone2(ss(3),mm(30));
   zone3(ss(3),mm(30));
   zone4(ss(3),mm(30));
}
void loop(void)
{  idle();
}

You'll want to add your pin definitions and customize the zone on and off durations, but shouldn't need to do anything else.   smiley-mr-green

Edited to repair stupid coding error!
« Last Edit: March 05, 2012, 09:37:49 am by Morris Dovey » Logged

There's always a better way!

Pages: 1 2 [3]   Go Up
Jump to: