Hello folks.
Fairly new to Arduino. Previous projects have only dealt with outputs.
What I'm trying to accomplish is reading an array of 10 digital inputs that are normally LOW and return the first one that reads HIGH.
For background this is for a machine that currently we plug in a board with LEDs numbered 1 through 10. When a fault condition occurs it lights up a number of LEDs. They will always be 2 through 10 or 6 through 10 etc. We then have to look at a chart and reference the lowest numbered lit LED to the location of the fault.
My project will print on an LCD the location, hence the need to know the first HIGH input.
I have the array set up as follows but that is as far as I have got so far
int readPins[] = {
2, 3, 4, 5, 6, 7, 8, 9, 10, 11
};
int readPinCount = 10;
void setup() {
// put your setup code here, to run once:
for (int thisPin = 0; thisPin < readPinCount; thisPin++){
pinMode(readPins[thisPin], INPUT);
}
dougp:
This is unclear, to me. After the first one reads high can others then also go high? The first one on the right? Left?
Example: if the 3rd input is high all the rest past it will be HIGH.
I only need to know what the array position of the first one read HIGH is.
Think of it like all the "sensors"on the machine are daisy chained together in one circuit. When a fault occurs somewhere along the chain it will show HIGH and every sensor past it in the chain will also show HIGH. The ones prior to it will be LOW.
I need to know the first one that is HIGH so that I can find the location in the machine where the fault is.
pinMode(readPins[thisPin], INPUT); // may have to use INPUT_PULLUP
Confused what you really want, maybe this?
for (byte thisPin = 0; thisPin < readPinCount; thisPin++)
{
if(digitalRead(readPins[thisPin]) == HIGH)
{
// thisPin is the index of the variable that is HIGH
//do some input was HIGH stuff
}
}
That may work. I mainly wanted to make sure that after it returned The index position of the first one read high that the "for" statement would not then continue to iterate over the array and return the next one that read high also.
for (byte thisPin = 0; thisPin < readPinCount; thisPin++)
{
if(digitalRead(readPins[thisPin]) == HIGH)
{
// thisPin is the index of the variable that is HIGH
// do some input was HIGH stuff
// call a function() that handles the situation
break; // leave the for loop on the first HIGH
}
}
Add break; to get out of the 'for' loop on the first HIGH detection.
bill110:
That may work. I mainly wanted to make sure that after it returned The index position of the first one read high that the "for" statement would not then continue to iterate over the array and return the next one that read high also.
Also, to head off the unlikely possibility of finding sensor nine high when sensor four started it all, you might consider counting at least two consecutive identical readings before declaring the fault - think of it like debouncing.
larryd:
for (byte thisPin = 0; thisPin < readPinCount; thisPin++)
{
if(digitalRead(readPins[thisPin]) == HIGH)
{
// thisPin is the index of the variable that is HIGH
// do some input was HIGH stuff
// call a function() that handles the situation
break; // leave the for loop on the first HIGH
}
}
Add break; to get out of the 'for' loop on the first HIGH detection.
got it. thanks
dougp:
Also, to head off the unlikely possibility of finding sensor nine high when sensor four started it all, you might consider counting at least two consecutive identical readings before declaring the fault - think of it like debouncing.
And I'm assuming I would do this with variables like thisReading = and lastReading= then compare that they are actually equal before breaking
To be more specific the machine has 25 interlocks, each attached to a board that determines if the interlock is open or closed. These boards monitor up to 5 interlocks.
If an interlock is activated that board sends the 5vdc to the rs232 that I plug into. It’s a test point.
It also drops voltage to the entire safety chain to turn the machine off.
There is more involved but that’s how I can read which is activated.
The system is supposed to display on the operator console exactly where the problem is but many times it only returns “Safety chain error”.
bill110:
got it. thanksAnd I'm assuming I would do this with variables like thisReading = and lastReading= then compare that they are actually equal before breaking
Not quite. The break is only to exit the for loop to avoid checking downstream sensors. Do a complete scan, break included, and end up with whatever the value of 'thisReading' is going to be. Compare that to previousReading then thisReading becomes previousReading as you mention. The result of the compare of thisReading and previousReading tells whether a fault is registered.
IMHO, it wouldn't hurt to put a small delay, ~50ms, between scans either.