replacing delay() using millis()

hey guys can someone help me replacing delay() with millis() in my code,
i want to use it for couting number of days so using delay will hang my code

#define LATCH 12
#define CLK 13
#define DATA 11

//This is the hex value of each number stored in an array by index num
int digitOne[10]= {192,249,164,176,153,146,130,248,128,24};
int digitTwo[10]= {192,249,164,176,153,146,130,248,128,24};

int i;

void setup(){
 
  pinMode(LATCH, OUTPUT);
  pinMode(CLK, OUTPUT);
  pinMode(DATA, OUTPUT);
 
}

void loop(){
 
  for(int i=0; i<10; i++){
    for(int j=0; j<10; j++){
      digitalWrite(LATCH, LOW);
      shiftOut(DATA, CLK, MSBFIRST, ~digitTwo[j]); // digitTwo
      shiftOut(DATA, CLK, MSBFIRST, ~digitOne[i]); // digitOne
      digitalWrite(LATCH, HIGH);
      delay(86400000);//this is 1 days in mseconds
    }
      }
        }

Moderator edit: CODE TAGS. Why is it so difficult?

walterkim:
hey guys can someone help me replacing delay() with millis() in my code,
i want to use it for couting number of days so using delay will hang my code

This will wait 21 days without using delay() (but your code will be just as hung):

void loop(){
  for(int i=0; i<10; i++){
    for(int j=0; j<10; j++){
      digitalWrite(LATCH, LOW);
      shiftOut(DATA, CLK, MSBFIRST, ~digitTwo[j]); // digitTwo
      shiftOut(DATA, CLK, MSBFIRST, ~digitOne[i]); // digitOne
      digitalWrite(LATCH, HIGH);
      unsigned long startTime = millis();
      while (millis() - startTime < (21UL*24UL*60UL*60UL*1000UL)); //wait 21 days
    }
  }
}

I'm guessing you want the code to act like the inner loop executes only every 21 days. To do that you have to invert your loops with something like:

 void loop() {
  static int outerLoop = 0;
  static int innerLoop = 0; 
  static unsigned long startTime = 0;
  if (startTime == 0 || millis()-startTime > (21UL*24UL*60UL*60UL*1000UL)) {
    // Do the inner loop
    startTime = millis();
    innerLoop++;
    if (innerLoop > 9) {
      innerLoop = 0;
      outerLoop++;
      if (outerLoop > 9)
        outerLoop = 0;
    }
  }
}

what i want is counting number of days
am building counter for chicken egg incubator that every day will count and show on the seven segment display until 21 days

in my project i have

  1. arduino board
  2. two seven segment display(common cathod)
  3. shift register 74HC595

So my issue is when i want it to count using milis() for interval of every 24hrs(every day)

i want counting after every 24hrs

your (main) problem is NOT that you're using delay(), your program is not doing anything else. Your issue is how long you've set it for. Your code is "hanging" because you have a 21 day delay BETWEEN each successive digit change. Change your delay to 1 day.

BTW - your comment at the top is incorrect - those are decimal values - NOT hex values.

also your code for digit 9 is incorrect - you're lighting the decimal point as well - it should be 152 (dec) 0x98 (hex).

also you don't need TWO identical arrays - one will suffice.

also doing a bitwise compliment (~) of the numbers in the array each time around is unnecessary if you put the appropriate compliments IN the array to start with

Finally - your counter will start at 00 for the first day - is that what you intend?

So what you WANT to do is:

#define LATCH 12
#define CLK 13
#define DATA 11

//This is the hex value of each number stored in an array by index num
int digits[10]= {
  192,249,164,176,153,146,130,248,128,24};

void setup(){
  pinMode(LATCH, OUTPUT);
  pinMode(CLK, OUTPUT);
  pinMode(DATA, OUTPUT);
}

void loop() {
  // Display elapsed time in days
  for(int i=0; i<100; i++) {
      digitalWrite(LATCH, LOW);
      shiftOut(DATA, CLK, MSBFIRST, ~digits[i/10]); // digitTwo
      shiftOut(DATA, CLK, MSBFIRST, ~digits[i%10]); // digitOne
      digitalWrite(LATCH, HIGH);
      delay(24UL*60UL*60UL*1000UL); //this is 1 day in mseconds
//      delay(60UL*1000UL); //this is 1 minute in mseconds (for testing)
    }
  }

yes i want a delay of 24hrs

my intension is the execution after every 24hrs and not 21 days
so this kind of hanging is only 24 hrs while i want some other things to proceed

johnwaser,from your post number 5,that code will hang due to the presence of delay...
for me i want somethng that will not hang but countingup every 24hrs for the 21 days of incubation

You appear to have ignored my post which gave you your solution. John's method is no different except that he uses a single loop rather than a double loop. BOTH will "hang" for 1 day and then update your display - which is what you say you want.

If you re-structure your whole approach to use millis() instead of delay() it will do nothing differently because your code is not doing anything else for a day anyway.

If you want to work with very long periods you need to use a Real Time Clock module because the Arduino millis() won't keep time well enough.

...R

thanks yaafm for your comments, i real appriciate

-you say about my comment i put as hex while its decimal value,thanks for that

  • you talk of my code for 9 digit is incorrect i dont understand cz its giving me digit 9 when i use small delay like delay(500)... i will see digits from 00 to 99 with even a dot point

  • yaafm may you please show me how i can use one array instead of two arrays

  • why have you said bitwise compliment (~) is unnecessary?

MAINLY: yaafm my main problem is code to hang for 24hrs if i try to put like this delay(24hours);

but i want a way that i will track time without hanging some other codes.

Robin advice me to use RTC which is very true especial on reseting but for this time am interest to track the time for 24hrs then later on i will see on how to use RTC....

SO how can i use millis() in that code?

thanks guys,

i have some other codes for controlling motor which turns clockwise and antclockwise after every 8 hours,
temperature sensor,humidity sensor,heater,fan,and LCD display
so you can see using delay in the counter will cost the entire code....so if will get solution to use mills() instead of delay during counting i think will help me alot

walterkim:
SO how can i use millis() in that code?

The demo several things at a time illustrates how to use millis().

Even over 24hrs millis() will probably drift noticeably from clock time. But at least if you modify your code to use millis() it won't be too difficult to change from millis() to the RTC.

...R

So after all this AND a DOUBLE POST you FINALLY reveal after TWELVE POSTS that you want to do other things (and with other until now unmentioned hardware) DURING the 24 hour period.

-am sorry yaaf for not demonstrate it so clear but am new in arduino so i just need your assistance...

You could use an RTC. That is way more accurate than using millis() and you won't have to hang the processor either. It can be doing other things too and check the RTC every now and then and display the proper value accordingly.

Robin
i have tried to go throgh what you have posted concerning millis(),its true but in relation to my problem i dont know how will it work but let me try to code from what you have explained and you will tell me where am i going wrong

codes:

#define LATCH 12
#define CLK 13
#define DATA 11

//This is the hex value of each number stored in an array by index num
int digitOne[10]= {192,249,164,176,153,146,130,248,128,24};
int digitTwo[10]= {192,249,164,176,153,146,130,248,128,24};

unsigned long interval=86400000; //24 hrs in msec
unsigned long previousMillis=0;

void setup(){

pinMode(LATCH, OUTPUT);
pinMode(CLK, OUTPUT);
pinMode(DATA, OUTPUT);

}

void loop(){

unsigned currentMillis=millis();
if (currentMillis - previousMillis > interval){
previousMillis=currentMillis;
for(int i=0; i<10; i++){
for(int j=0; j<10; j++){
digitalWrite(LATCH, LOW);
shiftOut(DATA, CLK, MSBFIRST, ~digitTwo[j]); // digitTwo
shiftOut(DATA, CLK, MSBFIRST, ~digitOne*); // digitOne*

  • digitalWrite(LATCH, HIGH);*
  • }*
  • }*
  • }*
  • }*

yes goodinvetor,
RTC in far better for sure but for the time being i wann employ millis()
thanks for your good comments

Combining my answer in Reply #5 with my answer in Reply #1:

#define LATCH 12
#define CLK 13
#define DATA 11

//This is the hex value of each number stored in an array by index num
int digits[10]= {
  192,249,164,176,153,146,130,248,128,24};

void setup(){
  pinMode(LATCH, OUTPUT);
  pinMode(CLK, OUTPUT);
  pinMode(DATA, OUTPUT);
}

void loop() {
  // Display elapsed time in days
  static int days = 0;
  static unsigned long startTime = 0;
  unsigned long currentTime = millis();
  if (startTime == 0 || currentTime-startTime > (24UL*60UL*60UL*1000UL)) {
    digitalWrite(LATCH, LOW);
    shiftOut(DATA, CLK, MSBFIRST, ~digits[days/10]); // digitTwo
    shiftOut(DATA, CLK, MSBFIRST, ~digits[days%10]); // digitOne
    digitalWrite(LATCH, HIGH);
    startTime += (24UL*60UL*60UL*1000UL);
    days++;
    if (days > 21)
      days = 0;
  }
}