Hi,
I want to log data from an Eeprom to a SD-Card using an Uno with an Adafruit SD-Card shield. To save power, I have a watch dog implemented, that is sniffing every 8 seconds for data.
Unfortunately, the code will not even create a file on my SD-Card, also I've been troubleshooting for days now. Is one of you able to help?
Thanks
Christoph
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>
#include <SD.h>
#define LED_PIN (13)
volatile int f_wdt=1;
int counter = 1; //initialize to 1
int X = 1; //# of watchdog interrupt cycles between data downloading sequence. Each cycle is ~8 seconds, so 2700 cycles = 6 hours
File myFile;
int byteNumber = 1;
char a[9];
int placeHolder = 1;
int dummyVar = 1;
int tempTimeStart;
int tempTimeEnd;
int variable = 3;
void enterSleep(void)
{
set_sleep_mode(SLEEP_MODE_PWR_SAVE); /* EDIT: could also use SLEEP_MODE_PWR_DOWN for lowest power consumption. */
sleep_enable();
/* Now enter sleep mode. */
sleep_mode();
/* The program will continue from here after the WDT timeout*/
sleep_disable(); /* First thing to do is disable sleep. */
/* Re-enable the peripherals. */
power_all_enable();
}
void setup()
{
pinMode(13, OUTPUT);
/*** Setup the WDT ***/
/* Clear the reset flag. */
MCUSR &= ~(1<<WDRF);
/* In order to change WDE or the prescaler, we need to
* set WDCE (This will allow updates for 4 clock cycles).
*/
WDTCSR |= (1<<WDCE) | (1<<WDE);
/* set new watchdog timeout prescaler value */
WDTCSR = 1<<WDP0 | 1<<WDP3; /* 8.0 seconds */
/* Enable the WD interrupt (note no reset). */
WDTCSR |= _BV(WDIE);
pinMode(10, OUTPUT);
a[8] = 0;
Serial.setTimeout(5000);
delay(7000); //delay 7 seconds every time battery is restarted so the bottom arduino has time to initialize. It can't be more than 8 seconds or the watchdog timer runs over.
SD.begin();
}
void loop()
{
if(f_wdt == 1) //Every 8 seconds the Arduino will wake. Every X wakeups it will toggle the LED
{
if(counter == X){ //If the Arduino has woken up X number of times, run through data-gathering sequence
downloadData();
counter = 1; //reset the counter variable to 1 for sleep cycle code
}
else{
counter = counter + 1; //update counter variable
}
/* Don't forget to clear the flag. */
f_wdt = 0;
/* Re-enter sleep mode. */
enterSleep();
}
else
{
/* Do nothing. */
}
}
void downloadData(void)
{while (dummyVar > 0){
myFile = SD.open("Sensor4.txt", FILE_WRITE);
Serial.begin(57600);
delay(1000);
Serial.print('k');
tempTimeStart = millis();
while(!Serial.available()){
tempTimeEnd = millis();
if ((tempTimeEnd - tempTimeStart) > 5000){ //5 seconds of no serial contact has elapsed, re-start while loop by setting dummyVar to 0.
dummyVar = 0; Serial.readBytes times out
}
};
while(Serial.available() > 0){
Serial.readBytes(a,8);
delay(1);
myFile.print(a);
}
Serial.print('A') ;
tempTimeStart = millis();
while(!Serial.available()){
tempTimeEnd = millis();
if ((tempTimeEnd - tempTimeStart) > 1000){
dummyVar = 0;
}
}
tempTimeStart = millis(); //record temporary time
while (placeHolder > 0){ //Keep looping until Serial.readBytes doesn't see any more data in buffer
byteNumber = Serial.readBytes(a,8); //read 8 bytes at a time
tempTimeEnd = millis(); //record temporary time
if ((tempTimeEnd - tempTimeStart) > 10000){ //once it takes Serial.readBytes longer than XX seconds to get more data, exit while loop
placeHolder = 0; //causes while loop to end when Serial.readBytes times out
}
myFile.print(a); //Send data to SD card
}
myFile.print("Bytes available on buffer right NOW: ");
myFile.println(Serial.available()); //Make sure no data remains in the buffer
Serial.print('X'); //delete data
Serial.print('Y'); //confirm delition
Serial.print('E'); //exit from logging mode
myFile.close(); // Close file (otherwise it doesn't save properly)
Serial.end(); //Close serial connection
placeHolder = 1; //reset placeholder to 1
break; //end the downloadData function once data has been collected
}
myFile.close(); // Close file (otherwise it doesn't save properly)
Serial.end(); //Close serial connection
dummyVar = 1; //reset dummyVar to 1 to enter while loop again, this way Arduino gets another shot at establishing serial connection
}