//Voltage reading (circuit = 12V---22k---VoltagePin---10k---GND )
#define voltagePin A1 // put this on definitions and change the pin to what you use
then this into sub-programs (where all other similar things are):
unsigned long batteryvoltage(void){
float vout = 0.0;
float vin = 0.0;
float R1 = 21550.0; // 22k
float R2 = 9095.0; // and 10k are just fine
int value = 0;
value = analogRead(voltagePin);
vout = (value * 5.0) / 1024.0;
vin = vout / (R2/(R1+R2));
return vin * 1000;
}
and add the voltage things to the menus the exact same way than everything else. The program name is "batteryvoltage" as you see.
This code is not mine, but i've used it successfully. It has 2% accuracy if you apply it unchanged, and 12V is after safety diode. Of course you can tweak it to your needs, whick is recommended.
|