Guest User

Untitled

a guest
Nov 24th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. // Voltage Divider for 12-to-5 Volts
  2. float R1 = 147000.0, R2 = 47000.0;
  3. float ConstADC = 5.0 // Vref
  4. float convADC = constADC / 1024.0;
  5. // vBatt ADC smoothing
  6. const int iDelay = 5000;
  7. float nReadings = 0.0, numReadings = 10.0;
  8. float tReadings = 0.0;
  9. // Misc
  10. int i, value;
  11. float vout, vin, vind, vAC;
  12. const float vTriggerOn = 12.45; // below this the AC charger will turn on
  13. const float vTriggerOff = 14.5; // above this the AC charger will turn off
  14.  
  15. void setup() {
  16. Serial.begin(9600);
  17. digitalWrite(powerPin,HIGH); pinMode(powerPin,OUTPUT); // Active-low
  18. digitalWrite(powerPin2,HIGH); pinMode(powerPin2,OUTPUT); // Active-low
  19. }
  20.  
  21. void loop() {
  22. int val=analogRead(A2); // Battery voltage input from voltage divider
  23. vout = val * convADC
  24. vin = vout / (R2/(R1+R2));
  25. tReadings = tReadings + vind; // for smoothing
  26. nReadings = nReadings + 1;
  27.  
  28. other stuff...
  29.  
  30. if (nReadings>=numReadings) {
  31. vind = tReadings / nReadings; // Smoothing
  32. vAC=0;
  33. if (vind < vTriggerOn) { // Batt low, turn on charger
  34. digitalWrite(powerPin,LOW);
  35. digitalWrite(powerPin2,LOW);
  36. vAC=13.8;
  37. }
  38. if (vind > vTriggerOff) { // Solar panels on, turn off charger
  39. digitalWrite(powerPin,HIGH);
  40. digitalWrite(powerPin2,HIGH);
  41. }
  42. if (vind>15.5) {vind=15.48; vin=15.48;} // <-- NEVER EXECUTES
  43.  
  44. if (vind>vTrigger) { // Normal - no worries
  45. Serial.print(vind); Serial.print("t");
  46. Serial.println(vAC);
  47. tReadings = 0.0;
  48. nReadings = 0.0;
  49. }
  50. else Serial.println("LOW VOLTAGE");
  51.  
  52. delay(iDelay);
  53. }
Add Comment
Please, Sign In to add comment