Facebook
From BihJonCoop, 1 Year ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 137
  1.  
  2. #include <LiquidCrystal_I2C.h>
  3. #include <SPI.h>
  4. LiquidCrystal_I2C lcd(0x3F, 16, 2);
  5. #define M1 5
  6. #define M2 4
  7. #define M3 3
  8. #define D1 10
  9. #define D2 9
  10. #define D3 8
  11. #define BUZZER 7
  12. #define B1_high A0
  13. #define B2_high A1
  14. #define B3_high A2
  15. #define Button 2
  16.  
  17. int buttonState = 0;
  18. int printStart = 0;
  19. int interval = 5000;  // Measurement Interwal (ms)
  20. float battLow = 2.8;   // End of measurement - voltage level
  21. float voltRef = 4.37;  // Reference voltage (pin 5V)
  22.  
  23. int l=0;
  24. boolean finished1 = false;
  25. boolean finished2 = false;
  26. boolean finished3 = false;
  27.  
  28. float mAh1 = 0;
  29. float mAh2 = 0;
  30. float mAh3 = 0;
  31. int poj1 = 0;
  32. int poj2 = 0;
  33. int poj3 = 0;
  34. float R1 = 4;  // Resistor R1 value [Ohm]
  35. float RB1 = 4.1; // Measured B1 circuit resistance [Ohm]
  36. float R2 = 4;  // Resistor R1 value [Ohm]
  37. float RB2 = 4.1; // Measured B2 circuit resistance [Ohm]
  38. float R3 = 4;  // Resistor R1 value [Ohm]
  39. float RB3 = 4.1; // Measured B3 circuit resistance [Ohm]
  40. float current1 = 0.0;
  41. float current2 = 0.0;
  42. float current3 = 0.0;
  43. float B1start = 0.0;
  44. float B2start = 0.0;
  45. float B3start = 0.0;
  46. float B1V1 = 0.0;  // voltage before R1
  47. float B2V1 = 0.0;  // voltage before R2
  48. float B3V1 = 0.0;  // voltage before R3
  49. float Rw1 = 0;
  50. float Rw2 = 0;
  51. float Rw3 = 0;
  52. float roznica1 = 0;
  53. float roznica2 = 0;
  54. float roznica3 = 0;
  55. float suma1 = 0;
  56. float suma2 = 0;
  57. float suma3 = 0;
  58. float X1 = 2; // voltage divider 1 ratio
  59. float X2 = 2; // voltage divider 2 ratio
  60. float X3 = 2; // voltage divider 3 ratio
  61.  
  62. int pomiar = 0;
  63. int k;
  64. boolean menu1_end = false;
  65. boolean menu_end = false;
  66. int mode = 0;
  67.  
  68. unsigned long previousMillis1 = 0;
  69. unsigned long previousMillis2 = 0;
  70. unsigned long previousMillis3 = 0;
  71. unsigned long millisPassed1 = 0;
  72. unsigned long millisPassed2 = 0;
  73. unsigned long millisPassed3 = 0;
  74.  
  75. void setup() {
  76.  
  77.   lcd.init();
  78.   lcd.backlight();
  79.   pinMode(M1, OUTPUT);
  80.   pinMode(M2, OUTPUT);
  81.   pinMode(M3, OUTPUT);
  82.   pinMode(D1, OUTPUT);
  83.   pinMode(D2, OUTPUT);
  84.   pinMode(D3, OUTPUT);
  85.   pinMode(BUZZER, OUTPUT);
  86.   digitalWrite(M1, LOW);
  87.   digitalWrite(M2, LOW);
  88.   digitalWrite(M3, LOW);
  89.   digitalWrite(D1, LOW);
  90.   digitalWrite(D2, LOW);
  91.   digitalWrite(D3, LOW);
  92.   menu();  // enter the menu
  93.   lcd.print("Battery test");
  94.   lcd.setCursor(0, 1);
  95.   lcd.print("Please wait");
  96.   delay(2000);
  97.   B1start = X1 * analogRead(B1_high) * voltRef / 1024.0; // B1 voltage - no load applied
  98.   B2start = X2 * analogRead(B2_high) * voltRef / 1024.0; // B2 voltage - no load applied
  99.   B3start = X3 * analogRead(B3_high) * voltRef / 1024.0; // B3 voltage - no load applied
  100.   lcd.clear();
  101.  
  102.   finished1 = false;
  103.   finished2 = false;
  104.   finished3 = false;
  105.   digitalWrite(BUZZER, HIGH);
  106.   delay(500);
  107.   digitalWrite(BUZZER, LOW);
  108.  
  109. }
  110.  
  111. void loop() {
  112.  
  113.   if(mode == 1){
  114.     test();
  115.   }
  116.   if(mode == 2){
  117.     discharge();
  118.   }
  119. }
  120. ////////////////////////////////////////////////////////////////////////////////////////////////
  121. void discharge() {
  122.   B1V1 = X1 * analogRead(B1_high) * voltRef / 1024.0; // B1 voltage
  123.   B2V1 = X2 * analogRead(B2_high) * voltRef / 1024.0; // B2 voltage
  124.   B3V1 = X3 * analogRead(B3_high) * voltRef / 1024.0; // B3 voltage
  125.  
  126.   if ((finished1 == true) && (finished2 == true) && (finished3 == true)) {
  127.     if(l < 3){
  128.       for (k = 0; k < 3; k++) {
  129.         digitalWrite(BUZZER, HIGH);
  130.         delay(200);
  131.         digitalWrite(BUZZER, LOW);
  132.         delay(200);
  133.     }
  134.       l++;
  135.     }
  136.   }
  137.  
  138.   if (B1V1 >= battLow && finished1 == false)
  139.   {
  140.     digitalWrite(M1, HIGH);
  141.     digitalWrite(D1, HIGH);
  142.  
  143.     lcd.setCursor(0, 0);
  144.     lcd.print("1:");
  145.     lcd.print(B1V1);
  146.     lcd.print("V");
  147.   }
  148.   if ((B1V1 < battLow) || (finished1 == true)) // Disconnects load, prints the result
  149.   {
  150.     digitalWrite(M1, LOW);
  151.  
  152.     finished1 = true;
  153.  
  154.     digitalWrite(D1, LOW);
  155.     lcd.setCursor(0, 0);
  156.     lcd.print("1:END");
  157.   }
  158.  
  159.   if (B2V1 >= battLow && finished2 == false)
  160.   {
  161.     digitalWrite(M2, HIGH);
  162.     digitalWrite(D2, HIGH);
  163.  
  164.     lcd.setCursor(9, 0);
  165.     lcd.print("2:");
  166.     lcd.print(B2V1);
  167.     lcd.print("V");
  168.   }
  169.   if ((B2V1 < battLow) || (finished2 == true)) // Disconnects load, prints the result
  170.   {
  171.     digitalWrite(M2, LOW);
  172.  
  173.     finished2 = true;
  174.  
  175.     digitalWrite(D2, LOW);
  176.     lcd.setCursor(9, 0);
  177.     lcd.print("2:END");
  178.   }
  179.  
  180.   if (B3V1 >= battLow && finished3 == false)
  181.   {
  182.     digitalWrite(M3, HIGH);
  183.     digitalWrite(D3, HIGH);
  184.  
  185.     lcd.setCursor(0, 1);
  186.     lcd.print("3:");
  187.     lcd.print(B3V1);
  188.     lcd.print("V");
  189.   }
  190.   if ((B3V1 < battLow) || (finished3 == true)) // Disconnects load, prints the result
  191.   {
  192.     digitalWrite(M3, LOW);
  193.  
  194.     finished3 = true;
  195.  
  196.     digitalWrite(D3, LOW);
  197.     lcd.setCursor(0, 1);
  198.     lcd.print("3:END     ");
  199.   }
  200.   delay(interval/4);
  201.   digitalWrite(D1, LOW);
  202.   digitalWrite(D2, LOW);
  203.   digitalWrite(D3, LOW);
  204.   delay(interval/4);
  205.   lcd.clear();
  206. }
  207. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  208. void test(){
  209.   B1V1 = X1 * analogRead(B1_high) * voltRef / 1024.0; // B1 voltage
  210.   B2V1 = X2 * analogRead(B2_high) * voltRef / 1024.0; // B2 voltage
  211.   B3V1 = X3 * analogRead(B3_high) * voltRef / 1024.0; // B3 voltage
  212.  
  213.   if ((finished1 == true) && (finished2 == true) && (finished3 == true)) {
  214.     if(l < 3){
  215.       for (k = 0; k < 3; k++) {
  216.         digitalWrite(BUZZER, HIGH);
  217.         delay(200);
  218.         digitalWrite(BUZZER, LOW);
  219.         delay(200);
  220.     }
  221.       l++;
  222.     }
  223.   }
  224.  
  225.   if (B1V1 >= battLow && finished1 == false)
  226.   {
  227.     digitalWrite(M1, HIGH);
  228.     millisPassed1 = millis() - previousMillis1;
  229.     current1 = (B1V1) / R1; // Current calculation
  230.     mAh1 = mAh1 + (current1 * 1000.0) * (millisPassed1 / 3600000.0); // Capacity sum
  231.     previousMillis1 = millis();
  232.  
  233.  
  234.     if (pomiar > 0 && pomiar < 5) // 4 x voltage measurement
  235.     {
  236.       suma1 = suma1 + B1V1;
  237.     }
  238.     if (pomiar == 6)
  239.     {
  240.       suma1 = suma1 / 4;
  241.       roznica1 = B1start / suma1;
  242.       Rw1 = (roznica1 - 1) * RB1; // Internal resistance
  243.     }
  244.  
  245.     digitalWrite(D1, HIGH);
  246.     lcd.clear();
  247.     lcd.home();
  248.     lcd.print("U:");
  249.     lcd.print(B1V1);
  250.     lcd.print("V");
  251.     lcd.setCursor(9, 0);
  252.     lcd.print("I:");
  253.     lcd.print(current1);
  254.     lcd.print("A");
  255.     lcd.setCursor(0, 1);
  256.     poj1 = mAh1;
  257.     lcd.print(poj1);
  258.     lcd.print("mAh");
  259.     if (pomiar > 6)
  260.     {
  261.       lcd.setCursor(9, 1);
  262.       lcd.print("Rw:");
  263.       lcd.print(Rw1);
  264.     }
  265.     pomiar++;
  266.     delay(interval);
  267.     digitalWrite(D1, LOW);
  268.   }
  269.   if ((B1V1 < battLow) || (finished1 == true)) // Disconnects load, prints the result
  270.   {
  271.     digitalWrite(M1, LOW);
  272.  
  273.     finished1 = true;
  274.  
  275.     digitalWrite(D1, HIGH);
  276.     lcd.clear();
  277.     lcd.home();
  278.     lcd.print("1 - Koniec");
  279.     lcd.setCursor(0, 1);
  280.     poj1 = mAh1;
  281.     lcd.print(poj1);
  282.     lcd.print("mAh ");
  283.     lcd.setCursor(8, 1);
  284.     lcd.print("Rw1:");
  285.     lcd.print(Rw1);
  286.  
  287.     delay(interval * 2);
  288.     digitalWrite(D1, LOW);
  289.   }
  290.  
  291.   if (B2V1 >= battLow && finished2 == false)
  292.   {
  293.     digitalWrite(M2, HIGH);
  294.     millisPassed2 = millis() - previousMillis2;
  295.     current2 = (B2V1) / R2; // Current calculation
  296.     mAh2 = mAh2 + (current2 * 1000.0) * (millisPassed2 / 3600000.0); // Capacity sum
  297.     previousMillis2 = millis();
  298.  
  299.  
  300.     if (pomiar > 0 && pomiar < 5) // 4 x voltage measurement
  301.     {
  302.       suma2 = suma2 + B2V1;
  303.     }
  304.     if (pomiar == 6)
  305.     {
  306.       suma2 = suma2 / 4;
  307.       roznica2 = B2start / suma2;
  308.       Rw2 = (roznica2 - 1) * RB2; // Internal resistance
  309.     }
  310.  
  311.     digitalWrite(D2, HIGH);
  312.     lcd.clear();
  313.     lcd.home();
  314.     lcd.print("U:");
  315.     lcd.print(B2V1);
  316.     lcd.print("V");
  317.     lcd.setCursor(9, 0);
  318.     lcd.print("I:");
  319.     lcd.print(current2);
  320.     lcd.print("A");
  321.     lcd.setCursor(0, 1);
  322.     poj2 = mAh2;
  323.     lcd.print(poj2);
  324.     lcd.print("mAh");
  325.     if (pomiar > 6)
  326.     {
  327.       lcd.setCursor(9, 1);
  328.       lcd.print("Rw:");
  329.       lcd.print(Rw2);
  330.     }
  331.     delay(interval);
  332.     digitalWrite(D2, LOW);
  333.   }
  334.   if ((B2V1 < battLow) || (finished2 == true)) // Disconnects load, prints the result
  335.   {
  336.     digitalWrite(M2, LOW);
  337.  
  338.     finished2 = true;
  339.  
  340.     digitalWrite(D2, HIGH);
  341.     lcd.clear();
  342.     lcd.home();
  343.     lcd.print("2 - Koniec");
  344.     lcd.setCursor(0, 1);
  345.     poj2 = mAh2;
  346.     lcd.print(poj2);
  347.     lcd.print("mAh ");
  348.     lcd.setCursor(8, 1);
  349.     lcd.print("Rw2:");
  350.     lcd.print(Rw2);
  351.  
  352.     delay(interval * 2);
  353.     digitalWrite(D2, LOW);
  354.   }
  355.  
  356.   if (B3V1 >= battLow && finished3 == false)
  357.   {
  358.     digitalWrite(M3, HIGH);
  359.     millisPassed3 = millis() - previousMillis3;
  360.     current3 = (B3V1) / R3; // Current calculation
  361.     mAh3 = mAh3 + (current3 * 1000.0) * (millisPassed3 / 3600000.0); // Capacity sum
  362.     previousMillis3 = millis();
  363.  
  364.  
  365.     if (pomiar > 0 && pomiar < 5) // 4 x voltage measurement
  366.     {
  367.       suma3 = suma3 + B3V1;
  368.     }
  369.     if (pomiar == 6)
  370.     {
  371.       suma3 = suma3 / 4;
  372.       roznica3 = B3start / suma3;
  373.       Rw3 = (roznica3 - 1) * RB3; // Internal resistance
  374.     }
  375.  
  376.     digitalWrite(D3, HIGH);
  377.     lcd.clear();
  378.     lcd.home();
  379.     lcd.print("U:");
  380.     lcd.print(B3V1);
  381.     lcd.print("V");
  382.     lcd.setCursor(9, 0);
  383.     lcd.print("I:");
  384.     lcd.print(current3);
  385.     lcd.print("A");
  386.     lcd.setCursor(0, 1);
  387.     poj3 = mAh3;
  388.     lcd.print(poj3);
  389.     lcd.print("mAh");
  390.     if (pomiar > 6)
  391.     {
  392.       lcd.setCursor(9, 1);
  393.       lcd.print("Rw:");
  394.       lcd.print(Rw3);
  395.     }
  396.     delay(interval);
  397.     digitalWrite(D3, LOW);
  398.   }
  399.   if ((B3V1 < battLow) || (finished3 == true)) // Disconnects load, prints the result
  400.   {
  401.     digitalWrite(M3, LOW);
  402.  
  403.     finished3 = true;
  404.  
  405.     digitalWrite(D3, HIGH);
  406.     lcd.clear();
  407.     lcd.home();
  408.     lcd.print("3 - Koniec");
  409.     lcd.setCursor(0, 1);
  410.     poj3 = mAh3;
  411.     lcd.print(poj3);
  412.     lcd.print("mAh ");
  413.     lcd.setCursor(8, 1);
  414.     lcd.print("Rw3:");
  415.     lcd.print(Rw3);
  416.  
  417.     delay(interval * 2);
  418.     digitalWrite(D3, LOW);
  419.   }
  420. }
  421. ////////////////////////////////////////////////////////////////////////////////////////////
  422.  
  423. void menu(){
  424. lcd.clear();
  425. lcd.setCursor(0,0);
  426. lcd.print("SELECT MODE");
  427. lcd.setCursor(0,1);
  428. lcd.print("<--CLICK");
  429. while(menu1_end==false){
  430.    while(digitalRead(Button) == LOW){}
  431.       delay(800);
  432.       if(digitalRead(Button) == HIGH){
  433.         menu1_end = true;
  434.         lcd.setCursor(0,0);
  435.         if(mode == 1){lcd.print(">Mode: CAP TEST");}
  436.         if(mode == 2){lcd.print(">Mode: DISCHARGE");}
  437.       }
  438.       else{
  439.          if(mode==2){
  440.           mode = 1;
  441.         }
  442.         else{
  443.           mode++;
  444.         }      
  445.         lcd.clear();
  446.         lcd.setCursor(0,0);
  447.         if(mode == 1){lcd.print("Mode: CAP TEST");}
  448.         if(mode == 2){lcd.print("Mode: DISCHARGE");}      
  449.       }
  450.     digitalWrite(D1, HIGH);
  451.     delay(100);
  452.     digitalWrite(D1, LOW);
  453. }
  454. while(menu_end==false){
  455.    while(digitalRead(Button) == LOW){}
  456.       delay(800);
  457.       if(digitalRead(Button) == HIGH){
  458.         menu_end = true;
  459.         lcd.setCursor(0,1);
  460.         lcd.print(">Low V: "); lcd.print(battLow); lcd.print("V");
  461.       }
  462.       else{
  463.          if(battLow > 3.8){
  464.           battLow = 2.9;
  465.         }
  466.         else{
  467.           battLow = battLow+0.1;
  468.         }      
  469.         lcd.setCursor(0,1);
  470.         lcd.print("Low V: "); lcd.print(battLow); lcd.print("V");    
  471.       }
  472.     digitalWrite(D2, HIGH);
  473.     delay(100);
  474.     digitalWrite(D2, LOW);
  475.     }
  476. delay(2000);
  477. lcd.clear();
  478. }