In my project I use an Arduino Mega board with an Ethernet Shield that I use to read a text file from an SD card. I am also using a DS3231 watch and a servo.
I must read the .txt file of the SD and place the content in the char "hours" in the following code
it is necessary that the time be taken from the file.
Thanks
#include <Servo.h>
#include <DS3231.h>
#include <Wire.h>
char *horas[]={ //----------------------------------------------
"16:45", // i have to put the content of the .txt file
"16:46", // here with the same format
"16:47", //
"16:47", //
"16:49" //-----------------------------------------------
};
long nhoras = (sizeof(horas)/sizeof(char *));
bool h12;
bool PM;
DS3231 Clock;
Servo servoMotor;
byte year, month, date, DoW, hour, minute, second;
//---------------------------------------------------------------------------------------------------
void setup() {
Wire.begin();
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
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());
}
//---------------------------------------------------------------------------------------------------
void loop() {
delay(1000);
Clock.getTime(year, month, date, DoW, hour, minute, second);
if(deboGirar()==1)
girar();
}
//---------------------------------------------------------------------------------------------------
void girar(){
servoMotor.attach(9);
int i;
for (i=0;i<4;i++){
servoMotor.write(0);
delay(500);
servoMotor.write(90);
delay(500);
}
servoMotor.detach();
}
int deboGirar(){
String h = "";
String m = "";
for(int i=0;i<nhoras;i++){
h= getValue(horas[i], ':', 0);
m= getValue(horas[i], ':', 1);
hour = Clock.getHour(h12, PM);
if(int(hour)==atoi(h.c_str()) && int(minute)==atoi(m.c_str()) && int(second)==0)
return 1;
}
return 0;
}
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}