#include #include // pin 3 - Serial clock out (SCLK) // pin 4 - Serial data out (DIN) // pin 5 - Data/Command select (D/C) // pin 7 - LCD chip select (CS) // pin 6 - LCD reset (RST) int sensorPin = 0; // к пину Analog IN 0 int sensor2Pin = 2; // к пину Analog IN 2 int SPKPin = 6; // пин к затвору транзистора int ledPin = 13; // LED подсоединен к выводу 13 Adafruit_PCD8544 display = Adafruit_PCD8544(3, 4, 5, 7, 6); // инициализация библиотеки с перечнем задействованных выводов int sensorValue = 0; // variable to store the value coming from the sensor (получаем текущее значение) int sensor2Value = 0; // variable to store the value coming from the sensor float LiMinThreshold = 2700; // Lithium Minimal Voltage for load removal float LiMaxThreshold = 4200; // Lithium Max Voltage for load removal float NmhMinThreshold = 950; // NMH Minimal Voltage for load removal float NmhMaxThreshold = 1600; // NMH Max Voltage for load removal float SelectedMinThreshold = 5000; int i; int BatVoltage = 5000; int FetVoltage = 5000; long TotalCurrent = 0; boolean done = false; unsigned long PrevMillis ; unsigned long MillisPassed ; void CL2(){ display.setCursor(0, 1);// Second line first char display.print(" "); display.setCursor(0, 1);// Second line first char } void setup() { Serial.begin(9600);// start serial port to send data during run to the PC pinMode(ledPin, OUTPUT);//activation led and enable for FET (устанавливаем вывод 13 как выход) pinMode(SPKPin, OUTPUT);//activation led and enable for FET display.begin();// устанавливаем кол-во столбцов и строк: display.print("Bat PWR Tester[Active]"); // печать сообщения на LCD. display.setCursor(0, 1);// Second line first char display.print("Detecting Bat Type..."); // print voltage value delay(2000); display.setCursor(0, 1);// Second line first char display.print(" "); display.setCursor(0, 1);// Second line first char digitalWrite(ledPin, HIGH); // включаем LED sensorValue = analogRead(sensorPin); // read the value from the sensor: digitalWrite(ledPin, LOW); // выключаем LED // Detecting battery type BatVoltage = sensorValue*4.887; if (BatVoltage > 4500){ display.print("Warning high-V! "); done = true;} else if (BatVoltage > LiMinThreshold){ display.print("Type:Li-Ion Bat "); SelectedMinThreshold = LiMinThreshold;} else if (BatVoltage > NmhMinThreshold){ display.print("Type:NiMH/Cd Bat "); SelectedMinThreshold = NmhMinThreshold;} else{ display.print("Unknown Bat V < 1"); done = true;} display.print("V="); display.print(sensorValue*4.887); // Печать значения напряжения Serial.print("DT[ms]"); Serial.print("\t"); Serial.print("Bat[mV]"); Serial.print("\t"); Serial.print("Fet[mV]"); Serial.println(""); delay(3000); CL2(); PrevMillis = millis(); } void loop() { if (BatVoltage > SelectedMinThreshold && !done) { digitalWrite(ledPin, HIGH); // set the LED on sensorValue = analogRead(sensorPin); // read the value from the sensor: sensor2Value = analogRead(sensor2Pin); // read the value from the FET: FetVoltage = (sensor2Value*4.887); BatVoltage = (sensorValue*4.887); CL2(); display.print("V="); display.print(BatVoltage); // print voltage value display.print("mV"); //lcd.print(FetVoltage); // print voltage value TotalCurrent=TotalCurrent+MillisPassed/1000*(BatVoltage-FetVoltage)/2.2/3.6; display.print(" I="); display.print(TotalCurrent/1000); display.print("mAH "); delay(1000); MillisPassed = millis()- PrevMillis; PrevMillis = millis(); Serial.print(int(MillisPassed)); Serial.print("\t"); // prints a tab Serial.print(BatVoltage); Serial.print("\t"); // prints a tab Serial.print(FetVoltage); Serial.println(""); // prints a tab CL2(); } else { done=true; digitalWrite(ledPin, LOW); // set the LED off - stop loading display.setCursor(0, 0);// First line first char display.print("Bat Power Tester [DONE] "); // Print a message to the LCD. CL2();//clear line 2 sensorValue = analogRead(sensorPin); // read the value from the sensor: BatVoltage = (sensorValue*4.887); display.setCursor(0, 1);// Second line first char display.print("V="); display.print(BatVoltage); // print voltage value display.print("mV I="); display.print(TotalCurrent/1000); display.print("mAH "); for (int i=0; i<100 ; i++){ digitalWrite(SPKPin, HIGH); delay(1); digitalWrite(SPKPin, LOW); delay(1); } delay(1000); } }