Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.90 KB | None | 0 0
  1. /* Be sure you have a voltage divider 4:1 to measure the battery voltages. Battery--[4k]----[1k]--GND
  2.  *                                                                                        |
  3.  *                                                                                    [Arduino]
  4.  
  5. Sens:
  6. Battery 1 is (thru voltage divider) on Pin A0
  7. Battery 2 is (thru voltage divider) on Pin A1
  8.  
  9. Relais for Batteries:
  10. On Digital 2: Relais to connect Battery 1 to load
  11. On Digital 3: Relais to connect Battery 1 to charger
  12. On Digital 4: Relais to connect Battery 2 to load
  13. On Digital 5: Relais to connect Battery 3 to charger
  14. */
  15.  
  16.  
  17.  
  18. /* Constants and Inits */
  19. unsigned int rereads = 0;           // we want to make a multiple-read on the battery to be sure it is not only a spike...
  20. float voltagesum = 0;               // The sum of 100x reading of battery in float
  21. int b1l = 2;                        // Definitions for pins
  22. int b1c = 3;                        // Definitions for pins
  23. int b2l = 4;                        // Definitions for pins
  24. int b2c = 5;                        // Definitions for pins
  25.  
  26.  
  27. /* Setup */
  28. float batteryempty = 11.8;          // Empty battery according to https://www.tobesure.com/2018/11/06/what-is-the-voltage-of-a-car-battery-harrisburg-pa/
  29.  
  30. void setup() {
  31.   // put your setup code here, to run once:
  32.   pinMode(A0, INPUT);     // sets the analog A0 to input --> Sens for battery 1
  33.   pinMode(A1, INPUT);     // sets the analog A1 to input --> Sens for battery 2
  34.   pinMode(b1l, OUTPUT);     // sets the digital 2 to output --> Control of relais for battery 1, Dischargepath
  35.   pinMode(b1c, OUTPUT);     // sets the digital 3 to output --> Control of relais for battery 2, Dischargepath
  36.   pinMode(b2l, OUTPUT);     // sets the digital 4 to output --> Control of relais for battery 1, Chargepath
  37.   pinMode(b2c, OUTPUT);     // sets the digital 5 to output --> Control of relais for battery 2, Chargepath
  38.  
  39.   digitalWrite(b2l, LOW);   // Battery 1 OFF for charge
  40.   digitalWrite(b1l, HIGH);  // Battery 1 ON for discharge
  41.   digitalWrite(b2c, HIGH);  // Battery 2 ON for charge
  42.   digitalWrite(b1c, LOW);   // Battery 2 OFF for discharge
  43. }
  44.  
  45.  
  46.  
  47. void loop() {
  48.   float bat1voltage = 5 * analogRead(A0);
  49.   if (bat1voltage <= batteryempty) {  // Battery 1 triggered a low indicator!
  50.     for (int i = 0; i <= 100; i++) { // Measure over 2sec every < 20ms
  51.       voltagesum = voltagesum + analogRead(A0);
  52.       delay(17); // Allow 1ms for analog read... have it a bit faster than 1per20ms to avoid powerline-frequencies
  53.     }
  54.     bat1voltage = 1.0 * voltagesum / 100; // * 1.0 to force float
  55.     voltagesum = 0;
  56.     // Now we have an average of the battery-voltage over around 2secs.
  57.     if (bat1voltage <= batteryempty) {
  58.       digitalWrite(b1c, HIGH);  // Battery 1 ON for charge
  59.       digitalWrite(b1l, LOW);   // Battery 1 OFF for load/discharge
  60.       digitalWrite(b2c, LOW);   // Battery 2 OFF for charge
  61.       digitalWrite(b2l, HIGH);  // Battery 2 ON for load/discharge
  62.       continue;               // Skips rest of mainloop and starts a new iteration
  63.     }    
  64.   }
  65.  
  66.   float bat2voltage = 5 * analogRead(A1);
  67.   if (bat2voltage <= batteryempty) {  // Battery 2 triggered a low indicator!
  68.     for (int i = 0; i <= 100; i++) {  // Measure over 2sec every < 20ms
  69.       voltagesum = voltagesum + analogRead(A0);
  70.       delay(17); // Allow 1ms for analog read... have it a bit faster than 1per20ms to avoid powerline-frequencies
  71.     }
  72.     bat2voltage = 1.0 * voltagesum / 100; // * 1.0 to force float
  73.     voltagesum = 0;
  74.     // Now we have an average of the battery-voltage over around 2secs.
  75.     if (bat2voltage <= batteryempty) {
  76.       digitalWrite(b1c, LOW);   // Battery 1 OFF for charge
  77.       digitalWrite(b1l, HIGH);  // Battery 1 ON for load/discharge
  78.       digitalWrite(b2c, HIGH);  // Battery 2 ON for charge
  79.       digitalWrite(b2l, LOW);   // Battery 2 OFF for load/discharge
  80.     }    
  81.   }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement