Here is the working program. Load it into a sketch and rename it before saving. If it does not compile too much or too little was copied. The program checks the battery every 5.5 seconds. Shorting pin 12 to ground speeds up the read to calibrate voltage divider every .5 seconds. Try to use a resistor to the the battery 47K or higher to prevent possible possible to the micro from over voltage. Use a 2K to 5K pot to adjust voltage. This is a convenient site to calculate voltage dividers.
http://www.raltron.com/cust/tools/voltage_divider.asp For Vin 28V input, R1 = 47k add 2.5K for a 5K pot, so 49.5K for the calculator value. V out is 3.3V, chosen because it is also available on board for testing. Using the calculator R2 is 4.71K. Subtracting 2.5K for the pot gives 2.2K for the lower resistor.
V out is 3.3V, chosen because it is also available on board for testing. Using the calculator R2 is 4.71K. Subtracting 2.5K for the pot gives 2.2K for the lower resistor. Just measure a known voltage and adjust the pot till the screen in tools measures that voltage.
The programming is very basic, but using this as a boiler plate other functions can be added. Clicking on TOOLS, select the correct board. Selecting serial monitor will allow you to see the data coming out of the board.
// SIMP_DIVERT_24 for Heater
// relay out PIN #10
// This is a simple SSR driver that turns on at
// at a fixed voltage and off at a lower
// Samples four times and adds those values together.
// This number is in tens of mv.
// Data is sent out serially to allow calibration
// every half second due to delay.
// Typical operating voltage gives an A/D count of about 750
// Divider values to create 3.3V
// Normal delay is 5 seconds. grounding pin 12 makes that faster.
// LED lights once per loop and when relay on.
// created 02/14/17
int battery = 0; // calculated voltage in mv
int SSR = 0; // relay output
void setup()
{
Serial.begin(9600); // setup serial port speed for data in TOOLS
pinMode(13, OUTPUT); // sets the digital LED pin 13 as output, onboard LED
pinMode(10, OUTPUT); // SSR relay out
pinMode(12, INPUT); // ground for 1 second loop
digitalWrite(12,1); // enable internal pullup (early version compatable)
}
void loop()
{ // this is the start of the program
// turn LED pin 13 ON for once per loop blink
digitalWrite(13,1); // blink while reading data
// A resistive voltage divider produces about 3V3 from battery
// READ ANALOG VALUE FOUR TIMES AND ADD TOGETHER
// A/D values go from 0 to 1023
battery = analogRead(2); // get first sample
delay(20); // delay for 20ms
battery = battery + analogRead(2); // get second sample
delay(20); // delay for 20ms
battery = battery + analogRead(2); // get third sample
delay(20); // delay for 20ms
battery = battery + analogRead(2); // get fourth sample
// turn LED pin 13 OFF
digitalWrite(13,0); // turn off blink after 60ms
// determine battery state
if (battery > 2900) // is voltage high enough?
{
SSR = 1; // turn relay ON
digitalWrite(13,1); // turn LED ON
}
if (battery < 2800) // has it dropped too low?
{
SSR = 0; // turn relay OFF
digitalWrite(13,0); // turn LED OFF
}
// output state to pin 10
digitalWrite(10,SSR);
//print out battery data in TOOLS
Serial.print((float)battery / 100); // battery voltage 2 decimal places
Serial.print("V battery ");
Serial.print(SSR); // relay state
Serial.println(" relay "); // go to next line
delay (500); // delay for printing every half second
// ROUTINE FOR DETERMINING LOOP TIME
// look at pin 12, is is shorted to gnd?
if (digitalRead(12) == 1) delay (5000); // if not delay another 5 seconds
} // end of program