Ok so here's the code which has the function to read the files from SD, find values of interest. I'm intending to store them within objects as atributes in order to compare them with the RTC and after this comparison i will make decisions with solenoids.
CODE 1 : (read,find values of interest)
#include <SD.h>
#include <Wire.h>
#include <SPI.h>
#include <libAC.h>
File arquivo;
void setup()
{
Serial.begin(9600);
pinMode(53,OUTPUT);
if(!SD.begin(53)){
Serial.println("O cartão falhou ao iniciar ou não está presente"); // Trocar "Serial.printl" por "Registrador:error" após os testes.
return;
}
Serial.println("Cartão iniciado !");
OpenReadArquivo();
LeVirgula;
}
else{
Serial.println("O arquivo já terminou de ser lido");
}
}
void OpenReadArquivo()
{
int setor,sequencial,i,k,j,n;
float volumeAgua = 0 ;
float volumeFert = 0;
float duracao;
char c, d, s,f, v;
arquivo = SD.open("Ferti.cal");
if (arquivo)
{
Serial.println("Ferti.cal:");
LeVirgula();
LeVirgula();
EncontraDiaHora();
LeDoisPontos();
LeDoisPontos();
setor = ((arquivo.read()-48));
Serial.println(setor);
LeDoisPontos();
//--------------------------------------------------------------//
d = arquivo.read();
duracao = 0;
while(d != ','){
duracao = duracao * 10 + ((d -48));
d = arquivo.read();
}
Serial.println(duracao);
//--------------------------------------------------------------//
LeDoisPontos();
sequencial = ((arquivo.read()-48));
Serial.println(sequencial);
//--------------------------------------------------------------//
LeDoisPontos();
LeDoisPontos();
//--------------------------------------------------------------//
c = arquivo.read();
i=1;
while(c != '.'){
volumeAgua = volumeAgua * 10 + (c - 48);
c = arquivo.read();
}
c = arquivo.read();
while(c != ','){
volumeAgua = volumeAgua + (c-48)*(pow(10,-i));
i++;
c=arquivo.read();
}
Serial.println(volumeAgua);
//--------------------------------------------------------------//
LeDoisPontos();
LeDoisPontos();
//--------------------------------------------------------------//
k=0; s=0;
s = arquivo.read();
while(s!=','){
Serial.print(s);
s = arquivo.read();
k++;
if (s==','){
break;
}
}
//--------------------------------------------------------------//
LeDoisPontos();
LeDoisPontos();
//--------------------------------------------------------------//
v = arquivo.read();
j=1;i=1;
while(v != '.'){
volumeFert = volumeFert * 10 + (v - 48);
v = arquivo.read();
}
v = arquivo.read();
while(v != '}'){
volumeFert = volumeFert + (v-48)*(pow(10,-i));
j++;
v=arquivo.read();
}
Serial.println();
Serial.println(volumeFert);
arquivo.close();
} else
{
Serial.println("Erro ao abrir o Ferti.cal");
}
}
void LeVirgula (){ // função que lê os caracteres do arquivo até encontrar ',' (virgula) que separa seus parâmetros.
char caracter;
while(arquivo.available())
{
caracter = arquivo.read();
if(caracter==',') break;
}
}
void EncontraDiaHora () {
int i, dia, hora, minutos,segundos;
LeDoisPontos();
LeDoisPontos();
dia = ((arquivo.read()-48));
Serial.println(dia);
LeDoisPontos();
LeAspaDupla();
hora= ((arquivo.read()-48)*10);
hora= hora +((arquivo.read() - 48));
Serial.println(hora);
LeDoisPontos();
minutos = ((arquivo.read()-48)*10);
minutos = minutos + ((arquivo.read() - 48));
Serial.println(minutos);
LeDoisPontos();
segundos = ((arquivo.read()-48)*10);
segundos = segundos + ((arquivo.read() - 48));
Serial.println(segundos);
}
void LeDoisPontos () { //function that reads the characters of the file until you find ': ' (colon) that separates its parameters.
char caracter;
while(arquivo.available()){
caracter=arquivo.read();
if (caracter==':') break;
}
}
void LeAspaDupla () { // function that reads the file until you find the string ' "' (quotation marks) that separates its parameters.
char caracter;
while(arquivo.available()){
caracter = arquivo.read() ;
if (caracter=='"') break;
}
}
If you take this byte chain :
{"Identificação":1002,"Programa":"PRG VRO","Agendamentos":[{"Dia":3,"Hora":"05:00:00","Eventos":[{"Setor":2,"Duração":1500,"Sequencial":3,"Receita":"REC01","Água":1.5,"Fertilizantes":[{"Silo":"Silo 1","Fertilizante":"Potássio","Volume":2.3}]}]}
CODE 1 will return all the values of interest
Alright that's what I have so far, sorry if this is not the most optimized way to achieve my goals, but as I told you before, I'm beginner in Arduino. Since you all know the procedement to find the values of interest here's the code which provides DATE,time. I will compare those values of interest of CODE 1 with the provided time of this CODE 2.
CODE 2 (RTC)
#include "Wire.h"
#define DS1307_ADDRESS 0x68
void setup(){
Wire.begin();
Serial.begin(9600);
}
void loop(){
printDate();
delay(1000);
}
byte bcdToDec(byte val) {
// Converte BCD para numeros decimais em sua forma normal
return ( (val/16*10) + (val%16) );
}
void printDate(){
// Reset apontador registrador
Wire.beginTransmission(DS1307_ADDRESS);
byte zero = 0x00;
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
int second = bcdToDec(Wire.read());
int minute = bcdToDec(Wire.read());
int hour = bcdToDec(Wire.read() & 0b111111); //formato 24h por dia
int weekDay = bcdToDec(Wire.read()); //0-6 -> domingo - Sabado
int monthDay = bcdToDec(Wire.read());
int month = bcdToDec(Wire.read());
int year = bcdToDec(Wire.read());
Serial.print(month);
Serial.print("/");
Serial.print(monthDay);
Serial.print("/");
Serial.print(year);
Serial.print(" ");
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.println(second);
}
This is huge right now and I apologize. I would appreciate all fellow's time to understand the situation and show me some light in the dark ahead. Again, thanks