//Library
#include <RTClib.h>
#include <Wire.h>
#include <SD.h>
#include <SPI.h>
RTC_DS3231 rtc;
char dataHari[7][12] = {"Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu"};
String hari;
int tanggal, bulan, tahun, jam, menit, detik;
float suhu;
int switchreed = 2; //Reed switch DO pin connected to pin 2 of Arduino
int switchAmount;
float rainIntensity;
int s, step;
const int chipSelect = 10; //SD card CS pin connected to pin 10 of Arduino
void setup()
{
Serial.begin(9600);
Wire.begin();
Initialize_SDcard();
Initialize_RTC();
pinMode(switchreed, INPUT); //Inisialisasi Reed Switch
delay(200);
attachInterrupt(digitalPinToInterrupt(switchreed), switchCount, FALLING);
s = 0;
step = 0;
}
void switchCount()
{
switchAmount++;
step = 1;
}
void loop() {
first:
if (step == 0)
{
s = s + 1;
delay(1);
goto first;
}
if (step == 1)
{
rainIntensity = 29.6 * switchAmount;
Serial.print("Intensitas : ");
Serial.print(rainIntensity);
Serial.println(" Milimeter");
Serial.print("Time : ");;
Serial.print(s);
Serial.println(" Microsecond");
step = 2;
}
if (step == 2)
{
s = 0;
step = 0;
}
DateTime now = rtc.now();
hari = dataHari[now.dayOfTheWeek()];
tanggal = now.day(), DEC;
bulan = now.month(), DEC;
tahun = now.year(), DEC;
jam = now.hour(), DEC;
menit = now.minute(), DEC;
detik = now.second(), DEC;
suhu = rtc.getTemperature();
Write_SDcard();
delay(1000);
}
void Initialize_RTC()
{
// Initialize the rtc object
rtc.begin();
//Atur Waktu
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
void Initialize_SDcard()
{
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("LoggerCD.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println("Date,Time,Intensitas"); //Write the first row of the excel file
dataFile.close();
}
}
void Write_SDcard()
{
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("LoggerCD.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.print(tanggal); //Store date on SD card
dataFile.print(","); //Move to next column using a ","
dataFile.print(jam); //Store date on SD card
dataFile.print(","); //Move to next column using a ","
dataFile.print(rainIntensity); //Store date on SD card
dataFile.print(","); //Move to next column using a ","
dataFile.println(); //End of Row move to next row
dataFile.close(); //Close the file
}
else
Serial.println("OOPS!! SD card writing failed");
}