Car battery monitor
i having trouble writing code car battery monitor, controls relay switch alternate supply @ set voltage reading
i have been testing 4.2v battery , voltage divider of 2 470ohm resistors after changing int r1 , r2 accordingly
but it`s outputting in serial monitor negative voltage of -4109 hoping 4.2?
i have grounds connected , voltage divider a0 through resistor +4.2v through other resistor earth. going wrong?
code: [select]
const int referencevolts = 5; // default reference on 5-volt board
const int r1 = 3900; // value maximum voltage of 18 volts
const int r2 = 1500;
// determine voltage divider resistors
const int resistorfactor = 255 / (r2/(r1 + r2));
const int batterypin = 0; // +v battery connected analog pin 0
void setup()
{
serial.begin(9600);
}
void loop()
{
int val = analogread(batterypin); // read value sensor
float volts = (val / resistorfactor) * referencevolts ; // calculate ratio
serial.print("2nd battery voltage: ");
serial.println(volts); // print value in volts
if (volts > 14.00) //if volts more 14v
{
digitalwrite (8, high); //turn on relay join batterys charging
}
else
{
digitalwrite (8, low); //if less 14v turn off relay separate batterys
delay(3000);
}} i have been testing 4.2v battery , voltage divider of 2 470ohm resistors after changing int r1 , r2 accordingly
but it`s outputting in serial monitor negative voltage of -4109 hoping 4.2?
i have grounds connected , voltage divider a0 through resistor +4.2v through other resistor earth. going wrong?
code: [select]
const int referencevolts = 5; // default reference on 5-volt board
const int r1 = 3900; // value maximum voltage of 18 volts
const int r2 = 1500;
// determine voltage divider resistors
const int resistorfactor = 255 / (r2/(r1 + r2));
i wonder if problem in int math, magical place works out whole numbers , 1500 / 5400 = 0.
Arduino Forum > Using Arduino > Programming Questions > Car battery monitor
arduino
Comments
Post a Comment