Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define relay1 14
- #define highPin1 A0
- #define lowPin1 A1
- #define relay2 16
- #define highPin2 A2
- #define lowPin2 A3
- #define relay3 15
- #define highPin3 A6
- #define lowPin3 A7
- #define relay4 5
- #define highPin4 A8
- #define lowPin4 A9
- #include <Adafruit_SSD1306.h>
- #define OLED_RESET 1
- Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire);
- #include <Adafruit_NeoPixel.h>
- #ifdef __AVR__
- #include <avr/power.h>
- #endif
- #define PIN 0
- #define NUMPIXELS 4
- Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
- #define BUT1 7
- int menu;
- int interwal = 1000;
- int a = 0;
- int b = 0;
- int c = 0;
- int d = 0;
- float napRef = 5.06;
- float napEnd_1_5V = 0.9;
- float napEnd_4_2V = 2.7;
- float rezystancja = 11.0;
- boolean koniec1;
- float mAh1 = 0.0;
- float mAh1_3;
- float prad1 = 0.0;
- float prad1_3;
- float battVolt1 = 0.0;
- float battVolt1_1;
- float battVolt1_2;
- float battVolt1_3;
- float shuntVolt1 = 0.0;
- float battLow1 = 0.0;
- int rw1;
- String rw11;
- boolean koniec2;
- float mAh2 = 0.0;
- float mAh2_3;
- float prad2 = 0.0;
- float prad2_3;
- float battVolt2 = 0.0;
- float battVolt2_1;
- float battVolt2_2;
- float battVolt2_3;
- float shuntVolt2 = 0.0;
- float battLow2 = 0.0;
- int rw2;
- String rw22;
- boolean koniec3;
- float mAh3 = 0.0;
- float mAh3_3;
- float prad3 = 0.0;
- float prad3_3;
- float battVolt3 = 0.0;
- float battVolt3_1;
- float battVolt3_2;
- float battVolt3_3;
- float shuntVolt3 = 0.0;
- float battLow3 = 0.0;
- int rw3;
- String rw33;
- boolean koniec4;
- float mAh4 = 0.0;
- float mAh4_3;
- float prad4 = 0.0;
- float prad4_3;
- float battVolt4 = 0.0;
- float battVolt4_1;
- float battVolt4_2;
- float battVolt4_3;
- float shuntVolt4 = 0.0;
- float battLow4 = 0.0;
- int rw4;
- String rw44;
- unsigned long previousMillis1 = 0;
- unsigned long millisPassed1 = 0;
- unsigned long previousMillis2 = 0;
- unsigned long millisPassed2 = 0;
- unsigned long previousMillis3 = 0;
- unsigned long millisPassed3 = 0;
- unsigned long previousMillis4 = 0;
- unsigned long millisPassed4 = 0;
- String rw_ex = " EXCELENT";
- String rw_good = " GOOD";
- String rw_suf = " SUFFICIENT";
- String rw_low = " LOW";
- String rw_bad = " BAD";
- void przycisk()
- {
- if (digitalRead(BUT1) == LOW)
- {
- delay(100);
- menu ++;
- if (menu > 2) menu = 1;
- }
- }
- void insertBatt()
- {
- display.setCursor(0, 0);
- display.print("Ogniwo ");
- display.print(menu);
- display.print(" BRAK\n\n");
- display.println(" Umiesc ogniwo");
- display.println(" w gniezdzie");
- }
- void battLow()
- {
- battLow(menu);
- }
- void discharge()
- {
- display.setCursor(0, 0);
- display.print("Ogniwo ");
- display.print(menu);
- if (battLow1 == 2.7) display.println(" 4.2V");
- if (battLow1 == 0.9) display.println(" 1.5V");
- display.println("");
- display.print("U: "); display.print(battVolt1); display.print("V "); display.print(battVolt1_1); display.println("V");
- display.print("I: "); display.print(prad1); display.print("A ");display.print(battLow1); display.println("V");
- display.println("");
- display.print(mAh1); display.println("mAh");
- if (battVolt1_2 > 0)
- {
- if (rw1 > 0)
- {
- display.print("Rw: ");
- display.print(rw1);
- display.println("mOhm");
- }
- display.print(rw11);
- }
- }
- void discharge_end()
- {
- display.setCursor(0, 0);
- display.print("Ogniwo ");
- display.print(menu);
- if (battLow1 == 2.7) display.println(" 4.2V");
- if (battLow1 == 0.9) display.println(" 1.5V");
- display.println(" KONIEC");
- display.print("U: "); display.print(battVolt1_3); display.print("V "); display.print(battVolt1_1); display.println("V");
- display.print("I: "); display.print(prad1_3); display.print("A ");display.print(battLow1); display.println("V");
- display.println("");
- display.print(mAh1); display.println("mAh");
- if (rw1 > 0)
- {
- display.print("Rw: ");
- display.print(rw1);
- display.println("mOhm");
- }
- display.print(rw11);
- }
- void setup()
- {
- pinMode(relay1, OUTPUT);
- pinMode(relay2, OUTPUT);
- pinMode(relay3, OUTPUT);
- pinMode(relay4, OUTPUT);
- pinMode(BUT1, INPUT_PULLUP);
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
- display.clearDisplay();
- display.display();
- display.setTextSize(1);
- display.setTextColor(SSD1306_WHITE);
- display.println("");
- display.println(" Tester");
- display.println(" pojemnosci");
- display.println(" ogniw");
- display.display();
- delay(1000);
- pixels.begin();
- menu = 2;
- }
- void loop() {
- przycisk();
- menu ++;
- if (menu > 2) menu = 1;
- battVolt1 = analogRead(highPin1) * napRef / 1024.0;
- shuntVolt1 = analogRead(lowPin1) * napRef / 1024.0;
- battVolt2 = analogRead(highPin2) * napRef / 1024.0;
- shuntVolt2 = analogRead(lowPin2) * napRef / 1024.0;
- battVolt3 = analogRead(highPin3) * napRef / 1024.0;
- shuntVolt3 = analogRead(lowPin3) * napRef / 1024.0;
- battVolt4 = analogRead(highPin4) * napRef / 1024.0;
- shuntVolt4 = analogRead(lowPin4) * napRef / 1024.0;
- if (battVolt1 <= 4.3 && battVolt1 > battLow1) a++;
- if (battVolt1 <= battLow1 && battVolt1 > 0.2) a = 1;
- if (battVolt1 <= 0.2) a = 0;
- if (battVolt2 <= 4.3 && battVolt2 > battLow2) b++;
- if (battVolt2 <= battLow2 && battVolt2 > 0.2) b = 1;
- if (battVolt2 <= 0.2) b = 0;
- if (battVolt3 <= 4.3 && battVolt3 > battLow3) c++;
- if (battVolt3 <= battLow3 && battVolt3 > 0.2) c = 1;
- if (battVolt3 <= 0.2) c = 0;
- if (battVolt4 <= 4.3 && battVolt4 > battLow4) d++;
- if (battVolt4 <= battLow4 && battVolt4 > 0.2) d = 1;
- if (battVolt4 <= 0.2) d = 0;
- if (a == 0)
- {
- pixels.setPixelColor(0, pixels.Color(0,0,250));
- digitalWrite(relay1, LOW);
- battVolt1 = 0;
- battVolt1_1 = 0;
- battVolt1_2 = 0;
- battLow1 = 0;
- prad1 = 0;
- mAh1 = 0;
- shuntVolt1 = 0;
- rw1=0;
- koniec1 = false;
- }
- if (a == 2)
- {
- if(battVolt1 >= 2.7) battLow1 = napEnd_4_2V;
- if(battVolt1 < 2.7) battLow1 = napEnd_1_5V;
- battVolt1_1 = battVolt1;
- }
- if (a == 22)
- {
- battVolt1_2 = battVolt1;
- }
- if (b == 0)
- {
- pixels.setPixelColor(1, pixels.Color(0,0,250));
- digitalWrite(relay2, LOW);
- battVolt2 = 0;
- battVolt2_1 = 0;
- battVolt2_2 = 0;
- battLow2 = 0;
- prad2 = 0;
- mAh2 = 0;
- shuntVolt2 = 0;
- rw2=0;
- koniec2 = false;
- }
- if (b == 2)
- {
- if(battVolt2 >= 2.7) battLow2 = napEnd_4_2V;
- if(battVolt2 < 2.7) battLow2 = napEnd_1_5V;
- battVolt2_1 = battVolt2;
- }
- if (b == 22)
- {
- battVolt2_2 = battVolt2;
- }
- if (c == 0)
- {
- pixels.setPixelColor(2, pixels.Color(0,0,250));
- digitalWrite(relay3, LOW);
- battVolt3 = 0;
- battVolt3_1 = 0;
- battVolt3_2 = 0;
- battLow3 = 0;
- prad3 = 0;
- mAh3 = 0;
- shuntVolt3 = 0;
- rw3=0;
- koniec3 = false;
- }
- if (c == 2)
- {
- if(battVolt3 >= 2.7) battLow3 = napEnd_4_2V;
- if(battVolt3 < 2.7) battLow3 = napEnd_1_5V;
- battVolt3_1 = battVolt3;
- }
- if (c == 22)
- {
- battVolt3_2 = battVolt3;
- }
- if (d == 0)
- {
- pixels.setPixelColor(3, pixels.Color(0,0,250));
- digitalWrite(relay4, LOW);
- battVolt4 = 0;
- battVolt4_1 = 0;
- battVolt4_2 = 0;
- battLow4 = 0;
- prad4 = 0;
- mAh4 = 0;
- shuntVolt4 = 0;
- rw4=0;
- koniec4 = false;
- }
- if (d == 2)
- {
- if(battVolt4 >= 2.7) battLow4 = napEnd_4_2V;
- if(battVolt4 < 2.7) battLow4 = napEnd_1_5V;
- battVolt4_1 = battVolt4;
- }
- if (d == 22)
- {
- battVolt4_2 = battVolt4;
- }
- if(battVolt1 >= 0.2)
- {
- millisPassed1 = millis() - previousMillis1;
- prad1 = (battVolt1 - shuntVolt1) / rezystancja;
- if (prad1 <= 0.00) prad1 = 0.00;
- mAh1 = mAh1 + (prad1 * 1000.0) * (millisPassed1 / 3600000.0);
- rw1 = ((battVolt1_1 * rezystancja) - (battVolt1_2 * rezystancja)) / battVolt1_2 * 1000;
- previousMillis1 = millis();
- }
- if(battVolt1 < battLow1)
- {
- koniec1 = true;
- battVolt1_3 = battVolt1;
- prad1_3 = prad1;
- mAh1_3 = mAh1;
- }
- if(battVolt2 >= 0.2)
- {
- millisPassed2 = millis() - previousMillis2;
- prad2 = (battVolt2 - shuntVolt2) / rezystancja;
- if (prad2 <= 0.00) prad2 = 0.00;
- mAh2 = mAh2 + (prad2 * 1000.0) * (millisPassed2 / 3600000.0);
- rw2 = ((battVolt2_1 * rezystancja) - (battVolt2_2 * rezystancja)) / battVolt2_2 * 1000;
- previousMillis2 = millis();
- }
- if(battVolt2 < battLow2)
- {
- koniec2 = true;
- battVolt2_3 = battVolt2;
- prad2_3 = prad2;
- mAh2_3 = mAh2;
- }
- if(battVolt3 >= 0.2)
- {
- millisPassed3 = millis() - previousMillis3;
- prad3 = (battVolt3 - shuntVolt3) / rezystancja;
- if (prad3 <= 0.00) prad3 = 0.00;
- mAh3 = mAh3 + (prad3 * 1000.0) * (millisPassed3 / 3600000.0);
- rw3 = ((battVolt3_1 * rezystancja) - (battVolt3_2 * rezystancja)) / battVolt3_2 * 1000;
- previousMillis3 = millis();
- }
- if(battVolt3 < battLow3)
- {
- koniec3 = true;
- battVolt3_3 = battVolt3;
- prad3_3 = prad3;
- mAh3_3 = mAh3;
- }
- if(battVolt4 >= 0.2)
- {
- millisPassed4 = millis() - previousMillis4;
- prad4 = (battVolt4 - shuntVolt4) / rezystancja;
- if (prad4 <= 0.00) prad4 = 0.00;
- mAh4 = mAh4 + (prad4 * 1000.0) * (millisPassed4 / 3600000.0);
- rw4 = ((battVolt4_1 * rezystancja) - (battVolt4_2 * rezystancja)) / battVolt4_2 * 1000;
- previousMillis4 = millis();
- }
- if(battVolt4 < battLow4)
- {
- koniec4 = true;
- battVolt4_3 = battVolt4;
- prad4_3 = prad4;
- mAh4_3 = mAh4;
- }
- delay(interwal);
- display.clearDisplay();
- if (koniec1 == false && a > 0)
- {
- pixels.setPixelColor(0, pixels.Color(0,250,0));
- if (a > 3) digitalWrite(relay1, HIGH);
- }
- if (koniec1 == true)
- {
- pixels.setPixelColor(0, pixels.Color(250,0,0));
- digitalWrite(relay1, LOW);
- }
- if (koniec2 == false && b > 0)
- {
- pixels.setPixelColor(1, pixels.Color(0,250,0));
- if (b > 3) digitalWrite(relay2, HIGH);
- }
- if (koniec2 == true)
- {
- pixels.setPixelColor(1, pixels.Color(250,0,0));
- digitalWrite(relay2, LOW);
- }
- if (koniec3 == false && c > 0)
- {
- pixels.setPixelColor(2, pixels.Color(0,250,0));
- if (c > 3) digitalWrite(relay3, HIGH);
- }
- if (koniec3 == true)
- {
- pixels.setPixelColor(2, pixels.Color(250,0,0));
- digitalWrite(relay3, LOW);
- }
- if (koniec4 == false && d > 0)
- {
- pixels.setPixelColor(3, pixels.Color(0,250,0));
- if (d > 3) digitalWrite(relay4, HIGH);
- }
- if (koniec4 == true)
- {
- pixels.setPixelColor(3, pixels.Color(250,0,0));
- digitalWrite(relay4, LOW);
- }
- if (rw1 > 0 && rw1 <= 150) rw11 = rw_ex;
- if (rw1 > 150 && rw1 <= 250) rw11 = rw_good;
- if (rw1 > 250 && rw1 <= 350) rw11 = rw_suf;
- if (rw1 > 350 && rw1 <= 500) rw11 = rw_low;
- if (rw1 >= 500 ) rw11 = rw_bad;
- if (rw2 > 0 && rw2 <= 150) rw22 = rw_ex;
- if (rw2 > 150 && rw2 <= 250) rw22 = rw_good;
- if (rw2 > 250 && rw2 <= 350) rw22 = rw_suf;
- if (rw2 > 350 && rw2 <= 500) rw22 = rw_low;
- if (rw2 >= 500 ) rw22 = rw_bad;
- if (rw3 > 0 && rw3 <= 150) rw33 = rw_ex;
- if (rw3 > 150 && rw3 <= 250) rw33 = rw_good;
- if (rw3 > 250 && rw3 <= 350) rw33 = rw_suf;
- if (rw3 > 350 && rw3 <= 500) rw33 = rw_low;
- if (rw3 >= 500 ) rw33 = rw_bad;
- if (rw4 > 0 && rw4 <= 150) rw44 = rw_ex;
- if (rw4 > 150 && rw4 <= 250) rw44 = rw_good;
- if (rw4 > 250 && rw4 <= 350) rw44 = rw_suf;
- if (rw4 > 350 && rw4 <= 500) rw44 = rw_low;
- if (rw4 >= 500 ) rw44 = rw_bad;
- switch (menu)
- {
- case 1:
- if (a == 0)
- {
- insertBatt();
- }
- if (koniec1 == false && a > 0)
- {
- discharge();
- }
- if (koniec1 == true)
- {
- discharge_end();
- }
- break;
- case 2:
- if (b == 0)
- {
- insertBatt();
- }
- if (koniec2 == false && b > 0)
- {
- discharge();
- }
- if (koniec2 == true)
- {
- discharge_end();
- }
- break;
- case 3:
- if (c == 0)
- {
- insertBatt();
- }
- if (koniec3 == false && c > 0)
- {
- discharge();
- }
- if (koniec3 == true)
- {
- discharge_end();
- }
- break;
- case 4:
- if (d == 0)
- {
- insertBatt();
- }
- if (koniec4 == false && d > 0)
- {
- discharge();
- }
- if (koniec4 == true)
- {
- discharge_end();
- }
- break;
- }
- display.display();
- pixels.show();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement