code for when error occurs do something

Hello everybody!
Thank you for reading my call for help :slight_smile:

First of all, I'm new here and am using an Arduino Uno.

I wish to write a code that does something when there is an error in my coding.

Example:
Say I have a pressure reader in my Arduino which is actively reading pressure. If the part of the code related to the pressure reading fails, for some reason, and gives me an error message is there a way I could write a code that does something like: when failure/error occurs do send voltage to pin A(x).

I have done some research but did not find what I was looking for.

Any help is much appreciated. Thank you for your time :slight_smile:

there's many things that can be done

  • recognize the error and not process the data
  • print an error msg to the serial interface (make sure in prints Error that can be searched for later)
  • update a log that could be dumped/reviewed later
  • blink an LED and continue (similar to raising a voltage on a pin as you suggested)
  • increment an error counter (different counts for each type of error)
  • stop the program to make sure the error is not overlooked, entering a loop flashing an LED
  • restart the program (cautiously)

You wrote: "If the part of the code related to the pressure reading fails,".

Are you assuming the microcontroller has failed in some way or are you asking if the sensor is returning some value that is outside the limits you are setting?

Paul

atlas_1:
I wish to write a code that does something when there is an error in my coding.

If you could detect an error in your coding then you would know what the error was and you wouldn't put it in your code in the first place.

If you want to detect an error in the sensor then that is dependent on the sensor. If there is a way to determine the sensor is giving readings that are out of bounds then you can do whatever you wish when you detect the error. In production systems the response to an error depends on the severity of it and the potential consequences the error can have on the system.

atlas_1:
If the part of the code related to the pressure reading fails, for some reason, and gives me an error message

The only way an Arduino program will produce an error message is when you write the code that detects the error and produces the message.

An Arduino has no operating system that can check that things are working properly.

...R

gcjr:
there's many things that can be done

  • recognize the error and not process the data
  • print an error msg to the serial interface (make sure in prints Error that can be searched for later)
  • update a log that could be dumped/reviewed later
  • blink an LED and continue (similar to raising a voltage on a pin as you suggested)
  • increment an error counter (different counts for each type of error)
  • stop the program to make sure the error is not overlooked, entering a loop flashing an LED
  • restart the program (cautiously)

Thank you very much for your suggestions!
I understand my question was pretty ambiguous, which was due to my last of understanding of what I was really asking.
I believe that defining the value boundaries for my sensor data and beep a noise when those boundaries are overstepped will be sufficient to simulate an error being detected.
Thanks again :slight_smile:

Paul_KD7HB:
You wrote: "If the part of the code related to the pressure reading fails,".

Are you assuming the microcontroller has failed in some way or are you asking if the sensor is returning some value that is outside the limits you are setting?

Paul

Hi Paul, thanks for your reply.
Well if the microcontroller's failed me there ain't much I can do in my particular situation.
Yes, what I was really asking for was if the sensor failed (i.e. the values it was returning were stupid). I'm sorry for my ambiguity, I just wasn't really sure how to phrase my problem.
I believe defining acceptable value boundaries of my pressure sensor data (e.g. in 0 psi at sea level = bad) and then making an audio beacon go beep if those boundaries are not respected would be the only solution to figuring out that something's gone wrong.

Thanks again for your time.

Atlas

ToddL1962:
If you could detect an error in your coding then you would know what the error was and you wouldn't put it in your code in the first place.

If you want to detect an error in the sensor then that is dependent on the sensor. If there is a way to determine the sensor is giving readings that are out of bounds then you can do whatever you wish when you detect the error. In production systems the response to an error depends on the severity of it and the potential consequences the error can have on the system.

Hi Todd,

Yes, that's true if I knew what the error was I wouldn't put it in. I phrased my question poorly and I apologize for the ambiguity. I was still confused with what I was really trying to ask.

Anyway, yes, I believe my way to go is defining bounds for my readings and then make something happen when those readings go out of bounds. Thank you for your suggestion and your time, it's truly helpful :slight_smile:

Atlas

Robin2:
The only way an Arduino program will produce an error message is when you write the code that detects the error and produces the message.

An Arduino has no operating system that can check that things are working properly.

...R

Hello Robin,
I understand that. I believe I was quite ambiguous in my question. I apologize for that.
Seems like my way to go is to write a code that detects the errors as you suggested. I'll attempt to map some boundary values and write a code that does something and those boundaries are stepped on.
Thanks for your help.
Atlas