Yes Archibald you are correct as the voltage values will only be used to trigger the relays for the LEDs
Would I still be able to combine the below error code?
With the idea of having an extra 2 LEDs connected to the 8 channel Optocoupler(Red and Green) to indicate if the arduino has encountered errors or to show that it is working as programmed.
This will be important as once completed the arduino will be installed out of sight.
I have been struggling to understand why you want the red and green LEDs as well as individual LEDs for each of the six inputs. Am I correct in now understanding that the six LEDs will not appear on your car’s dashboard, only the red and green LEDs?
I have also been unclear why you are using 12 volt LEDs driven from relay contacts. Why not use ordinary LEDs (each with a series resistor) driven directly from your Arduino at, say, 20mA?
You now mention an optocoupler. I am not clear what that is doing.
In the code below I am assuming that you want the input voltages on analogue inputs A0 to A5 to work six relays connected to corresponding digital outputs 0 to 5.
As before, my code below finds the maximum of the six input voltages and if it’s greater than 3.0V sets an output HIGH (now changed to digital output 6). Alternatively you could count the number or errors as suggested by Koepel or just have a Boolean variable set to false initially but set to true if an error is found in the ‘for’ loop.
Note variable “vin” is no longer an array. I have removed the print statements.
float vin; // now not an array
float R1=30000, R2=7500; // R2 connected to ground
float vpb = (R1+R2)/R2 * 5/1024; // volts per bit
vmax = 0;
for(index=0 ; index<=5 ; index++ )
vin = analogRead(index)*vpb;
digitalWrite(index,vin>3.0); // will write HIGH or LOW to digital outputs 0 to 5
if(vin>vmax) vmax = vin;
if(vmax>=3.0) digitalWrite(6, HIGH); // assuming the red and green LEDs are on output 6
I was assuming that the red and green LEDs are both connected to digital output 6, whether or not via a relay. If not via a relay, the red LED (with series resistor) would be connected between output 6 and ground while the green LED (with series resistor) would be connected between output 6 and Vcc (i.e. 5 volts).
I am also assuming you want the same voltage threshold for each input, currently 3 volts.