Hi!
I am a little confused. After working through the Elegoo manual with different sensors and motors and so on I tried something on my own:
I combined a photoresistor, a water sensor, an inclination sensor and a humidity/temperature sensor. These I use with the arduino uno and a SD card with a RTC.
I implemented the sensors sequentially and until I use the blue humidity/temperature sensor everything runs fine. But when using the sensor and the corresponding libraries I get a totally erratic behavior: it jumps from the loop part to the setup part and so on. The output is strange.
The sensor itself runs fine. I tested it separately.
Can this be a problem of the memory. I am at 65%?
Erratic output:
20-08-2/l&À454197,1534791454,721,0,1,26,58
20-08-2/l&À454197,1534791454,758,0,1,26,57
t�,0�����,26,57�97,1534791454,758,0,1,26,57
20-08-2/l&À654197,1534791456,759,0,1,26,58
20-08-2/l&À754197,1534791457,763,0,1,26,57
20-08-2/l&À865197,1534791568,768,0,1,26,57
20-08-2/l&À965197,1534791569,763,0,1,26,56
����������������������ÿÿÿ��������Z���ÿÿÿÿÿÿ20-08-2/l'K637197,1534791736,659,0,1,27,55
20-08-2/l'K837197,1534791738,685,0,1,27,55
Correct output without humidity sensor:
20-08-2018 17:16:48,1534781808,957,0,1
20-08-2018 17:16:49,1534781809,956,0,1
20-08-2018 17:16:51,1534781811,956,0,1
20-08-2018 17:16:52,1534781812,956,0,1
20-08-2018 17:16:53,1534781813,956,0,1
Here is the code.
#include <Wire.h>
#include <DS3231.h>
#include <SPI.h>
#include <SD.h>
#include <SimpleDHT.h>
DS3231 clock;
RTCDateTime dt;
File myfile;
const int chipSelect = 10;
const String filename = "data6.txt";
int pinDHT = 3; //humidity sensor
SimpleDHT11 dht11;
void setup() {
Serial.begin(9600);
clock.begin();
clock.setDateTime(__DATE__, __TIME__);
Serial.println("Initializing SD card");
pinMode(SS, OUTPUT);
pinMode(2, INPUT); //inclination sensor
digitalWrite(2, HIGH);
if (!SD.begin(chipSelect)){
Serial.println("Initialisation failed...");
return;
}
myfile = SD.open(filename, FILE_WRITE);
delay(5000);
if (myfile) {
Serial.println("File opened... and gets closed");
myfile.close();
} else {
Serial.println("error creating "+ filename);
}
}
void loop() {
myfile = SD.open(filename, FILE_WRITE);
String time = "";
byte temperature = 0;
byte humidity = 0;
byte data[40]={0};
dt = clock.getDateTime();
time += clock.dateFormat("d-m-Y H:i:s", dt);
time += "," + String(clock.dateFormat("U", dt));
String dataString="";
for (int analogPin=0;analogPin<2; analogPin++){ // Fotowiderstand, feuchtgkeitsensor
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin<1){
dataString += ",";
}
}
dataString = time + "," + dataString + "," + String(digitalRead(2));
if (dht11.read(pinDHT, &temperature, &humidity, data)){
Serial.println("Read DHT11 failed...");
return;
}
Serial.println((int)temperature, (int) humidity);
dataString += ",";
dataString += String((int)temperature) + "," + String((int) humidity);
Serial.println(dataString); //output to serial and SD card
delay(500);
myfile.println(dataString);
Serial.flush();
delay(1000);
myfile.close();
}