Hi,
I ran into trouble on timer issue. My project includes a 4 pin Rotary Switch to select 4 different time inputs, 30, 60, 90 seconds, and an hour. However, I ran into trouble having the timer to stop the motion at 30 seconds through out all the pins. I know that arduino can run for 50 days till it overflow, but when I compile the program, it gave me overflow warning for anything bigger than 30 seconds. Please let me know if there is anyway to fix this issue. I use Arduino Uno.
SBSv5.9_Question.ino (1.1 KB)
All time variables should be unsigned long
long startTime;
long stopTime;
int k;
as that is what millis() returns.
These need to fixed
if (digitalRead(DOOR) == HIGH) {
if (HIGH == digitalRead(TIME1_PIN)) {
k= RUNTIME1;}
else if (HIGH == digitalRead(TIME2_PIN)) {
k= RUNTIME2;}
else if (HIGH == digitalRead(TIME3_PIN))
HIGH == is not how a comparison starts. The first if() is correct syntax.
I know that arduino can run for 50 days till it overflow,
It can run for ever without the millis() rollover to zero being a problem as long as it is programmed correctly.
when I compile the program, it gave me overflow warning for anything bigger than 30 seconds
Sorry, but I have no idea what you mean by this.
I get no errors reported when I compile the program, but that does not mean that the program will work as you want.
HIGH == is not how a comparison starts. The first if() is correct syntax.
There is nothing wrong with doing the comparison with HIGH == first. In fact it is a way of avoiding the use of a single equals sign instead of 2 as intended because the compiler will report an error if only 1 is used, but it always looks odd to me.
when I compile the program, it gave me overflow warning for anything bigger than 30 seconds
Sorry I didn't explain it well. It gave me this warning:
C:\Documents and Settings\BAX\Desktop\SBS_5.9.2\SBS_5.9.2.ino:62:8: warning: overflow in implicit constant conversion [-Woverflow]
k= RUNTIME3;}
^
So the problem is, when I turn on the punching machine, it stop the motion at 30 seconds no matter if I put 40, 60, 100000 seconds at any RUNTIME_PIN
SBS_5.9.2.ino (2.94 KB)
All time variables should be unsigned long as that is what millis() returns.
long startTime;
long stopTime;
int k; << int can't hold 32 bits!
#define RUNTIME1 30000 << fits in int
#define RUNTIME2 60000 << fits in int - limit is 65535, allows 2^16-1 max
#define RUNTIME3 90000 << does not fit in int, use 90000UL, allows 2^32-1 max
#define RUNTIME4 3600000 //forever << does not fit in int, use 3600000UL, allows 2^32-1 max