Hello arduino community!
I have a problem with my arduino. I've recently gotten into programming and found it quite fun but i can figure this one out.
What i want to do is quite simple, i want to store my sensor data to a SD card which it does. The only problem is is that the arduino stops after a couple of loops and i cant figure out why.
To my arduino i have a robodyn moisture sensor, a AM2320 sensor and a TFT screen which i currently use as an SD card reader connected.
Here is my code:
#include <AM2320.h>
#include <SD.h>
#include <SPI.h>
File myFile;
int pinCS = 53;
#define SensorPin A0
float sensorValue = 0;
AM2320 sensor;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
sensor.begin();
Serial.print("Arduino is ready");
pinMode(pinCS, OUTPUT);
if (SD.begin())
{
Serial.println("SD card is ready to use.");
} else
{
Serial.println("SD card initialization failed");
return;
}
}
void loop() {
// put your main code here, to run repeatedly:
if (sensor.measure()) {
Serial.print("temperature:");
Serial.println(sensor.getTemperature());
Serial.print("humidity:");
Serial.print(sensor.getHumidity());
delay(1);
}
else delay(1000);;
for (int i = 0; i <= 100; i++)
{
sensorValue = sensorValue + analogRead(SensorPin);
delay(1);
}
sensorValue = sensorValue/100.0;
Serial.print("Soil Moisture: ");
Serial.println(sensorValue);
myFile = SD.open("test.txt", FILE_WRITE);
if (myFile) {
myFile.print(float(sensor.getTemperature()));
myFile.print(",");
myFile.println(float(sensor.getHumidity()));
myFile.print(",");
myFile.println(float(sensorValue));
myFile.close();
}
// if the file didn't open, print an error:
else {
Serial.println("error opening test.txt");
}
delay(3000);
}