Hello everyone. I am currently working on a small Project with an Arduino Pro Mini 3.3V.
Right now i am trying to add an SD-Card Module to my already existing code, but for some reason the code wont run anymore now. It feels like a storage problem or something because when i remove all my Serial Outputs it starts working partially but the Arduino tells me that he cant find/open the file, which works with the example code from the Arduino IDE without any problems.
The Arduino IDE tells me that i am only using 64 and 57% of storage tho, so i dont know whats going on. Maybe i just made a mistake when implementing the example code for the SD-Card into my own Code.
I know that my code is nowhere near perfect but so far everything worked as intended.
Any help is appreciated. I know for sure that the SD-Card Module works and also my code. It only stops working if I add:
File Textdatei = SD.open("Messdaten.txt", FILE_WRITE); // An dieser Stelle wird die Textdatei erstellt. Unsere Textdatei soll "test" heißen und im Format ".txt" (Text) erstellt werden.
String ausgabe_sd = String(buff) + "," + String(Temperatur) + "," + String(Luftfeuchte) + "\n";
if (Textdatei) {
Textdatei.println(ausgabe_sd);
Textdatei.close();
} else {
Serial.println("Konnte Messdaten.txt nicht öffnen/finden");
}
I have included the needed librarys so thats not the problem.
Heres the code:
/*
//---------------------------BMP180--------------------------
#include <Wire.h>
#include <Adafruit_BMP085.h>
Adafruit_BMP085 bmp;
*/
//---------------------------DHT22---------------------------
#include <DHT.h>
#define DHTPIN 3
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
//--------------------------SD-Card--------------------------
#include <SPI.h>
#include <SD.h>
//---------------------------DS3231--------------------------
#include <ds3231.h>
#include <Wire.h>
#define BUFF_MAX 256
uint8_t sleep_period = 1; // the sleep interval in minutes between 2 consecutive alarms
// how often to refresh the info on stdout (ms)
unsigned long prev = 1000, interval = 1000;
void set_next_alarm(void) {
struct ts t;
unsigned char wakeup_min;
DS3231_get(&t);
// calculate the minute when the next alarm will be triggered
wakeup_min = (t.min / sleep_period + 1) * sleep_period;
if (wakeup_min > 59) {
wakeup_min -= 60;
}
// flags define what calendar component to be checked against the current time in order
// to trigger the alarm
// A2M2 (minutes) (0 to enable, 1 to disable)
// A2M3 (hour) (0 to enable, 1 to disable)
// A2M4 (day) (0 to enable, 1 to disable)
// DY/DT (dayofweek == 1/dayofmonth == 0)
uint8_t flags[4] = { 0, 1, 1, 1 };
// set Alarm2. only the minute is set since we ignore the hour and day component
DS3231_set_a2(wakeup_min, 0, 0, flags);
// activate Alarm2
DS3231_set_creg(DS3231_CONTROL_INTCN | DS3231_CONTROL_A2IE);
}
//---------------------------Sleep---------------------------
#include <avr/sleep.h>
#include <avr/power.h>
int pin2 = 2;
void pin2Interrupt(void) {
/* This will bring us back from sleep. */
/* We detach the interrupt to stop it from
continuously firing while the interrupt pin
is low.
*/
detachInterrupt(0);
}
void enterSleep(void) {
/* Setup pin2 as an interrupt and attach handler. */
attachInterrupt(0, pin2Interrupt, LOW);
delay(100);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_mode();
/* The program will continue from here. */
/* First thing to do is disable sleep. */
sleep_disable();
}
//------------------------Variablen--------------------------
unsigned long previousMillis;
unsigned long currentMillis;
const long interval_2 = 2000;
float Temp_sum = 0;
float Luftfeuchte_sum = 0;
int x = 0;
int done = 0;
float Temperatur = 0;
float Luftfeuchte = 0;
//--------------------------Setup----------------------------
void setup() {
Serial.begin(9600);
while (!Serial);
Wire.begin();
/*
//BMP180
if (!bmp.begin()) {
Serial.println("Sensor nicht gefunden!");
while (1) {}
} else if (bmp.begin()) {
Serial.println("Sensor gefunden!");
}*/
/*
Serial.println(" Starte Initialisierung");
Serial.println("---------------------------------------------");
*/
//DHT22
dht.begin();
/*
//SD-Karte
Serial.println("- Initialisiere SD-Karte");
if (!SD.begin(5)) { // Wenn die SD-Karte nicht (!SD.begin) gefunden werden kann, ...
Serial.println(" - SD-Karten Initialisierung fehlgeschlagen!"); // ... soll eine Fehlermeldung ausgegeben werden. ....
return;
} else {
Serial.println(" - SD-Karten Initialisierung abgeschlossen"); // ... Ansonsten soll die Meldung "Initialisierung abgeschlossen." ausgegeben werden.
}
*/
//DS3231
DS3231_init(DS3231_CONTROL_INTCN);
DS3231_clear_a2f();
set_next_alarm();
//Sleep
pinMode(pin2, INPUT);
/*
Serial.println("---------------------------------------------");
Serial.println(" Initialisierung abgeschlossen");
Serial.println(" Starte Programm");
Serial.println("---------------------------------------------\n");
*/
delay(2000);
}
//--------------------------Main-----------------------------
void loop() {
/*
Serial.print("Temperatur BMP180 = ");
Serial.print(float(bmp.readTemperature()));
Serial.println(" °C");
float luftdruck = (bmp.readPressure() / 100) + 20;
Serial.print("Luftdruck BMP180 = ");
Serial.print(luftdruck);
Serial.println(" hPa");
*/
if (DS3231_triggered_a2()) { //Wenn DS3231 (RTC) Alarm ausgelöst --> zurücksetzen
set_next_alarm();
DS3231_clear_a2f();
}
if (done != 1) { //Solange nicht Messung abgeschlossen
if (x < 5) { //Schleife um 4 Messungen zu machen
currentMillis = millis();
if (currentMillis - previousMillis >= interval_2) { //2 Sekunden Pause zwischen Messung wegen DHT22
Temp_sum = Temp_sum + dht.readTemperature();
Luftfeuchte_sum = Luftfeuchte_sum + dht.readHumidity();
previousMillis = currentMillis;
Serial.println("Test");
x += 1;
}
}
if (x == 4) { //Wenn 4 Messungen durchgeführt --> Temperatur und Luftfeuchte Ausgabewerte berechnen
Temperatur = Temp_sum / 4;
Luftfeuchte = round(Luftfeuchte_sum / 4);
done = 1;
}
} else if (done == 1) { //Wenn Messungen fertig
/*
String ausgabe_temp = "Temperatur DHT22: " + String(Temperatur) + "°C";
Serial.println(ausgabe_temp);
String ausgabe_luftfeuchte = "Luftfeuchtigkeit DHT22: " + String(Luftfeuchte) + "%\n";
Serial.println(ausgabe_luftfeuchte);
*/
char buff[BUFF_MAX];
struct ts t;
DS3231_get(&t); //Zeit messen
snprintf(buff, BUFF_MAX, "%d.%02d.%02d %02d:%02d:%02d", t.year - 1900, t.mon, t.mday, t.hour, t.min, t.sec); //Puffer mit Zeit beschreiben
/*
String ausgabe_sleep = "Going to sleep at: " + String(buff) + "\n";
Serial.println(ausgabe_sleep);
*/
File Textdatei = SD.open("Messdaten.txt", FILE_WRITE); // An dieser Stelle wird die Textdatei erstellt. Unsere Textdatei soll "test" heißen und im Format ".txt" (Text) erstellt werden.
String ausgabe_sd = String(buff) + "," + String(Temperatur) + "," + String(Luftfeuchte) + "\n";
if (Textdatei) {
Textdatei.println(ausgabe_sd);
Textdatei.close();
} else {
Serial.println("Konnte Messdaten.txt nicht öffnen/finden");
}
x = 0;
done = 0;
Temp_sum = 0;
Luftfeuchte_sum = 0;
enterSleep(); //Arduino schlafen schicken (Stromsparen)
}
}