//Trailer Voltage Monitor
//For power brakes etc.
//supply Arduino through VIN/GND or DC jack from Trailer battery
//LED is green if OK, red if low and off!! if disconnected
//define led module pins here, polarity is automatically handled by presence of LEDPLUS or LEDMINUS
//if LED's have common negative, use LEDMINUS
//if LED's have common positive, use LEDPLUS
//#define LEDMINUS 3
#define LEDPLUS 7
#define LEDBLUE 4
#define LEDRED 5
#define LEDGREEN 6
//Set resistors here:
#define RVIN (10000.0)
#define RGND (4700.0)
#define VTRIGGER (11.5)
void setup() {
ledsetup(); //set up pins
}
void loop() {
int a=analogRead(A0); //read input
float v;
v=(a*5*(RVIN+RGND))/RGND/1023; //work out v at VIN based on resistors
if(v ledset(1,0,0);//red
}else{
ledset(0,1,0);//green
}
delay(200);
}
void ledsetup(){ //set up led pins depending on whether they are common + or common -, turn all LED's off
#ifdef LEDPLUS
pinMode(LEDPLUS, OUTPUT);
digitalWrite(LEDPLUS, HIGH);
pinMode(LEDRED, OUTPUT);
digitalWrite(LEDRED, HIGH);
pinMode(LEDGREEN, OUTPUT);
digitalWrite(LEDGREEN, HIGH);
pinMode(LEDBLUE, OUTPUT);
digitalWrite(LEDBLUE, HIGH);
#endif
#ifdef LEDMINUS
pinMode(LEDMINUS, OUTPUT);
digitalWrite(LEDMINUS, LOW);
pinMode(LEDRED, OUTPUT);
digitalWrite(LEDRED, LOW);
pinMode(LEDGREEN, OUTPUT);
digitalWrite(LEDGREEN, LOW);
pinMode(LEDBLUE, OUTPUT);
digitalWrite(LEDBLUE, LOW);
#endif
}
void ledset(byte r, byte g, byte b){
#ifdef LEDPLUS
r=!r; //invert if we're using common +
g=!g;
b=!b;
#endif
digitalWrite(LEDRED, r); //set outputs
digitalWrite(LEDGREEN, g);
digitalWrite(LEDBLUE, b);
}