#include <SD.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
const byte chipSelect = 2;
const byte relay = A1;
float Vbat;
void setup()
{
lcd.begin(16, 2);
lcd.clear();
pinMode(relay, OUTPUT);
digitalWrite(relay, LOW);
if (!SD.begin(chipSelect)) {
lcd.setCursor(0, 0);
lcd.print("Card failed");
return;
}
lcd.print("card initialized.");
}
byte key(){
int val = analogRead(0);
if (val < 50) return 5;
else if (val < 150) return 3;
else if (val < 350) return 4;
else if (val < 500) return 2;
else if (val < 800) return 1;
else return 0;
}// --- конец функции кнопок
void file(float data) {
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(data);
dataFile.close();
}
// if the file isn't open, pop up an error:
else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("error open file");
}
}
void test(){
file(0);
digitalWrite(relay, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" !! test !! ");
do{
Vbat = (analogRead(A3) * 5.0) / 1024.0;
file(Vbat);
lcd.setCursor(0, 1);
lcd.print(Vbat);
lcd.print("V");
delay(1000);
} while(Vbat > 2.5);
digitalWrite(relay, LOW);
lcd.setCursor(0, 0);
lcd.print(" !!!END!!! ");
while(1);
}
void loop()
{
Vbat = (analogRead(A3) * 5.0) / 1024.0;
lcd.setCursor(0, 1);
lcd.print(Vbat);
lcd.print("V");
if (key() == 1){
test();
}
delay(200);
}