recover from bad sensor

Hi all,

I’m building a project with one arduino and multiple sensors connected to it.
Sometimes one of the sensors just stop working and stack the loop.
Is there any way to recover from it programmatically?

thanks.

It all depends on the type of sensor and the code you use which you both did not show us...

rotemati: Hi all,

I'm building a project with one arduino and multiple sensors connected to it. Sometimes one of the sensors just stop working and stack the loop. Is there any way to recover from it programmatically?

If the sensor provides an auto-reset on power-on, simply power-cycle the sensor: power-off and power-on after some time.

If you want to do that from software, provide a suitable circuit to cut-off power and also power-on the sensor from your hardwarecircuit by software!

If the sensor provides no auto-reset on power-on, refer to the sensors datasheet how to reset the sensor while it is powered!

If you are interrogating multiple sensors in the loop(), it is usually a good idea to a apportion a time slice to each to limit the damage if one gets stuck. This won't cover all failure scenarios but it could help. Especially if you are measuring temperature, humidity etc., you don't need thousands of readings per second.

unsigned long loopCount = 0 ; . . . . . . // the % 8 is an example of doing something every 8 times in the loop, leaving some gaps in between. void Loop() {

if (loopCount % 8 == 0 ) { // do stuff with sensor 1 } else if (loopCount % 8 == 3) { // do stuff with sensor 2 } else if (loopCount % 8 == 5 ) { // so stuff with sensor 3 }

loopCount ++ ; }

rotemati:
Sometimes one of the sensors just stop working and stack the loop.

I don’t understand this but it leads me to think the program is poorly designed.

You need to post your program.
And please use the code button </>so your code looks like thisand is easy to copy to a text editor

…R

Hi,

Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :)

If you have a device which may not always return from a read command, you could define a timer which counts, say millisecond intervals. Then when you go to read a sensor which might hang up, set a variable to 0, which is counted up by the timer. When it reaches a point where you are sure the sensor has not responded, you use a (shudder!) goto statement to jump past the sensor read, as shown below. It wouldn’t have to involve a goto, but that’s the simplest example.

volatile unsigned long clockTick;

loop0
{
  clockTick = 0;
  value = readSensor();

  skipToHere:

// recovery code for unread timer

...

}//end of loop


ISR(TIMER2_COMPA_vect) 
{
   clockTick ++;
   if (clockTick == 100) goto skipToHere;

}

I'd fear that a goto out of an ISR would corrupt the stack.

Suggested edit ...

DrDiettrich: I'd fear that a goto out of an ISR would corrupt the stack.

I would need evidence that it would not cause an instant crash.

Just use RETURN instead of GOTO in all situations where you want a function to terminate on a timeout.

...R

Who uses goto anyway? Is this 1982?

ChrisTenone: Who uses goto anyway? Is this 1982?

Those of us who were brought up on Fortran 4 may not yet have completely given up those bad habits.

FORTRAN was my first language, BASIC my second. It wasn't until FORTH that I discovered goto wasn't necessary.

(By the way, if the tools had kept up, I would still be programming in FORTH. Instead they tried turning the language into Pascal or something, and all the FORTH programmers bailed. Now I'm stuck with C++ like everybody else.)

ChrisTenone: FORTRAN was my first language, BASIC my second. It wasn't until FORTH that I discovered goto wasn't necessary.

What happened to THIRD ?

By the way, we seem to have lost the OP

...R

DrDiettrich: I'd fear that a goto out of an ISR would corrupt the stack.

a goto jump outside the current function will not(!) even compile error-free, no matter hwether it is a normal or an interrupt handling function.

Does longjmp still exist? It was the unstructured ancestor of SEH, would do what we need.