Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <SD.h>
- #include <Wire.h>
- #include <DS3231.h>
- File root, oke;
- DS3231 RTC;
- const int chipSelect = 10;
- int detik=0;
- int menit=0;
- int jam=0;
- boolean dataLengkap=false;
- String dataString="";
- String dataTgl="";
- String dataku="";
- boolean katalengkap=false;
- boolean ambilFile=false;
- int mode=4;//default
- void setup(){
- Wire.begin();
- RTC.begin();
- Serial.begin(9600);
- pinMode(10, OUTPUT);
- if (!SD.begin(10))return;
- }
- void loop(){
- DateTime now = RTC.now();
- dataString="";
- dataString=String(now.date())+'/';
- dataString+=String(now.month())+'/';
- dataString+=String(now.year())+',';
- dataString+=String(now.hour())+':';
- dataString+=String(now.minute())+':';
- dataString+=String(now.second())+',';
- dataTgl="";
- dataTgl='z'+String(now.date());
- dataTgl+=String(now.month());
- dataTgl+=String(now.year())+".txt";
- // dataTgl='z'+String(now.minute())+".txt";
- if(mode==1){
- if(now.second()==0 && menit!=now.minute()){
- simpan();
- menit=now.minute();}}
- if(mode==2){
- if(now.minute()%5==0 && detik!=now.second() && menit!=now.minute()){
- simpan();
- detik=now.second();
- menit=now.minute();}}
- if(mode==3){
- if(now.minute()%10==0 && detik!=now.second() && menit!=now.minute()){
- simpan();
- detik=now.second();
- menit=now.minute();}}
- if(mode==4){
- if(now.minute()%15==0 && detik!=now.second() && menit!=now.minute()){
- simpan();
- detik=now.second();
- menit=now.minute();}}
- if(mode==5){
- if(now.minute()%20==0 && detik!=now.second() && menit!=now.minute()){
- simpan();
- detik=now.second();
- menit=now.minute();}}
- if(mode==6){
- if(now.minute()%30==0 && detik!=now.second() && menit!=now.minute()){
- simpan();
- detik=now.second();
- menit=now.minute();}}
- if(mode==7){
- if(now.second()==0 && now.minute()==0 && jam!=now.hour()){
- simpan();
- jam=now.hour();}}
- if(katalengkap){
- if(dataku[0]=='j') {
- dataku=dataku.substring(1);
- char namafileku[dataku.length()];
- dataku.toCharArray(namafileku,sizeof(namafileku));
- SD.remove(namafileku);}
- else{
- char namafileku[dataku.length()];
- dataku.toCharArray(namafileku,sizeof(namafileku));
- File dataFile = SD.open(namafileku);
- if (dataFile) {
- while(dataFile.available()){
- Serial.write(dataFile.read());}
- dataFile.close();}}
- dataku="";
- katalengkap=false;}
- if(ambilFile){
- root = SD.open("/");
- printDirectory(root);
- ambilFile=false;}
- }
- void printDirectory(File sip) {
- sip.rewindDirectory();
- while(true) {
- File masuk = sip.openNextFile();
- if (!masuk) {
- break;}
- Serial.println(masuk.name());
- File dataFile = SD.open(masuk.name());
- if (dataFile) {
- while(dataFile.available()){
- Serial.write(dataFile.read());}
- dataFile.close();}
- Serial.println('x');
- SD.remove(masuk.name());
- masuk.close();}
- }
- void serialEvent(){
- while(Serial.available()){
- char ambil=(char)Serial.read();
- if(ambil=='a') mode=1;
- if(ambil=='b') mode=2;
- if(ambil=='c') mode=3;
- if(ambil=='d') mode=4;
- if(ambil=='e') mode=5;
- if(ambil=='f') mode=6;
- if(ambil=='g') mode=7;
- if(ambil=='h') ambilFile=true;
- else dataku+=ambil;
- if(ambil=='\n') katalengkap=true;
- }}
- void simpan(){
- for (int analogPin = 0; analogPin < 3; analogPin++) {
- int sensor = analogRead(analogPin);
- dataString += String(sensor);
- if (analogPin < 2) dataString += ',';}
- char namafileku[dataTgl.length()+1];
- dataTgl.toCharArray(namafileku,sizeof(namafileku));
- File dataFile = SD.open(namafileku,FILE_WRITE);
- if (dataFile) {
- dataFile.print('w');
- dataFile.println(dataString);
- dataFile.close();
- Serial.println(dataString);}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement