I've encountered a problem that I can't wrap my mind around. I have a sketch that takes some data and a timestamp string, put's it all into one output string and then writes that to a SD card and also to the serial monitor.
When I have this:
card_detect = digitalRead(8);
write_protect = digitalRead(9);
//Serial.println(card_detect);
//Serial.println(write_protect);
if (card_detect == 0 && write_protect == 0) {
Serial.println(str_SDlog);
myFile = SD.open("datalog.csv", FILE_WRITE); if (myFile) { myFile.println(str_SDlog); myFile.close(); } }
//if (card_detect == 1) {Serial.println("Error: SD Card is not present!");}
//if (card_detect == 0 && write_protect == 1) {Serial.println("Error: SD Card is write protected!");}
I get this:
but if I uncomment one or both of the if statements at the bottom:
card_detect = digitalRead(8);
write_protect = digitalRead(9);
//Serial.println(card_detect);
//Serial.println(write_protect);
if (card_detect == 0 && write_protect == 0) {
Serial.println(str_SDlog);
myFile = SD.open("datalog.csv", FILE_WRITE); if (myFile) { myFile.println(str_SDlog); myFile.close(); } }
if (card_detect == 1) {Serial.println("Error: SD Card is not present!");}
if (card_detect == 0 && write_protect == 1) {Serial.println("Error: SD Card is write protected!");}
I get this - No info in the serial monitor nor does and data get saved to the card:
What is going on? Am I missing something?
Here is the entire sketch:
//======================================================================================================================================================================
//======================================================================================================================================================================
//======================================================================================================================================================================
// Libraries and Initializations
#include <SPI.h>
#include <SD.h>
#include "Adafruit_SHT4x.h"
#include "RTClib.h"
Adafruit_SHT4x sht4 = Adafruit_SHT4x();
RTC_PCF8523 rtc;
File myFile;
//======================================================================================================================================================================
//======================================================================================================================================================================
//======================================================================================================================================================================
// Timers
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;
const long interval = 5000;
unsigned long logging_interval = 60000; //ms
// Local Variables
float indoor_temp;
float indoor_humidity;
String str_timestamp;
String str_SDlog;
bool card_detect;
bool write_protect;
//Floats and Strings removed for now. They are still in the 02 version if I need them back. (data received from main station).
float outdoor_temp = -11;
float outdoor_humidity = 40;
//======================================================================================================================================================================
//======================================================================================================================================================================
//======================================================================================================================================================================
void setup() {
// Serial Monitor Initialization
Serial.begin(115200); while (!Serial) delay(10);
// Temperature Sensor Initialization
if (! sht4.begin()) {Serial.println("Couldn't find SHT4x"); while (1) delay(1);}
Serial.print("Found SHT4x sensor! - "); Serial.print("Serial number: "); Serial.print(sht4.readSerial(), DEC);
sht4.setHeater(SHT4X_NO_HEATER); sht4.setPrecision(SHT4X_HIGH_PRECISION); Serial.print(" - High precision,"); Serial.println(" No heater.");
// Real Time Clock Initialization
if (! rtc.begin()) {Serial.println("Couldn't find RTC"); Serial.flush(); while (1) delay(10);}
if (! rtc.initialized() || rtc.lostPower()) {Serial.println("RTC is NOT initialized, let's set the time!"); rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); } rtc.start();
// SD Card Initialization
Serial.print("Initializing SD card..."); if (!SD.begin(10)) {Serial.println("initialization failed!");while (1);} Serial.println("initialization done.");
pinMode (8, INPUT_PULLUP);
pinMode (9, INPUT_PULLUP);
}
//======================================================================================================================================================================
//======================================================================================================================================================================
//======================================================================================================================================================================
void loop() {
unsigned long currentMillis3 = millis();
if (currentMillis3 - previousMillis3 >= (10000)) {
previousMillis3 = currentMillis3;
get_data();
get_indoor_temp_and_humidity();
get_timestamp();
str_SDlog = str_timestamp + "," + outdoor_temp + "," + outdoor_humidity + "," + indoor_temp + "," + indoor_humidity;
write_to_card();
}
}
//======================================================================================================================================================================
//======================================================================================================================================================================
//======================================================================================================================================================================
void get_timestamp(){
DateTime now = rtc.now();
String str_YY; String str_MM; String str_MM1; String str_DD; String str_DD1; String str_hh; String str_hh1; String str_mm; String str_mm1; String str_ss; String str_ss1;
int Month; int Day; int Hour; int Minute; int Second;
str_YY = String(now.year(), DEC);
str_MM = String(now.month(), DEC); Month = (now.month()); if (Month < 10)(str_MM1 = "0" + str_MM); else str_MM1 = str_MM;
str_DD = String(now.day(), DEC); Day = (now.day()); if (Day < 10)(str_DD1 = "0" + str_DD); else str_DD1 = str_DD;
str_hh = String(now.hour(), DEC); Hour = (now.hour()); if (Hour < 10)(str_hh1 = "0" + str_hh); else str_hh1 = str_hh;
str_mm = String(now.minute(), DEC); Minute = (now.minute()); if (Minute < 10)(str_mm1 = "0" + str_mm); else str_mm1 = str_mm;
str_ss = String(now.second(), DEC); Second = (now.second()); if (Second < 10)(str_ss1 = "0" + str_ss); else str_ss1 = str_ss;
str_timestamp = str_YY + "-" + str_MM1 + "-" + str_DD1 + " " + str_hh1 + ":" + str_mm1 + ":" + str_ss1; // Form timestamp STRING:
//Serial.println(str_timestamp);
}
//======================================================================================================================================================================
void get_indoor_temp_and_humidity() {
unsigned long currentMillis2 = millis();
if (currentMillis2 - previousMillis2 >= (interval*1)) {
previousMillis2 = currentMillis2;
sensors_event_t humidity, temp; sht4.getEvent(&humidity, &temp); indoor_temp = temp.temperature; indoor_humidity = humidity.relative_humidity;
//Serial.print("Indoor Temperature: "); Serial.print(indoor_temp); Serial.print(" Indoor Humidity: "); Serial.println(indoor_humidity);
}
}
//======================================================================================================================================================================
void get_data() {
unsigned long currentMillis1 = millis();
if (currentMillis1 - previousMillis1 >= (interval*2)) {
previousMillis1 = currentMillis1;
outdoor_temp++; if (outdoor_temp > 10) {outdoor_temp = -10;}
outdoor_humidity++; if (outdoor_humidity > 60) {outdoor_humidity = 40;}
//Serial.print("Outdoor Temperature: "); Serial.print(outdoor_temp); Serial.print(" Outdoor Humidity: "); Serial.println(outdoor_humidity);
}
}
//======================================================================================================================================================================
void write_to_card() {
card_detect = digitalRead(8);
write_protect = digitalRead(9);
//Serial.println(card_detect);
//Serial.println(write_protect);
if (card_detect == 0 && write_protect == 0) {
Serial.println(str_SDlog);
myFile = SD.open("datalog.csv", FILE_WRITE); if (myFile) { myFile.println(str_SDlog); myFile.close(); } }
if (card_detect == 1) {Serial.println("Error: SD Card is not present!");}
if (card_detect == 0 && write_protect == 1) {Serial.println("Error: SD Card is write protected!");}
}
//======================================================================================================================================================================
//======================================================================================================================================================================
//======================================================================================================================================================================

