#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
#include <RTClib.h>
RTC_DS1307 rtc;
DateTime waktu;
int jam,menit,detik,tahun,bulan,hari;
char temp[20],temp2[8];
const float GAMMA = 0.7;
const float RL10 = 50;
#include "FS.h";
#include "SD.h";
#include "SPI.h";
String NamaFile = "\dataku.txt";
const float BETA = 3950;
void setup() {
lcd.init();
Serial.begin(115200);
rtc.begin();
if(!SD.begin(5))
{
lcd.home();
lcd.print("cek SD Card");
return;
}
waktu = rtc.now();
jam = waktu.hour();
menit = waktu.minute();
detik = waktu.second();
sprintf(temp,"%2d:%2d:%2d:%2d", jam, menit, detik);
buatFile(SD, NamaFile, temp, "waktu,lux");
bacaFile(SD, NamaFile);
lcd.home();
lcd.print(" HELLO WORLD");
delay(100);
// put your setup code here, to run once:
//Serial.begin(115200);
}
void loop() {
lcd.setCursor(0,1);
lcd.print(temp);
Serial.print();
tahun = waktu.year();
bulan = waktu.month();
hari = waktu.day();
sprintf(temp,"%2d/%2d/%2", tahun, bulan, hari);
lcd.setCursor(0,2);
lcd.print(temp);
int analogValue = analogRead(36);
float voltage = analogValue / 4095. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
dtostrf(lux,6,2,temp2);
//sprintf(temp,"Cahaya : %s",temp2);
lcd.setCursor(0,3);
lcd.print(temp);
delay(10); // this speeds up the simulation
}
void cekIsi(fs::FS &fs, const char * dirname, uint8_t levels){
Serial.printf("Listing directory: %s\n", dirname);
File root = fs.open(dirname);
if(!root){
lcd.clear();
lcd.setCursor(0,3);
lcd.print("Gagal buka folder");
return;
}
if(!root.isDirectory()){
lcd.clear();
lcd.setCursor(0,3);
lcd.print("Bukan folder");
return;
}
File file = root.openNextFile();
lcd.clear();
int j=0;
while(file){
if(file.isDirectory()){
Serial.print(" DIR : ");
Serial.println(file.name());
if(levels){
cekIsi(fs, file.name(), levels -1);
}
} else {
lcd.setCursor(0,j);
lcd.print(file.name());
j++;
}
file = root.openNextFile();
}
}
void bacaFile(fs::FS &fs, String path){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Baca file : ");
lcd.setCursor(0,1);
lcd.print(path);
File file = fs.open(path);
if(!file){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Gagal membaca");
return;
}
Serial.print("Read from file: ");
while(file.available()){
Serial.write(file.read());
}
file.close();
}
void buatFile(fs::FS &fs, String path, String waktu ,String message){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Buat file : ");
lcd.setCursor(0,3);
lcd.print(path);
File file = fs.open(path, FILE_WRITE);
if(!file){
lcd.clear();
lcd.setCursor(0,3);
lcd.print("Gagal menulis");
delay(2000);
return;
}
if(file){
// Serial.printf("Appending to file: %s\n", path);
file.print("ini adalah isi ");
file.println(path);
file.print("Mulai direkam pada ");
file.println(waktu);
file.println(message);
lcd.clear();
lcd.setCursor(0,3);
lcd.print("Berhasil menulis");
delay(2000);
} else {
lcd.clear();
lcd.setCursor(0,3);
lcd.print("Gagal menulis");
}
file.close();
}
void isiFile(fs::FS &fs, String path, String message){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Mengisi file : ");
lcd.setCursor(0,1);
lcd.print(path);
File file = fs.open(path, FILE_APPEND);
if(!file){
lcd.clear();
lcd.setCursor(0,3);
lcd.print("Gagal mengisi");
return;
}
if(file.println(message)){
lcd.clear();
lcd.setCursor(0,3);
lcd.print("Isi tertambah");
} else {
lcd.clear();
lcd.setCursor(0,3);
lcd.print("Gagal mengisi");
}
file.close();
}
String ambil_waktu(DateTime waktu)
{
sprintf(temp,"%2d/%2d/%2d %2d:%2d:%2d",waktu.day(),waktu.month(),waktu.year(),waktu.hour(),waktu.minute(),waktu.second());
return temp;
}
float ambil_suhu(int analogValue)
{
float celsius = 1 / (log(1 / (4095. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
return celsius;
}