Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Voltage Divider for 12-to-5 Volts
- float R1 = 147000.0, R2 = 47000.0;
- float ConstADC = 5.0 // Vref
- float convADC = constADC / 1024.0;
- // vBatt ADC smoothing
- const int iDelay = 5000;
- float nReadings = 0.0, numReadings = 10.0;
- float tReadings = 0.0;
- // Misc
- int i, value;
- float vout, vin, vind, vAC;
- const float vTriggerOn = 12.45; // below this the AC charger will turn on
- const float vTriggerOff = 14.5; // above this the AC charger will turn off
- void setup() {
- Serial.begin(9600);
- digitalWrite(powerPin,HIGH); pinMode(powerPin,OUTPUT); // Active-low
- digitalWrite(powerPin2,HIGH); pinMode(powerPin2,OUTPUT); // Active-low
- }
- void loop() {
- int val=analogRead(A2); // Battery voltage input from voltage divider
- vout = val * convADC
- vin = vout / (R2/(R1+R2));
- tReadings = tReadings + vind; // for smoothing
- nReadings = nReadings + 1;
- other stuff...
- if (nReadings>=numReadings) {
- vind = tReadings / nReadings; // Smoothing
- vAC=0;
- if (vind < vTriggerOn) { // Batt low, turn on charger
- digitalWrite(powerPin,LOW);
- digitalWrite(powerPin2,LOW);
- vAC=13.8;
- }
- if (vind > vTriggerOff) { // Solar panels on, turn off charger
- digitalWrite(powerPin,HIGH);
- digitalWrite(powerPin2,HIGH);
- }
- if (vind>15.5) {vind=15.48; vin=15.48;} // <-- NEVER EXECUTES
- if (vind>vTrigger) { // Normal - no worries
- Serial.print(vind); Serial.print("t");
- Serial.println(vAC);
- tReadings = 0.0;
- nReadings = 0.0;
- }
- else Serial.println("LOW VOLTAGE");
- delay(iDelay);
- }
Add Comment
Please, Sign In to add comment