I'm trying to improve my coding and my projects. I'm far from a programmer but I do a lot of scripting in PowerShell for my job, so basic programming concepts are okay for me, although complex ones do escape my knowledge.
I'm trying to understand how should I expect disconnected/malfunctioning sensors/inputs to be like so I can handle them.
Let's say that I have a series of ds18b20 sensors connected to a GPIO and one of them stops working or gets disconnected.
I want to use that information when processing the data to show it as such.
But I'm unable to understand why or how that input is when this happens.
In PowerShell, when a variable is missing, I know I can expect it to be Null, but I think that's not the case with electronics and their inputs.
Is there any NULL equivalence in MCUs and Electronics for when a Sensor or other input fails to work as expected?
That is a hard to answer question. An Arduino board is a black box with nothing inside until the user uploads code. You can program the Arduino to recognize and inform of abnormal situations.
I don't know how a variable goes missing. What does that mean?
I don't know if the DB18b20's update periodically by some setting or you have to poll them.
But to answer your question NO there is no "NULL" response from a sensor that does not respond.
You must keep track of each sensor over time and know when it should send data and if no data is received then one can assume there is an issue. I would setup some code to recognize if a sensor has "gone away" then come back or it is gone for good. What to do with that information depends greatly on your system.
Perhaps an array counting the number of missed responses might be helpful. Assuming you pass the data on you could replace the missing reading with a grossly out of range value (i.e. -200°) That is unless your are storing vaccine.
Sorry, might be a language barrier.
When I say when a variable is missing I mean when an expected variable value is missing/when there's an unexpected value for that variable.
Let's say that I ask the user to input text, I define the variable as string and the user inputs a negative number.
PowerShell will return an error and the value for that variable will be Null.
Then the negative number will be received as a string. It is up to you to parse the incoming data and determine its validity.
PoweShell is using extra data bits behind the scenes to record that the values is null/missing. C language does not do that. PowerShell is doing this to make life easy for you at the expense of being less efficient in its use of memory and processing resources. That's fine because PowerShell runs on multi-gigahertz, multi-core, 64-bit processors with gigabytes of ram, so why not? PowerShell could never run on an processor with one 8-bit processor that runs at 16MHz and has 2K of ram. But C programs can because they are more efficient.
So the short answer is no, generally there is no equivalent of Null built into standard C. How you best deal with that depends on the situation, the sensors and libraries you are using, but there is always a way. It's not as convenient, but convenience and efficiency are often mutually exclusive.
what is an "invalid" sensor value? the processor reads bits from a bus, how would it know those bits are from a failed sensor?
if you use the DallasTemperature.h-library if a sensor is damaged or not connected or answers invalid the value given back is -127.0
It really depends on the sensor.
As testing is a common part of professional software-developping you should do intensive testing These tests should include extraordinary situations:
no sensor connected
in case of DS18B20-sensors:
sensor is missing
sensor has been replaced by a new one (which has a different worldwide-unique-ID-number)
number of sensors in code is smaller than the physical sensors
number of sensors in code is higher than the physical sensors
best regards Stefan
Thank you, Stefan. That's exactly what I needed to help me understand how to create a solution!
not connected ?? (because the response is all ones)?