Installed the DS3231.
Glitches after 8min.
No idea what to do.
Take a look at the scope pictures (reminder: Thats pin9).
#include <Wire.h>
#include <DS3231.h>
DS3231 clock;
RTCDateTime dt;
#include <SdFat.h>
#include <SPI.h>
#include <LowPower.h>
const int chipSelect = 10;
SdFat SD;
int val = 0;
const int dPin = 2; // interrupt 0
// a few markers used to signal when the new temp. reading is complete etc.
boolean stringComplete = false; // whether the string is complete
bool an = 1; //marker for first startup
int num;
bool plusminus = 0;
char inChar;
int warten = 0;
char buffer[300];
void setup() {
Serial.begin(9600);
buffer[0] = 0; // terminating null byte
delay(250);
clock.begin();
clock.setDateTime(__DATE__, __TIME__); // Set sketch compiling time
dt = clock.getDateTime();
Serial.println(clock.dateFormat("Y.m.d H:i:s", dt));
pinMode(5, OUTPUT); //power to SD
digitalWrite(5, LOW);
pinMode(dPin, INPUT); //interruptpin
pinMode(8, OUTPUT); //power for RTC
digitalWrite(8, HIGH);
pinMode(9, OUTPUT); //debug pin sleep
digitalWrite(9, LOW);
Serial.println(F("an"));
Serial.flush();
}
void loop() {
recvoneChar();
if (an) { //on first start up sleep and wait for signal
attachInterrupt(0, interrupt, CHANGE);
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
detachInterrupt(0);
an = 0;
}
if ((millis() - warten) > 50) { //dont sleep while data comes in
val++;
if (val == 10) { //count 10x data for lower SD-card access -> lower power consumption
stringComplete = true;
val = 0;
}
if (stringComplete) { //print the 10 records to the SD-card
digitalWrite(5, HIGH); //power SD-card (MOSFET)
if (!SD.begin(chipSelect)) { // see if the card is present and can be initialized:
Serial.println(F("SD fail"));
delay(50);
return;
}
File dataFile = SD.open("log.txt", FILE_WRITE);
dataFile.print(buffer);
dataFile.close();
Serial.print(buffer);
Serial.flush();
buffer[0] = 0; // clear the string
stringComplete = false;
digitalWrite(5, LOW);
}
// Serial.println(", sl"); //debug
// Serial.flush();
digitalWrite(9, HIGH); //debug
attachInterrupt(0, interrupt, LOW);
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
detachInterrupt(0);
digitalWrite(9, LOW); //debug
warten = millis();
Serial.println();
// Serial.print("wk ,"); //debug
linefeed = 0;
num = 0;
plusminus = 0;
}
}
void recvoneChar() {
while (Serial.available()) {
inChar = (char)Serial.read(); // get the new byte:
num++;
int l = strlen(buffer); //defines the lenght of the string "buffer" to be int L
// buffer[l++] = inChar; //Counts up one position in "buffer" and puts the inChar into this position
// buffer[l] = 0; // terminating null byte, at position L
if ( (byte) inChar == 177) {
plusminus = 1; //this is the false (due to wakeup) symbol, but its consistent, so its used to toggle new data
buffer[l++] = '\r';
buffer[l++] = '\n';
dt = clock.getDateTime();
l += snprintf(buffer + l, 400 - l, clock.dateFormat("Y.m.d H:i:s", dt));
buffer[l++] = ',';
buffer[l] = 0;
inChar = '\0';
}
if (plusminus) { //new data is coming in, now just put it into the buffer
buffer[l++] = inChar; //Counts up l position in "buffer" and puts the inChar into this position
buffer[l] = 0; // terminating null byte
}
Serial.print(inChar); //debug print to serial
}
}
void interrupt() {
}