Having trouble with elapsedMillis

First of all I am pretty new to coding in general and probably am just misunderstanding something simple but I cannot seem to figure it out.

I am trying to use an arduino to automate a dishwasher to keep it going for many cycles. I have a series of actions that happen to open/close the door of the dishwasher and then push the start button using a servo motor. The issue I am having is with the timing. When I am testing the code with a short cycle time (i.e. 30 seconds to a few minutes) the code seems to work but when I try to run it for an hour is doesn't end up restarting the dishwasher. I am using elapsed millis to track the run time and then a while loop to keep it looping for the duration of the cycle and then once the elapsed time is greater than the cycle time it runs back through the button pushing portion of the code and resets the time. At first i thought the arduino might be going to sleep but that wasn't happening and then I realized I was initializing the cycle time as an integer and not a long but even after those things it still won't work.

If anyone has any insight that would be awesome.

#include <elapsedMillis.h>
elapsedMillis time;
unsigned long cycletime = (1000*60*62);
int cyclenumber = 1;

#include<Servo.h>;
Servo servoone; 
int DoorSwitch = 9; //switches door to "open" door between cycles

//unsigned long time; 
void setup() {
 Serial.begin(9600); 
 pinMode(DoorSwitch, OUTPUT);
 
}

void loop() {

time = 0;
Serial.print(" ");
Serial.print("  Cycle Number: ");
Serial.print(cyclenumber);

// Opens and Closes door
digitalWrite(DoorSwitch, LOW);
delay (3000);
digitalWrite(DoorSwitch, HIGH); 
Serial.print("   the door has been closed   ");
delay(3000);

//Button Pusher to start the dishwasher

Serial.print("   button pressed   ");

servoone.write(60);
servoone.attach(10);
   delay(100);
 servoone.write(60);
 delay(100);
 servoone.write(130);
 delay(1000);
 servoone.write(60);
servoone.detach();
delay(1000);
servoone.write(60);
servoone.attach(10);
   delay(100);
 servoone.write(60);
 delay(100);
 servoone.write(130);
 delay(1000);
 servoone.write(60);
servoone.detach();
 delay(1000);
servoone.write(60);
servoone.attach(10);
   delay(100);
 servoone.write(60);
 delay(100);
 servoone.write(130);
 delay(1000);
 servoone.write(60);
servoone.detach();
servoone.write(60);
 delay(100);
 servoone.write(130);
 delay(1000);
 servoone.write(60);
servoone.detach();

Serial.print("   while loop start  ");

while (time < cycletime) {
 Serial.print("looping");
 Serial.print(time);
 delay(5000);

}
Serial.print("   while loop done  ");
Serial.print("  Cycle Time: ");
Serial.print(cycletime/1000);
digitalWrite(DoorSwitch, LOW);

cyclenumber++; 
}

elapsedMillis.h (4.2 KB)

Please edit your post to add code tags, and a link to where you downloaded the library.

Best to review the "How to use this forum" post.

iluvchocmilk:
First of all I am pretty new to coding in general and probably am just misunderstanding something simple but I cannot seem to figure it out.

I am trying to use an arduino to automate a dishwasher to keep it going for many cycles. I have a series of actions that happen to open/close the door of the dishwasher and then push the start button using a servo motor. The issue I am having is with the timing. When I am testing the code with a short cycle time (i.e. 30 seconds to a few minutes) the code seems to work but when I try to run it for an hour is doesn't end up restarting the dishwasher. I am using elapsed millis to track the run time and then a while loop to keep it looping for the duration of the cycle and then once the elapsed time is greater than the cycle time it runs back through the button pushing portion of the code and resets the time. At first i thought the arduino might be going to sleep but that wasn't happening and then I realized I was initializing the cycle time as an integer and not a long but even after those things it still won't work.

If anyone has any insight that would be awesome.

#include <elapsedMillis.h>

elapsedMillis time;
unsigned long cycletime = (10006062);
int cyclenumber = 1;

#include<Servo.h>;
Servo servoone;
int DoorSwitch = 9; //switches door to "open" door between cycles

//unsigned long time;
void setup() {
Serial.begin(9600);
pinMode(DoorSwitch, OUTPUT);

}

void loop() {

time = 0;
Serial.print(" ");
Serial.print("  Cycle Number: ");
Serial.print(cyclenumber);

// Opens and Closes door
digitalWrite(DoorSwitch, LOW);
delay (3000);
digitalWrite(DoorSwitch, HIGH);
Serial.print("   the door has been closed   ");
delay(3000);

//Button Pusher to start the dishwasher

Serial.print("   button pressed   ");

servoone.write(60);
servoone.attach(10);
  delay(100);
servoone.write(60);
delay(100);
servoone.write(130);
delay(1000);
servoone.write(60);
servoone.detach();
delay(1000);
servoone.write(60);
servoone.attach(10);
  delay(100);
servoone.write(60);
delay(100);
servoone.write(130);
delay(1000);
servoone.write(60);
servoone.detach();
delay(1000);
servoone.write(60);
servoone.attach(10);
  delay(100);
servoone.write(60);
delay(100);
servoone.write(130);
delay(1000);
servoone.write(60);
servoone.detach();
servoone.write(60);
delay(100);
servoone.write(130);
delay(1000);
servoone.write(60);
servoone.detach();

Serial.print("   while loop start  ");

while (time < cycletime) {
Serial.print("looping");
Serial.print(time);
delay(5000);

}
Serial.print("   while loop done  ");
Serial.print("  Cycle Time: ");
Serial.print(cycletime/1000);
digitalWrite(DoorSwitch, LOW);

cyclenumber++;
}

My thoughts are to shift away from using any delay() and use something like this SecondCounter example code:

#define OneSecond 1000 // 1000 miliseconds
int SecondCounter

void setup() {
  // put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly:
  static unsigned long ExactTimer;
  if ( millis() - ExactTimer >= (OneSecond)) {
    ExactTimer += (OneSecond); // Allows for every second to be accounted for even if there is other delays that could cause skips
    SecondCounter++; // Counts the seconds
    if(digitalRead(DoorOpenSensorPin)){
      SecondCounter--; ;//Pause Stop The Count
    }
    if(digitalRead(RestartInputPin)){
      SecondCounter = 0;
    }
    if(SecondCounter == 10){
      // Start this Process at 10 seconds
    }
    if(SecondCounter == 60){
      // at one Minute into the process do this
    }
    if(SecondCounter == (60*5)){
      // at 5 Minutes into the process do this
    }
  }
}

This code triggers a counter every second that then runs through your sequence triggering each action at the exact time the event is to occur.
Google Arduino Blink Without Delay for tones of example code.
Hope this helps
Z

unsigned long cycletime = (1000*60*62);The above is not the number you were hoping for.

Make it:

unsigned long cycletime = (1000UL*60*62);

Thank you for the responses - I really appreciate it.

I added the UL to the 1000 and it worked as desired. Of course I began running into other issues but got those resolved as well.

My understanding was that using unsigned long instead of int should have fixed the issue but obviously didn't. So do you understand what adding UL to the number does? I read about it online and it didn't really make sense to me.

Thanks again,
Adam

The default data type for integer math expressions in Arduino is 16 bit int.

Adding the "UL" forces the calculation to be done as unsigned long.

My understanding was that using unsigned long instead of int should have fixed the issue but obviously didn't. So do you understand what adding UL to the number does?

UL qualifies a simple numeric literal as an unsigned long, instead of the default "int"