So, I've been working on a datalogging scripts, and so far have had some fun with coding on the arduino. Good, healthy fun, usually, but now I've run in to a few snags.
Snag no. 1:
The code seems to just... stop. Just after running for about 10 seconds. Oddly; the actual number seems consistent between resets/restarts, but whenever I change a bit of code later on (for example; change the contents of a string), it changes.
Snag no. 2:
When I try to store some values in a 2d array, nothing happens. Really, nothing; the added screen (which I use for debugging right now) blinks, but doesn't show anything after that. Which is odd, since the code adding something to the array is in loop(), and there's a bit of the initialization feedback in setup(), which doesn't get shown.
I've also got an oddity; whenever I change a counter ('datapoint') to int instead of byte, I get an error on the SD card. Which works fine otherwise.
My code:
#include <U8glib.h> // screen
#include <DS1302.h> // time-keeping thingy
#include <SPI.h> // spi
#include <SD.h> // sd
U8GLIB_PCD8544 u8g(4, 3, 1, 2, 0); //init screen
DS1302 rtc(7, 6, 5); // Init time thingy
const int sd_select = 10; // sd card slave pin
Sd2Card card;
SdVolume volume;
SdFile root;
File datalog;
const int datalimit = 60;
byte datapoint = 0; //if this is an int, logstart() shows !datalog to be true :S
const int samplefreq = 1000;
String fb[5];
unsigned long times[datalimit];
int data[3][datalimit];
void setup() {
screeninitialize();
sdinitialize();
timeinitialize();
draw();
delay(2500);
logstart();
}
void loop() {
long unsigned t = millis()+samplefreq;
fb[0] = rtc.getDateStr();
fb[1] = rtc.getTimeStr();
fb[2] = "MILLIS() = "+String(millis());
fb[3] = "DP = "+String(datapoint);
delay(10);
draw(); // show data
//data[1][datapoint] = datapoint; // if this is uncommented, nothing happens. Really, just a blank screen.
datapoint++;
if (datapoint >= datalimit){
logwrite(); // if array is full, write data off
}
if (millis()<t | millis()<t-(samplefreq*2) | millis()>t+(samplefreq*2)){ // if we're still in our timeframe...
delay(t-millis()); // wait until we're out (note the overflow protection)
} // if we're already out our window, we quickly go to the next phase
}
void draw(){
u8g.firstPage();
do {
for (int x = 0;x<6;x++){
u8g.setPrintPos(0, ((x+1)*9));
u8g.print(fb[x]);
}
}
while( u8g.nextPage() );
for (int x = 0;x<6;x++){fb[x] = "";};
}
void screeninitialize(){
u8g.setRot180();
u8g.setColorIndex(1);
u8g.setFont(u8g_font_helvR08);
fb[0] = "Screen located";
}
void sdinitialize(){
pinMode(sd_select, OUTPUT);
if (!SD.begin()) {
fb[1] = "ERROR WITH SD CARD";
draw(); //show error
while (1){} //stay there
}
fb[1] = "SD found";
}
void timeinitialize(){
fb[2] = rtc.getTimeStr();
fb[3] = rtc.getDateStr();
}
void logstart(){
datalog = SD.open("datalog.txt", FILE_WRITE);// create file, using start date and increment
if (!datalog){fb[1] = "SD ERROR";fb[2] = "LOGSTART";draw();while (1){}} //give error, stay there
String tempstr = "Start of measurement: ";
tempstr += rtc.getDOWStr();
tempstr += ", ";
tempstr += rtc.getDateStr();
tempstr += ", ";
tempstr += rtc.getTimeStr();
datalog.println(tempstr);
datalog.close();// close file
}
void logwrite(){
datalog = SD.open("datalog.txt", FILE_WRITE); // (re-)open file
if (!datalog){fb[1] = "SD ERROR";fb[2] = "LOGWRITE";draw();while (1){}} //give error, stay there
while (datapoint+1<0){
datalog.print(String(times[datapoint]));
datapoint--;
}
datalog.close();
}
Due to the weirdness of the errors, I assume they share some common origin, but as of yet have had no luck in finding it. Can anyone spot one (or more) glaring issues?