#include <OneWire.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <DS3231.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
DS3231 rtc(SDA, SCL);
OneWire ds(7);// on pin 7 (a 4.7K resistor is necessary)
int suhu;
int dataadc;
float ph;
int btset = 2;
int btup = 3;
int btdown = 5;
int btok = 6;
int btsetx;
int btupx;
int btdownx;
int btokx;
int layar;
float kal;
int positionCounter;
File myFile;
// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 4;
void setup() {
Serial.begin(9600);
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(SS, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
lcd.begin(); //setting lcd
lcd.clear(); //clear screen
lcd.noCursor(); //tidak ada kursor
rtc.begin();
//The following lines can be uncommented to set the date and time
//rtc.setDOW(SUNDAY); // Set Day-of-Week to SUNDAY
//rtc.setTime(15, 46, 0); // Set the time to 12:00:00 (24hr format)
//rtc.setDate(3, 4, 2022); // Set the date to January 1st, 2014
pinMode(btset,INPUT_PULLUP);
pinMode(btup,INPUT_PULLUP);
pinMode(btdown,INPUT_PULLUP);
pinMode(btok,INPUT_PULLUP);
}
void loop(void) {
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit;
if ( !ds.search(addr)) {
ds.reset_search();
delay(250);
return;
}
for( i = 0; i < 8; i++) {
}
if (OneWire::crc8(addr, 7) != addr[7]) {
return;
}
switch (addr[0]) {
case 0x10:
type_s = 1;
break;
case 0x28:
type_s = 0;
break;
case 0x22:
type_s = 0;
break;
default:
return;
}
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
delay(1000); // maybe 750ms is enough, maybe not
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
}
celsius = (float)raw / 16.0;
fahrenheit = celsius * 1.8 + 32.0;
suhu = celsius;
dataadc = analogRead(A0);
ph = (dataadc + 11.026)/58.009;
ph = ph + kal;
btsetx = digitalRead(btset);
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);
btokx = digitalRead(btok);
if(btokx == 0){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("SAVING.... ");
simpan();
delay(3000);
lcd.clear();
}
if(btsetx == 0){
lcd.clear();
delay(200);
setting();
lcd.clear();
}
if(btupx == 0){
layar++;
}
if(layar > 1){
layar = 0;
}
if(layar == 0){
lcd.setCursor(0,0);
lcd.print("T: ");
lcd.print(suhu);
lcd.print(" C ");
lcd.setCursor(0,1);
lcd.print("PH: ");
lcd.print(ph);
lcd.print(" ");
}
if(layar == 1){
lcd.setCursor(0,0);
lcd.print(rtc.getDateStr());
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(rtc.getTimeStr());
lcd.print(" ");
}
delay(100);
}
void setting(){
dataadc = analogRead(A0);
ph = (dataadc + 11.026)/58.009;
ph = ph + kal;
btsetx = digitalRead(btset);
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);
btokx = digitalRead(btok);
lcd.setCursor(0,0);
lcd.print("Kal PH: ");
lcd.print(ph);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("Kal: ");
lcd.print(kal);
lcd.print(" ");
if(btupx == 0){
delay(200);
kal = kal + 0.01;
}
if(btdownx == 0){
delay(200);
kal = kal - 0.01;
}
if(btokx == 0){
delay(200);
return;
}
setting();
}
void simpan(){
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
//pinMode(10, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
myFile.print("Tanggal=");
myFile.print(rtc.getDateStr());
myFile.print(" Waktu=");
myFile.println(rtc.getTimeStr());
myFile.print("PH= ");
myFile.print(ph);
myFile.print(" Suhu= ");
myFile.println(suhu);
myFile.println();
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}