Advertisement
Krejzi_Dark

Untitled

Feb 18th, 2020
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 12.24 KB | None | 0 0
  1. #define relay1 14
  2. #define highPin1 A0
  3. #define lowPin1 A1
  4.  
  5. #define relay2 16
  6. #define highPin2 A2
  7. #define lowPin2 A3
  8.  
  9. #define relay3 15
  10. #define highPin3 A6
  11. #define lowPin3 A7
  12.  
  13. #define relay4 5
  14. #define highPin4 A8
  15. #define lowPin4 A9
  16.  
  17. #include <Adafruit_SSD1306.h>
  18. #define OLED_RESET 1
  19. Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire);
  20.  
  21. #include <Adafruit_NeoPixel.h>
  22. #ifdef __AVR__
  23.   #include <avr/power.h>
  24. #endif
  25.  
  26. #define PIN            0
  27. #define NUMPIXELS      4
  28.  
  29. Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
  30.  
  31. #define BUT1 7
  32. int menu;
  33.  
  34. int interwal = 1000;
  35.  
  36. int a = 0;
  37. int b = 0;
  38. int c = 0;
  39. int d = 0;
  40.  
  41. float napRef = 5.06;
  42. float napEnd_1_5V = 0.9;
  43. float napEnd_4_2V = 2.7;
  44. float rezystancja = 11.0;
  45.  
  46. boolean koniec1;
  47. float mAh1 = 0.0;
  48. float mAh1_3;
  49. float prad1 = 0.0;
  50. float prad1_3;
  51. float battVolt1 = 0.0;
  52. float battVolt1_1;
  53. float battVolt1_2;
  54. float battVolt1_3;
  55. float shuntVolt1 = 0.0;
  56. float battLow1 = 0.0;
  57. int rw1;
  58. String rw11;
  59.  
  60. boolean koniec2;
  61. float mAh2 = 0.0;
  62. float mAh2_3;
  63. float prad2 = 0.0;
  64. float prad2_3;
  65. float battVolt2 = 0.0;
  66. float battVolt2_1;
  67. float battVolt2_2;
  68. float battVolt2_3;
  69. float shuntVolt2 = 0.0;
  70. float battLow2 = 0.0;
  71. int rw2;
  72. String rw22;
  73.  
  74. boolean koniec3;
  75. float mAh3 = 0.0;
  76. float mAh3_3;
  77. float prad3 = 0.0;
  78. float prad3_3;
  79. float battVolt3 = 0.0;
  80. float battVolt3_1;
  81. float battVolt3_2;
  82. float battVolt3_3;
  83. float shuntVolt3 = 0.0;
  84. float battLow3 = 0.0;
  85. int rw3;
  86. String rw33;
  87.  
  88. boolean koniec4;
  89. float mAh4 = 0.0;
  90. float mAh4_3;
  91. float prad4 = 0.0;
  92. float prad4_3;
  93. float battVolt4 = 0.0;
  94. float battVolt4_1;
  95. float battVolt4_2;
  96. float battVolt4_3;
  97. float shuntVolt4 = 0.0;
  98. float battLow4 = 0.0;
  99. int rw4;
  100. String rw44;
  101.  
  102. unsigned long previousMillis1 = 0;
  103. unsigned long millisPassed1 = 0;
  104.  
  105. unsigned long previousMillis2 = 0;
  106. unsigned long millisPassed2 = 0;
  107.  
  108. unsigned long previousMillis3 = 0;
  109. unsigned long millisPassed3 = 0;
  110.  
  111. unsigned long previousMillis4 = 0;
  112. unsigned long millisPassed4 = 0;
  113.  
  114. String rw_ex = "       EXCELENT";
  115. String rw_good = "         GOOD";
  116. String rw_suf = "     SUFFICIENT";
  117. String rw_low = "         LOW";
  118. String rw_bad = "         BAD";
  119.  
  120. void przycisk()
  121. {
  122.   if (digitalRead(BUT1) == LOW)
  123.   {
  124.     delay(100);
  125.     menu ++;
  126.     if (menu > 2) menu = 1;
  127.   }
  128. }
  129.  
  130. void insertBatt()
  131. {
  132.   display.setCursor(0, 0);
  133.   display.print("Ogniwo ");
  134.   display.print(menu);
  135.   display.print(" BRAK\n\n");
  136.   display.println(" Umiesc ogniwo");
  137.   display.println(" w gniezdzie");
  138. }
  139.  
  140. void battLow()
  141. {
  142.   battLow(menu);
  143. }
  144.  
  145. void discharge()
  146. {
  147.   display.setCursor(0, 0);
  148.   display.print("Ogniwo ");
  149.   display.print(menu);
  150.   if (battLow1 == 2.7) display.println("         4.2V");
  151.   if (battLow1 == 0.9) display.println("         1.5V");
  152.   display.println("");
  153.    
  154.   display.print("U: "); display.print(battVolt1); display.print("V        "); display.print(battVolt1_1); display.println("V");
  155.   display.print("I: "); display.print(prad1); display.print("A        ");display.print(battLow1); display.println("V");
  156.   display.println("");
  157.      
  158.   display.print(mAh1); display.println("mAh");
  159.    
  160.   if (battVolt1_2 > 0)
  161.   {
  162.     if (rw1 > 0)
  163.     {
  164.       display.print("Rw: ");
  165.       display.print(rw1);
  166.       display.println("mOhm");
  167.     }
  168.     display.print(rw11);
  169.   }
  170. }
  171.  
  172. void discharge_end()
  173. {
  174.   display.setCursor(0, 0);
  175.   display.print("Ogniwo ");
  176.   display.print(menu);
  177.   if (battLow1 == 2.7) display.println("         4.2V");
  178.   if (battLow1 == 0.9) display.println("         1.5V");
  179.   display.println("         KONIEC");
  180.    
  181.   display.print("U: "); display.print(battVolt1_3); display.print("V        "); display.print(battVolt1_1); display.println("V");
  182.   display.print("I: "); display.print(prad1_3); display.print("A        ");display.print(battLow1); display.println("V");
  183.   display.println("");
  184.      
  185.   display.print(mAh1); display.println("mAh");
  186.    
  187.   if (rw1 > 0)
  188.   {
  189.     display.print("Rw: ");
  190.     display.print(rw1);
  191.     display.println("mOhm");
  192.   }
  193.   display.print(rw11);
  194. }
  195.  
  196. void setup()
  197. {
  198.   pinMode(relay1, OUTPUT);
  199.   pinMode(relay2, OUTPUT);
  200.   pinMode(relay3, OUTPUT);
  201.   pinMode(relay4, OUTPUT);
  202.   pinMode(BUT1, INPUT_PULLUP);
  203.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  204.   display.clearDisplay();
  205.   display.display();
  206.  
  207.   display.setTextSize(1);
  208.   display.setTextColor(SSD1306_WHITE);
  209.  
  210.   display.println("");
  211.   display.println("        Tester");
  212.   display.println("      pojemnosci");
  213.   display.println("        ogniw");
  214.   display.display();
  215.   delay(1000);
  216.  
  217.   pixels.begin();
  218.   menu = 2;
  219. }
  220.  
  221. void loop() {
  222.   przycisk();
  223.   menu ++;
  224.   if (menu > 2) menu = 1;
  225.  
  226.   battVolt1 = analogRead(highPin1) * napRef / 1024.0;
  227.   shuntVolt1 = analogRead(lowPin1) * napRef / 1024.0;
  228.  
  229.   battVolt2 = analogRead(highPin2) * napRef / 1024.0;
  230.   shuntVolt2 = analogRead(lowPin2) * napRef / 1024.0;
  231.  
  232.   battVolt3 = analogRead(highPin3) * napRef / 1024.0;
  233.   shuntVolt3 = analogRead(lowPin3) * napRef / 1024.0;
  234.  
  235.   battVolt4 = analogRead(highPin4) * napRef / 1024.0;
  236.   shuntVolt4 = analogRead(lowPin4) * napRef / 1024.0;
  237.  
  238.   if (battVolt1 <= 4.3 && battVolt1 > battLow1) a++;
  239.   if (battVolt1 <= battLow1 && battVolt1 > 0.2) a = 1;
  240.   if (battVolt1 <= 0.2) a = 0;
  241.  
  242.   if (battVolt2 <= 4.3 && battVolt2 > battLow2) b++;
  243.   if (battVolt2 <= battLow2 && battVolt2 > 0.2) b = 1;
  244.   if (battVolt2 <= 0.2) b = 0;
  245.  
  246.   if (battVolt3 <= 4.3 && battVolt3 > battLow3) c++;
  247.   if (battVolt3 <= battLow3 && battVolt3 > 0.2) c = 1;
  248.   if (battVolt3 <= 0.2) c = 0;
  249.  
  250.   if (battVolt4 <= 4.3 && battVolt4 > battLow4) d++;
  251.   if (battVolt4 <= battLow4 && battVolt4 > 0.2) d = 1;
  252.   if (battVolt4 <= 0.2) d = 0;
  253.  
  254.   if (a == 0)
  255.   {
  256.     pixels.setPixelColor(0, pixels.Color(0,0,250));
  257.     digitalWrite(relay1, LOW);
  258.     battVolt1 = 0;
  259.     battVolt1_1 = 0;
  260.     battVolt1_2 = 0;
  261.     battLow1 = 0;
  262.     prad1 = 0;
  263.     mAh1 = 0;
  264.     shuntVolt1 = 0;
  265.     rw1=0;
  266.     koniec1 = false;
  267.   }
  268.  
  269.   if (a == 2)
  270.   {
  271.     if(battVolt1 >= 2.7) battLow1 = napEnd_4_2V;
  272.     if(battVolt1 < 2.7) battLow1 = napEnd_1_5V;
  273.     battVolt1_1 = battVolt1;
  274.   }
  275.  
  276.   if (a == 22)
  277.   {
  278.     battVolt1_2 = battVolt1;
  279.   }
  280.  
  281.   if (b == 0)
  282.   {
  283.     pixels.setPixelColor(1, pixels.Color(0,0,250));
  284.     digitalWrite(relay2, LOW);
  285.     battVolt2 = 0;
  286.     battVolt2_1 = 0;
  287.     battVolt2_2 = 0;
  288.     battLow2 = 0;
  289.     prad2 = 0;
  290.     mAh2 = 0;
  291.     shuntVolt2 = 0;
  292.     rw2=0;
  293.     koniec2 = false;
  294.   }
  295.  
  296.   if (b == 2)
  297.   {
  298.     if(battVolt2 >= 2.7) battLow2 = napEnd_4_2V;
  299.     if(battVolt2 < 2.7) battLow2 = napEnd_1_5V;
  300.     battVolt2_1 = battVolt2;
  301.   }
  302.  
  303.   if (b == 22)
  304.   {
  305.     battVolt2_2 = battVolt2;
  306.   }
  307.  
  308.   if (c == 0)
  309.   {
  310.     pixels.setPixelColor(2, pixels.Color(0,0,250));
  311.     digitalWrite(relay3, LOW);
  312.     battVolt3 = 0;
  313.     battVolt3_1 = 0;
  314.     battVolt3_2 = 0;
  315.     battLow3 = 0;
  316.     prad3 = 0;
  317.     mAh3 = 0;
  318.     shuntVolt3 = 0;
  319.     rw3=0;
  320.     koniec3 = false;
  321.   }
  322.  
  323.   if (c == 2)
  324.   {
  325.     if(battVolt3 >= 2.7) battLow3 = napEnd_4_2V;
  326.     if(battVolt3 < 2.7) battLow3 = napEnd_1_5V;
  327.     battVolt3_1 = battVolt3;
  328.   }
  329.  
  330.   if (c == 22)
  331.   {
  332.     battVolt3_2 = battVolt3;
  333.   }
  334.  
  335.   if (d == 0)
  336.   {
  337.     pixels.setPixelColor(3, pixels.Color(0,0,250));
  338.     digitalWrite(relay4, LOW);
  339.     battVolt4 = 0;
  340.     battVolt4_1 = 0;
  341.     battVolt4_2 = 0;
  342.     battLow4 = 0;
  343.     prad4 = 0;
  344.     mAh4 = 0;
  345.     shuntVolt4 = 0;
  346.     rw4=0;
  347.     koniec4 = false;
  348.   }
  349.  
  350.   if (d == 2)
  351.   {
  352.     if(battVolt4 >= 2.7) battLow4 = napEnd_4_2V;
  353.     if(battVolt4 < 2.7) battLow4 = napEnd_1_5V;
  354.     battVolt4_1 = battVolt4;
  355.   }
  356.  
  357.   if (d == 22)
  358.   {
  359.     battVolt4_2 = battVolt4;
  360.   }
  361.  
  362.   if(battVolt1 >= 0.2)
  363.   {
  364.     millisPassed1 = millis() - previousMillis1;
  365.     prad1 = (battVolt1 - shuntVolt1) / rezystancja;
  366.     if (prad1 <= 0.00) prad1 = 0.00;
  367.     mAh1 = mAh1 + (prad1 * 1000.0) * (millisPassed1 / 3600000.0);  
  368.     rw1 = ((battVolt1_1 * rezystancja) - (battVolt1_2 * rezystancja)) / battVolt1_2 * 1000;
  369.     previousMillis1 = millis();
  370.   }
  371.  
  372.   if(battVolt1 < battLow1)
  373.   {
  374.  
  375.     koniec1 = true;
  376.     battVolt1_3 = battVolt1;
  377.     prad1_3 = prad1;
  378.     mAh1_3 = mAh1;
  379.   }
  380.  
  381.   if(battVolt2 >= 0.2)
  382.   {
  383.     millisPassed2 = millis() - previousMillis2;
  384.     prad2 = (battVolt2 - shuntVolt2) / rezystancja;
  385.     if (prad2 <= 0.00) prad2 = 0.00;
  386.     mAh2 = mAh2 + (prad2 * 1000.0) * (millisPassed2 / 3600000.0);  
  387.     rw2 = ((battVolt2_1 * rezystancja) - (battVolt2_2 * rezystancja)) / battVolt2_2 * 1000;
  388.     previousMillis2 = millis();
  389.   }
  390.  
  391.   if(battVolt2 < battLow2)
  392.   {
  393.  
  394.     koniec2 = true;
  395.     battVolt2_3 = battVolt2;
  396.     prad2_3 = prad2;
  397.     mAh2_3 = mAh2;    
  398.   }
  399.  
  400.   if(battVolt3 >= 0.2)
  401.   {
  402.     millisPassed3 = millis() - previousMillis3;
  403.     prad3 = (battVolt3 - shuntVolt3) / rezystancja;
  404.     if (prad3 <= 0.00) prad3 = 0.00;
  405.     mAh3 = mAh3 + (prad3 * 1000.0) * (millisPassed3 / 3600000.0);  
  406.     rw3 = ((battVolt3_1 * rezystancja) - (battVolt3_2 * rezystancja)) / battVolt3_2 * 1000;
  407.     previousMillis3 = millis();
  408.   }
  409.  
  410.   if(battVolt3 < battLow3)
  411.   {
  412.  
  413.     koniec3 = true;
  414.     battVolt3_3 = battVolt3;
  415.     prad3_3 = prad3;
  416.     mAh3_3 = mAh3;    
  417.   }
  418.  
  419.   if(battVolt4 >= 0.2)
  420.   {
  421.     millisPassed4 = millis() - previousMillis4;
  422.     prad4 = (battVolt4 - shuntVolt4) / rezystancja;
  423.     if (prad4 <= 0.00) prad4 = 0.00;
  424.     mAh4 = mAh4 + (prad4 * 1000.0) * (millisPassed4 / 3600000.0);  
  425.     rw4 = ((battVolt4_1 * rezystancja) - (battVolt4_2 * rezystancja)) / battVolt4_2 * 1000;
  426.     previousMillis4 = millis();
  427.   }
  428.  
  429.   if(battVolt4 < battLow4)
  430.   {
  431.    
  432.     koniec4 = true;
  433.     battVolt4_3 = battVolt4;
  434.     prad4_3 = prad4;
  435.     mAh4_3 = mAh4;    
  436.   }
  437.  
  438.   delay(interwal);
  439.   display.clearDisplay();
  440.  
  441. if (koniec1 == false && a > 0)
  442. {
  443.   pixels.setPixelColor(0, pixels.Color(0,250,0));
  444.   if (a > 3) digitalWrite(relay1, HIGH);
  445. }
  446. if (koniec1 == true)
  447. {
  448.   pixels.setPixelColor(0, pixels.Color(250,0,0));
  449.   digitalWrite(relay1, LOW);
  450. }
  451.  
  452. if (koniec2 == false && b > 0)
  453. {
  454.   pixels.setPixelColor(1, pixels.Color(0,250,0));
  455.   if (b > 3) digitalWrite(relay2, HIGH);
  456. }
  457. if (koniec2 == true)
  458. {
  459.   pixels.setPixelColor(1, pixels.Color(250,0,0));
  460.   digitalWrite(relay2, LOW);
  461. }
  462.  
  463. if (koniec3 == false && c > 0)
  464. {
  465.   pixels.setPixelColor(2, pixels.Color(0,250,0));
  466.   if (c > 3) digitalWrite(relay3, HIGH);
  467. }
  468. if (koniec3 == true)
  469. {
  470.   pixels.setPixelColor(2, pixels.Color(250,0,0));
  471.   digitalWrite(relay3, LOW);
  472. }
  473.  
  474. if (koniec4 == false && d > 0)
  475. {
  476.   pixels.setPixelColor(3, pixels.Color(0,250,0));
  477.   if (d > 3) digitalWrite(relay4, HIGH);
  478. }
  479. if (koniec4 == true)
  480. {
  481.   pixels.setPixelColor(3, pixels.Color(250,0,0));
  482.   digitalWrite(relay4, LOW);
  483. }
  484.  
  485. if (rw1 > 0 && rw1 <= 150) rw11 = rw_ex;
  486. if (rw1 > 150 && rw1 <= 250) rw11 = rw_good;
  487. if (rw1 > 250 && rw1 <= 350) rw11 = rw_suf;
  488. if (rw1 > 350 && rw1 <= 500) rw11 = rw_low;
  489. if (rw1 >= 500 ) rw11 = rw_bad;
  490.  
  491. if (rw2 > 0 && rw2 <= 150) rw22 = rw_ex;
  492. if (rw2 > 150 && rw2 <= 250) rw22 = rw_good;
  493. if (rw2 > 250 && rw2 <= 350) rw22 = rw_suf;
  494. if (rw2 > 350 && rw2 <= 500) rw22 = rw_low;
  495. if (rw2 >= 500 ) rw22 = rw_bad;
  496.  
  497. if (rw3 > 0 && rw3 <= 150) rw33 = rw_ex;
  498. if (rw3 > 150 && rw3 <= 250) rw33 = rw_good;
  499. if (rw3 > 250 && rw3 <= 350) rw33 = rw_suf;
  500. if (rw3 > 350 && rw3 <= 500) rw33 = rw_low;
  501. if (rw3 >= 500 ) rw33 = rw_bad;
  502.  
  503. if (rw4 > 0 && rw4 <= 150) rw44 = rw_ex;
  504. if (rw4 > 150 && rw4 <= 250) rw44 = rw_good;
  505. if (rw4 > 250 && rw4 <= 350) rw44 = rw_suf;
  506. if (rw4 > 350 && rw4 <= 500) rw44 = rw_low;
  507. if (rw4 >= 500 ) rw44 = rw_bad;
  508.  
  509. switch (menu)
  510. {
  511.   case 1:
  512.   if (a == 0)
  513.   {
  514.     insertBatt();
  515.   }
  516.  
  517.   if (koniec1 == false && a > 0)
  518.   {
  519.     discharge();
  520.   }
  521.  
  522.   if (koniec1 == true)
  523.   {
  524.     discharge_end();
  525.   }
  526.   break;
  527.  
  528.   case 2:
  529.   if (b == 0)
  530.   {
  531.     insertBatt();
  532.   }
  533.  
  534.   if (koniec2 == false && b > 0)
  535.   {
  536.     discharge();
  537.   }
  538.  
  539.   if (koniec2 == true)
  540.   {
  541.     discharge_end();
  542.   }
  543.   break;
  544.  
  545.   case 3:
  546.   if (c == 0)
  547.   {
  548.     insertBatt();
  549.   }
  550.  
  551.   if (koniec3 == false && c > 0)
  552.   {
  553.     discharge();
  554.   }
  555.  
  556.   if (koniec3 == true)
  557.   {
  558.     discharge_end();
  559.   }
  560.   break;
  561.  
  562.   case 4:
  563.   if (d == 0)
  564.   {
  565.     insertBatt();
  566.   }
  567.  
  568.   if (koniec4 == false && d > 0)
  569.   {
  570.     discharge();
  571.   }
  572.  
  573.   if (koniec4 == true)
  574.   {
  575.     discharge_end();    
  576.   }
  577.   break;
  578.  
  579. }
  580. display.display();
  581. pixels.show();
  582. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement