Delay() not running past 32 seconds

I am using this code to turn a valve off and on.

// Solar irrigation project.

int valve1Pin = 5; // Pin controlling valve 1

void setup() {
pinMode (valve1Pin, OUTPUT);

}

void loop()
{
unsigned long onInterval = 1000 * 10; // On time .
unsigned long offInterval = 1000 * 35; ;

digitalWrite(valve1Pin, HIGH);
delay(onInterval);
digitalWrite(valve1Pin, LOW);
delay(offInterval);

}
It works with off time of 32 seconds but when using 33 s teh valve never comes back on.

Every thing I read says delay() can work for up to 49 days. Certainly longer that 32 seconds.

What have I overlooked?

I have tried this code on a Uno and Mega with no change. Used IDE 1.8.19 and 2.0 no changes.

try

unsigned long offInterval = 1000 * 35ul;

Adding the "ul" to the operand make this number unsigned long.
If you do not explicitly specify the type of the operands, all calculations will be made in int type. Int has comtains values from -32768 to 32767

32767 ms = 32.7 seconds

1 Like

Or you could just remove the 1000 * 35 and just use delay(35000);
or
unsigned long offInterval = 35000;

Id also get rid of the double semicolon at the end of
unsigned long offInterval = 1000 * 35; ; <--- this one

But first it's clearer.
For example, an interval of 10 minutes, I always write like this

uint32_t ten_minutes_interval = 10 * 60 * 1000ul;
2 Likes

So that is more clearer to you than this
unsigned long thirtytwo_sec_timer = 32000;
or
delay(32000);

I guess everyone is diffrent. It dosnt realy matter as long as it works and people understand what is being done.

I changed that line to this.

unsigned long offInterval = 1000 * 12 * 3ul;

and it works but does not when I take the ul off of 3ul.

Why would it be needed on the rvalue?

1 Like

@spenterr
This will answer your question.

i explained it in detail in post #2

So where would I find a quark like this on my own? That certainly in not in standard C. I got this program from Arduino Cookbook. (2nd). Have checked in Beginning C for
Arduino, not there either. Not on the timer() on Arduino.cc either.

Can you backup that statement with reference to the C Language Standard? BTW, Arduino is programmed in C++.

Not to be uppity but I have a BS in Computer Science and a few years of experience. They did not reinvent the wheel when they made C++. C is a sub set of C++ meaning the built C++ on C. So what works for C should work in C++ but not the other way around. C does all the casting so the given computations should have been saved as unsigned long. Back to my original quest. Where would the instructions for this problem be found and what else is different?

Then you should have no problem backing up your statement with reference to the language standard.

Perhaps you aren't accustomed to programming on architectures where int variables are 16 bits.

But your experience is right out of the book. Dropping a L or UL on any constant causes the entire expression to be evaluated differently.

a7

"Saved" - yes, but not evaluated. As I said before, If you do not explicitly specify the type of the operands, all calculations will be made in int type. if the result of the calculation does not fit into the int type, you will get an overflow at the calculation stage. And the fact that you then assign this value to a variable of the long type cannot correct the situation.
The fact that you have not encountered this before is probably due to the fact that you worked on systems where the int is 4 bytes or more, so the probability of overflow is less.

1 Like

you are mistaken
See the International standard - Programming languages — C
chapter 6.4.4.1

Not to be uppity but if you have a BS in computer science and you can't understand as to why you can't store a UL in an int then my advice would be for you to go back to your institution and relearn that part you missed.

Agreed. A lot of us didn't even get/don't have a BS and we have learnt these things. A "badge" doesn't mean much if you don't have the knowledge to back up the "badge".

Knowledge and application of the knowledge is more important than having a degree.

1 Like

Fixed that for ya. :wink:

a7

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.