#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <SD.h>
const int chipSelect = 10;
File file;
int chosen = 0;
float voltage = 2.8;
float voltage_0 = 0;
float curent = 0.5;
int mode = 0;
int samples = 10;
int stav = 0;
float pwm_value = 203;
float mesured_curent = 0;
float mesured_voltage = 0;
float capacity = 0;
float Rinternal = 0;
float wanted_curent = 0.5;
long start_time = 0;
long second_time = 0;
#define but_ok 5
#define but_up 6
#define but_down 7
#define pwm_pin 3
#define curent_pin A2
#define voltage_pin A3
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup() {
Serial.begin(500000);
pinMode(but_ok, INPUT_PULLUP);
pinMode(but_up, INPUT_PULLUP);
pinMode(but_down, INPUT_PULLUP);
pinMode(chipSelect, OUTPUT);
pinMode(pwm_pin, OUTPUT);
pinMode(curent_pin, INPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(3,1);
lcd.print("Li-ion battery");
lcd.setCursor(7,2);
lcd.print("tester");
delay(3000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(">voltage : 2.80 V");
lcd.setCursor(1,1);
lcd.print("curent : 0.50 A");
lcd.setCursor(1,2);
lcd.print("mode : SDcard");
lcd.setCursor(1,3);
lcd.print("sampels : 10 S/m");
}
void loop() {
if(digitalRead(but_ok)==LOW){
delay(300);
if(digitalRead(but_ok)==LOW){
lcd.clear();
if(mode == 1){
USB_Mode();
}
if(mode == 0){
SD_Mode();
}
delay(5000);
}else{
chosen ++;
if(chosen > 3){
chosen = 0;
}
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,2);
lcd.print(" ");
lcd.setCursor(0,3);
lcd.print(" ");
lcd.setCursor(0,chosen);
lcd.print(">");
}
}
if(digitalRead(but_up)==LOW){
delay(350);
if(chosen == 0){
voltage = voltage + 0.05;
if(voltage > 3){
voltage = 3;
}
}
if(chosen == 1){
curent = curent + 0.05;
if(curent > 1.5){
curent = 1.5;
}
}
if(chosen == 2){
mode = 1;
}
if(chosen == 3){
samples++;
if(samples > 60){
samples = 60;
}
}
aktual_values();
}
if(digitalRead(but_down)==LOW){
delay(350);
if(chosen == 0){
voltage = voltage - 0.05;
if(voltage < 0){
voltage = 0;
}
}
if(chosen == 1){
curent = curent - 0.05;
if(curent < 0){
curent = 0;
}
}
if(chosen == 2){
mode = 0;
}
if(chosen == 3){
samples--;
if(samples < 1){
samples = 1;
}
}
aktual_values();
}
}
void aktual_values(){
lcd.setCursor(11,0);
lcd.print(voltage);
lcd.print(" V ");
lcd.setCursor(11,1);
lcd.print(curent);
lcd.print(" A ");
lcd.setCursor(11,2);
if(mode == 0){
lcd.print("SDcard");
}
if(mode == 1){
lcd.print("USB ");
}
lcd.setCursor(11,3);
lcd.print(samples);
lcd.print(" S/m ");
}
void USB_Mode(){
if(stav == 0){
Serial.println("Li-ion battery tester by USB");
lcd.setCursor(3,0);
lcd.print("If you are not");
lcd.setCursor(2,1);
lcd.print("conected, please");
lcd.setCursor(0,2);
lcd.print("conect USB now, and");
lcd.setCursor(1,3);
lcd.print("restart the device.");
delay(1000);
if(digitalRead(but_ok)==LOW){
lcd.clear();
delay(2000);
stav = 1;
}}
if(stav == 1){
lcd.setCursor(2,0);
lcd.print("Start mesuring?");
lcd.setCursor(0,1);
lcd.print("volage : ");
mesured_voltage = analogRead(voltage_pin);
mesured_voltage = 0.00469*mesured_voltage + 0.00532;
lcd.print(mesured_voltage);
lcd.print(" V");
lcd.setCursor(0,2);
lcd.print("trig vol : ");
lcd.print(voltage);
lcd.print(" V");
lcd.setCursor(0,3);
lcd.print("curent : ");
lcd.print(curent);
lcd.print(" A");
if(digitalRead(but_ok) == LOW){
start_mesuring();
}
}
USB_Mode();
}
void SD_Mode(){
if(stav == 0){
lcd.setCursor(0,1);
lcd.print("SD card");
lcd.setCursor(0,2);
lcd.print("inicialization");
if (SD.begin(chipSelect)) {
stav = 1;
delay(1000);
lcd.clear();
}else{
lcd.setCursor(14,2);
lcd.print(".");
delay(300);
lcd.print(".");
delay(300);
lcd.print(".");
delay(300);
lcd.print(".");
delay(300);
lcd.clear();
}
}
if(stav == 1){
lcd.setCursor(2,0);
lcd.print("Start mesuring?");
lcd.setCursor(0,1);
lcd.print("volage : ");
mesured_voltage = analogRead(voltage_pin);
mesured_voltage = 0.00469*mesured_voltage + 0.00532;
lcd.print(mesured_voltage);
lcd.print(" V");
lcd.setCursor(0,2);
lcd.print("trig vol : ");
lcd.print(voltage);
lcd.print(" V");
lcd.setCursor(0,3);
lcd.print("curent : ");
lcd.print(curent);
lcd.print(" A");
if(digitalRead(but_ok) == LOW){
start_mesuring();
}
}
SD_Mode();
}
void start_mesuring(){
lcd.clear();
if(mode == 0){
int maxFileNumber = findMaxFileNumber();
int newFileNumber = maxFileNumber + 1;
char fileName[8];
sprintf(fileName, "%03d", newFileNumber);
file = SD.open(fileName, FILE_WRITE);
if(file){
lcd.setCursor(0,1);
lcd.print("Created new file");
lcd.setCursor(0,2);
lcd.print("under name: ");
lcd.print(fileName);
lcd.print(".txt");
delay(3000);
start_time = millis()/1000;
second_time = millis()/1000;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("time: 0m 0s");
lcd.setCursor(0,1);
lcd.print("voltage: 0.00V");
lcd.setCursor(0,2);
lcd.print("Rinternal:0.00");
lcd.write(0xF4);
lcd.setCursor(0,3);
lcd.print("capacity: 0mAh");
mesure_SD();
}else{
lcd.setCursor(5,1);
lcd.print("file error");
delay(3000);
}
}
if(mode == 1){
mesure_USB();
}
if(mode == 100){
file.close();
}
}
int findMaxFileNumber() {
int maxNumber = 0;
File root = SD.open("/");
while (true) {
File entry = root.openNextFile();
if (!entry) {
// Konec složky
break;
}
if (entry.isDirectory()) {
// Ignoruj složky
continue;
}
String fileName = entry.name();
int fileNumber = fileName.toInt();
if (fileNumber > maxNumber) {
maxNumber = fileNumber;
}
entry.close();
}
root.close();
return maxNumber;
}
void mesure_SD(){
wanted_curent = curent;
drive_curent();
capacity = (millis()/1000-start_time);
capacity = capacity/3.6;
capacity = capacity * curent;
lcd.setCursor(10,0);
lcd.print((millis()/1000-start_time)/60);
lcd.print("m ");
lcd.print((millis()/1000-start_time)%60);
lcd.print("s ");
lcd.setCursor(10,1);
lcd.print(mesured_voltage);
lcd.print("V ");
lcd.setCursor(10,2);
lcd.print(Rinternal);
lcd.write(0xF4);
lcd.print(" ");
lcd.setCursor(10,3);
lcd.print(capacity);
lcd.print("mAh ");
if(millis()/1000-second_time > 60/samples){
second_time = second_time + millis()/1000-second_time-1;
analogWrite(pwm_pin, 0);
delay(5);
voltage_0 = analogRead(voltage_pin);
voltage_0 = 0.00469*voltage_0 + 0.00532;
analogWrite(pwm_pin, pwm_value);
if(voltage_0 <= voltage){
lcd.clear();
//File file.close();
mode = 100;
start_mesuring();
while(true){
delay(60000);
}
}
}
mesure_SD();
}
void mesure_USB(){
mesure_USB();
}
void drive_curent(){
mesured_curent = analogRead(curent_pin);
mesured_curent = 0.0469*mesured_curent + 0.0532;
mesured_voltage = analogRead(voltage_pin);
mesured_voltage = 0.00469*mesured_voltage + 0.00532;
analogWrite(pwm_pin, pwm_value);
if(mesured_curent < wanted_curent){
pwm_value = pwm_value + 0.1;
}
if(mesured_curent > wanted_curent){
pwm_value = pwm_value - 0.1;
}
if(pwm_value < 0){
pwm_value = 0;
}
if(pwm_value > 255){
pwm_value = 255;
}
}