I updated the code, replaced the big string with the buffer and im also using the 2nd char, byte 177 (tho its false but thats what arduino reads right after wakeup with clock initialization), to start mark the new linke eg. add the datestring.
But I run into problems while compiling:
read-rs232-log-13:29: error: 'buffer' does not name a type
buffer[0] = 0; // terminating null byte
^
C:\Users\Leo\Documents\Arduino\read-rs232-log-13\read-rs232-log-13.ino: In function 'void recvoneChar()':
read-rs232-log-13:143: error: cannot convert 'String' to 'const char*' for argument '2' to 'char* strncat(char*, const char*, size_t)'
strncat(buffer, datestring, 25); //Output would be "buffer" combined with "This is the string to add"
^
exit status 1
'buffer' does not name a type
Isnt "buffer" defined as "char"?
New code:
#include <Wire.h>
#include <Time.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
#include <SdFat.h>
#include <SPI.h>
#include <LowPower.h>
const int chipSelect = 10;
SdFat SD;
int val = 0;
const int dPin = 2; // interrupt 0
String inputString = ""; // a string to hold incoming data
String datestring = ""; // strong for date assembly
boolean stringComplete = false; // whether the string is complete
bool fertig = 1;
int warten = 0;
bool linefeed = 0;
bool an = 1;
int num;
char inChar;
boolean newData = false;
char buffer[350];
buffer[0] = 0; // terminating null byte
bool plusminus = 0;
void setup() {
Serial.begin(9600);
// inputString.reserve(312); // reserve 312 bytes for the inputString
datestring.reserve(32);
delay(250);
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);
digitalWrite(9, LOW);
Serial.println("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 the 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);
if (!SD.begin(chipSelect)) { // see if the card is present and can be initialized:
Serial.println("Card failed or not present");
delay(50);
return;
}
File dataFile = SD.open("log.txt", FILE_WRITE);
dataFile.print(buffer);
dataFile.close();
// Serial.println();
Serial.print(buffer);
// Serial.println();
Serial.flush();
inputString = ""; // clear the string:
stringComplete = false;
digitalWrite(5, LOW);
}
// Serial.println(", sl"); //debug to see what happens when
// Serial.flush();
digitalWrite(9, HIGH);
attachInterrupt(0, interrupt, LOW);
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
detachInterrupt(0);
digitalWrite(9, LOW);
warten = millis();
// Serial.print("wk ,");
linefeed = 0;
num = 0;
plusminus = 0;
}
}
void recvoneChar() {
while (Serial.available()) {
inChar = Serial.read();
num++;
tmElements_t tm;
if (RTC.read(tm)) {
datestring = String(tmYearToCalendar(tm.Year)) + "." + String(tm.Month) + "." + String(tm.Day) + " " + String(tm.Hour) + ":" + String(tm.Minute) + ":" + String(tm.Second);
// Serial.print(datestring);
} else {
if (RTC.chipPresent()) { //this never shows up, so the I2C itself failed
Serial.println(F("The DS1307 is stopped. Please run the SetTime"));
Serial.println(F("example to initialize the time and begin running."));
Serial.println();
} else {
Serial.println(F("DS1307 read error! Please check the circuitry."));
Serial.println();
}
delay(2000);
}
int l = strlen(buffer); //defines the lenght of the string(?!) to be l, eg. add one char
buffer[l++] = inChar; //Counts up l position in "buffer" and puts "c" into this position, I would put my "inChar" here
// buffer[l] = 0; // terminating null byte, but why fixed at position 1?
if ( (byte) inChar == 177) {
plusminus = 1;
buffer[l++] = '\r';
buffer[l++] = '\n';
strncat(buffer, datestring, 25); //Output would be "buffer" combined with a max of 25 chars of "datestring"
buffer[l++] = ',';
}
if (plusminus) {
buffer[l++] = inChar; //Counts up l position in "buffer" and puts "c" into this position, I would put my "inChar" here
buffer[l] = 0; // terminating null byte
}
Serial.println(inChar);
}
}
void interrupt() {
}
For the wake-up glitch: I changed the code a little, so there is only one sleep that can only happen after at least 50ms passed since wake-up. The glitch is a little different that way, its wake up after 1,5ms of signals and is up for 1,75ms. Then back to sleep and back up etc.
if ((millis() - warten) > 50) { //dont sleep while the data comes in
[blabla other code]
digitalWrite(9, HIGH);
attachInterrupt(0, interrupt, LOW); //sleep after printing it and/or the buffer of 10 is not full yet
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
detachInterrupt(0);
digitalWrite(9, LOW);
warten = millis();
}
152: Still working, no bug
153: zoomed in on the working part
155: glitched part
154: zoomed in on the glitched part