Thanks for the reply. Here is most of the code, with some omitted so it fits in the message size limit:
#include <SD.h>
#include <SPI.h>
#include <Wire.h>
#include "RTClib.h"
#include <DHT22.h>
#include <stdlib.h>
RTC_DS1307 RTC;
File logfile; //declare the name of the logging file
//char current_file[]="UPODXX/MMDDYYYY.txt";
char current_file[]="XXMMDDYY.txt"; //make sure to check first_date value
char model[]="UPOD00"; //UPOD model indicator
void setup()
{
//attachInterrupt(1,calibrate,RISING); //signal interrupt for calibration button
pinMode(CS_Uno, OUTPUT);
pinMode(CS_ADC_1, OUTPUT);
digitalWrite(CS_ADC_1,HIGH); //deselect ADC initially
pinMode(CS_ADC_2, OUTPUT);
digitalWrite(CS_ADC_2,HIGH); //deselect ADC initially
pinMode(red_led,OUTPUT);
pinMode(green_led,OUTPUT);
pinMode(flag_led,OUTPUT);
Serial.begin(9600);
Wire.begin();
RTC.begin();
SPI.begin();
SPI.setDataMode(0);
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV16);
//delay(200); //allow initialization
if (RESET_LOCAL_TIME)
{
RTC.adjust(DateTime(__DATE__, __TIME__));
}
if (! SD.begin(CS_Uno))
{
if(ECHO_DEBUG)
{
Serial.println("*Card initialization failed*");
}
//while(1) //lock into loop with red/green sync flashing at 1Hz for 10 sec, then reboot system
for(int i=1;i<=10;i++)
{
digitalWrite(green_led, HIGH);
digitalWrite(red_led,HIGH);
delay(500);
digitalWrite(green_led,LOW);
digitalWrite(red_led,LOW);
delay(500);
}
soft_reset();
}
if (! RTC.isrunning())
{
if(ECHO_DEBUG)
{
Serial.println("*RTC is not working*");
}
while(1) //Flash red/green in alternate at 1Hz if RTC not working
{
digitalWrite(green_led,HIGH);
digitalWrite(red_led,LOW);
delay(500);
digitalWrite(green_led,LOW);
digitalWrite(red_led,HIGH);
delay(500);
}
}
DateTime now=RTC.now();
if(ECHO_DEBUG)
{
Serial.println();
Serial.print("Current File: ");
Serial.println(current_file);
}
if(! SD.exists(current_file))
{
logfile=SD.open(current_file, FILE_WRITE);
char Header[]= "Analog2 Analog3 Analog4 Analog5";
logfile.println(Header);
logfile.flush();
}
//if(SD.exists(current_file))
else
{
logfile=SD.open(current_file, FILE_WRITE);
}
if(!logfile)
{
if(ECHO_DEBUG)
{
Serial.println("*File cannot be found or opened on card*");
}
//while(1) //lock into loop with red/green sync flashing at 1Hz
for(int i=1;i<=10;i++)
{
digitalWrite(green_led, HIGH);
digitalWrite(red_led,HIGH);
delay(500);
digitalWrite(green_led,LOW);
digitalWrite(red_led,LOW);
delay(500);
}
soft_reset();
}
}
void loop()
{
DateTime now=RTC.now();
if(ECHO_DEBUG)
{
Serial.println("Sampling Sensors...");
}
digitalWrite(green_led,HIGH); //flash green light when sampling
delay(50);
digitalWrite(green_led,LOW);
//Sample sensors and write to buffer
ADCcall(ADC1, CS_ADC_1);
ADCcall(ADC2,CS_ADC_2);
myRHT03.readData(); //read the RHT03 T and Rh
//add values to space delimited data line
logfile.print(model); //model name
logfile.print(" ");
logfile.print((int)now.year()); //current year
logfile.print("/");
logfile.print((int)now.month()); //current month
logfile.print("/");
logfile.print((int)now.day()); //current day
logfile.print(" ");
logfile.print((int)now.hour()); //curent hr
logfile.print(":");
logfile.print((int)now.minute()); //current minute
logfile.print(":");
logfile.print((int)now.second()); //current second
logfile.print(" ");
logfile.print(now.unixtime()); //current unixtime; time since midnight 1/1/1970 (seconds)
logfile.print(" ");
logfile.print(ADC2[1]); //BAT voltage
logfile.print(" ");
logfile.print(analogRead(A1)); //Baseline VOC sensor
logfile.print(" ");
logfile.print(analogRead(A0)); //CO2 sensor
logfile.print(" ");
logfile.print(analogRead(A2)); //CO electrolytic sensor
logfile.print(" ");
dtostrf(myRHT03.getTemperatureC(),6,2,RHT03_buffer); //Temp internal
logfile.print(RHT03_buffer);
logfile.print(" ");
dtostrf(myRHT03.getHumidity(),6,2,RHT03_buffer); //Rh internal
logfile.print(RHT03_buffer);
logfile.println();
flush_crit++; //index counter up for flus criteria
if(flush_crit>=cycles_b4_flush)
{
digitalWrite(red_led,HIGH);
logfile.flush();
flush_crit=0;
delay(200);
digitalWrite(red_led,LOW);
if(ECHO_DEBUG)
{
Serial.println("Flushing SD card buffer");
Serial.println("Closing and reopening file");
Serial.println();
}
logfile.close(); //close and reopen logfile to check SD card presence
current_file[(first_date+2)]=now.day()/10+'0';
current_file[(first_date+3)]=now.day()%10+'0'; //edit current file
if(! SD.exists(current_file))
{
/* logfile=SD.open(current_file, FILE_WRITE);
char Header[]="Model YYYY/MM/DD Hour Minute Second UnixTime "
"Op_voltage Baseline CO2 CO Fig1 Fig2 e2v_O3 e2v_NO2 e2v_1 "
"e2v_2 e2v_3 e2v_4 Temp_outside Temp_inside Rh Light Analog1 Analog2 Analog3 Analog4 Analog5";
logfile.println(Header);
logfile.flush(); */
soft_reset();
}
//if(SD.exists(current_file))
else
{
logfile=SD.open(current_file, FILE_WRITE);
}
if(!logfile)
{
soft_reset();
}
} //end 'flush' loop
//if(calibrate_flag) //enter calibration procedure if calibration button is hit; note: 'calibrate_flag' set with signal interrupt
// {
/*for(int i=1; i<=int(CALIBRATION_DURATION*60000./500.); i++)
{
digitalWrite(red_led,LOW);
digitalWrite(green_led,LOW);
digitalWrite(flag_led,HIGH);
delay(150);
digitalWrite(HIGH,LOW);
digitalWrite(green_led,LOW);
digitalWrite(flag_led,LOW);
delay(150);
digitalWrite(red_led,LOW);
digitalWrite(green_led,HIGH);
digitalWrite(flag_led,LOW);
delay(150);
}*/
// }
delay(1000/SAMPLE_RATE);
}
void ADCcall(int Figaro[], int pin)
{
const byte ADC_ch[8]={B1000,B1001,B1010,B1011,B1100,B1101,B1110,B1111}; //channel designations
const byte start_byte= B00000001; //initial byte to send to start ADC transmission
digitalWrite(pin,LOW);
delay(10);
digitalWrite(pin,HIGH); //cycle ADC to reset if it was in LOW initially
delay(10);
for (int i=0; i<8; i++)
{
digitalWrite(pin,LOW); //activate ADC chip
byte channel_byte=ADC_ch[i]<<4; //determine channel to read from
SPI.transfer(start_byte);
byte byte1=SPI.transfer(channel_byte);
byte byte2=SPI.transfer(B00000000);
Figaro[i]= int(((byte1 & B00000011)<<8 | byte2) ); //mask first 6 bits of 'byte1' and concatenate w/ 'byte2'
digitalWrite(pin,HIGH); //deselect ADC
}
}
void soft_reset() //software reset; bring script to beggining of reset loop; internal watchdog on atmega
{ //http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1273155873/all
asm volatile (" jmp 0");
}
/*void calibrate()
{
calibrate_flag=true;
Serial.println("CALIBRATE");
}
*/
I'll have to look in to the different types of memory, and how different parts of the sketch are partitioned/allocated/using different types of memory. Intuitively, wouldn't the if() and other logical loops be compiled and stored in the machine code and not RAM? It doesn't make sense to me that adding an extra if() loop to my code would use up all the RAM.
The if(calibrate_flag) {} loop is the one that causes the system to stop working. Even with the inside of the {} commented out, it still messes up the operations.
The code will also stop working if I attach a signal interrupt to one of the pins; best I know this is part of the register?
Thanks,
-Nick