every approximately 60 minutes the Arduino stop reading analog inputs

Hi
I implemented PID and every approximately 60 minutes the Arduino stop reading analog inputs
What can be the reason for that

Regards
Avi
:roll_eyes:

cap2252:
I implemented PID and every approximately 60 minutes the Arduino stop reading analog inputs
What can be the reason for that

Code.

Maybe error in line 42 of the code.

jurs:
Code.

Maybe error in line 42 of the code.

I see some problems on line 134 as well. That line shows some code that should never be there.

The OP should also try uncommenting lines 156~187 and comment out lines 188~211.

OTOH, is your Arduino connected to a computer? Is the computer set to sleep/hibernate/etc (generally, take some power-saving measures) after 60 minutes? What is possibly happening is that your computer is going into some power-saving mode after 60 minutes and putting the USB ports in low-power mode, therefore powering down the Arduino or not giving it enough current to run. Again, this a possibility IF you're running the Arduino from your computer.

Thanks for your reply
i am not using the computer the Arduino uno runs alone
it is running 24V motor through controller with encoder wheel to keep steady speed
it runs perfectly until 60 minutes then it do not read any inputs and stop

what Especial in line 42?

would you like to see the program

Regards
Avi

The code would PROBABLY help in this situation.

But I'm thinking the code in line 78 is calling your cat over every 60 minutes to pee on it, that's making it reset and stop reading inputs.

hi i tried to send the code but it too long
is there any other way to send it

cap2252:
is there any other way to send it

If the code is too long for sending it 'inline' in the message, send it as a "file attachment".

Go to message "Preview" of your message written and then upload the file as an attachment.

here is the code

carriege.2015.02.09.ino (9.9 KB)

cap2252:
here is the code

Your program deals with the "micros()" function.

The micros() timer rolls over after about 71 minutes, or to be accurate after 4294967295 µs.

This is about 4294 seconds or roughly 71 minutes.

Most likely, your program stops after 71 minutes because of a weird programming logic that cannot handle micros() rollover correctly.

What do you think: Your program stops after 60 minutes? Or maybe after 71 minutes?

i think 71 minutes

cap2252:
i think 71 minutes

Your time_since() function is the absolutely weird thing I've seen in time calculation for a long time.

unsigned long time_since( unsigned long since_time, int x )
{
 unsigned long t;
 
 t = curr_time();

 if( has_wrapped_around(t, since_time) ) {
#if 0
 Serial.print("wo1 ");
 Serial.print( x );
 Serial.print(" ");
 Serial.print( since_time );
 Serial.print(" > ");
 Serial.println( t );
#endif
 return (MAX_TIME - since_time) + t;
 }
 
 if( since_time > t ) {
 return 0;
 }
 else {
 return t - since_time;
 }
 
}

If you want to get a "time difference" that is safe for rolling over 0 after some time, you better always use that:

  unsigned long oldTime=micros(); // save the earlier time in an 'unsigned long'
  ...
  and when you later need the time difference you calculate like that:
  ... 
  long timeDiff= micros()-oldTime; // calculate the time difference

That way you always get the time difference up to half of the rollover period, so max. time difference to obtain that way is about 34 minutes using micros(). Whether there was a time rollover in between or not.

I just don't remember for sure, but if calculationg the difference like

  unsigned long timeDiff= micros()-oldTime; // calculate the time difference

you also will be able to obtain time differences of up to the full 71 minutes.

jurs:
That way you always get the time difference up to half of the rollover period, so max. time difference to obtain that way is about 34 minutes using micros(). Whether there was a time rollover in between or not.

Why half? You should be able to time out to 71 minutes. As long as you don't have two rollovers in the same timing period you should be fine.

Delta_G:
Why half? You should be able to time out to 71 minutes. As long as you don't have two rollovers in the same timing period you should be fine.

Just added something to my posting

If you calculate using "long" you can get positive or negative results for half of the rollover period. Depends if you substract an ealier from a later time or the other way round.

Sometimes you have two times from two actions, and you don't know which is earlier and which is later, but you know that they are close together. Then calculate the result into a 'long' and you know the difference as well as if it is positive or negative.

If you calculate using "unsigned long" you always get a positive result and it depends which time is earlier and which is later if this will be a big number or a small number as the result. If you substract the later time from the earlier time and put the result into a "unsigned long", the result will be the time difference up to the rollover time.

thanks for your reply
is there any thing else that can cause this arduino freezing beside the rollover time

cap2252:
thanks for your reply
is there any thing else that can cause this arduino freezing beside the rollover time

Yes: memory.

There are several places in your code where you’re using Serial.print().

Example: line 141: Serial.print( "time is " );

This is better coded as: Serial.print(F("time is "));

This way your strings will move to the Flash area, thus saving you lots of valuable memory. Wherever you are using Serial.print with fixed strings, enclose them in F().

Whenever possible, use global variables.

thanks

thanks :blush: