Hi everyone!
I'm a newbie with Arduino, the first time I'm trying to program something...
What I am trying to do, is to create a data logger that can record the values received by some sensors. I’d like to connect two linear potentiometers and a soil potential sensor to arduino. I am using Arduino UNO, to which I have connected a logging and RTC shield. The sensors seem to be working, but I can’t get the soil sensor values on the SD card. Opening the serial monitor, I see a first line where the values of the ground sensor (sensor number, potential and temperature) are correctly read. For example, what appears is "0-3.5+20.1". From the next lines, they appear similar to this "5/2/2023,17:25:5, ,0.0000000000,14.5161294937", which should be: date, time, value of the soil sensor (which does not appear), value of the potentiometer 1 (correct), value of the potentiometer 2 (correct). Practically, I see the value of the soil sensor only at the beginning, and it is not written correctly with others. I have tried in various ways to edit my sketch... Can anyone suggest me where I am going wrong?
I paste here my sketch:
#include <Wire.h>
#include "RTClib.h"
#include <SDI12.h>
#include <SPI.h>
#include <SD.h>
#define SERIAL_BAUD 9600
#define DATA_PIN 2
#define POWER_PIN 7
RTC_DS1307 RTC;
SDI12 mySDI12(DATA_PIN);
String myCommand = "0R0!";
const int chipSelect = 10;
void setup() {
pinMode(7, OUTPUT);
Serial.begin(SERIAL_BAUD);
Wire.begin();
RTC.begin();
mySDI12.begin();
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running");}
DateTime now = RTC.now();
DateTime compiled = DateTime(__DATE__, __TIME__);
if (now.unixtime() < compiled.unixtime()) {
Serial.println("RTC is older than compile time! Updating");
RTC.adjust(DateTime(__DATE__, __TIME__));}
Serial.println("Setup complete.");
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
}
Serial.println("card initialized.");
if (POWER_PIN > 0) {
Serial.println("Powering up sensors...");
pinMode(POWER_PIN, OUTPUT);
digitalWrite(POWER_PIN, HIGH);
delay(200);
}
}
void loop() {
// Get the current time
DateTime now = RTC.now();
mySDI12.sendCommand(myCommand);
delay(300); // wait a while for a response
while (mySDI12.available())
{ // write the response to the screen
Serial.write(mySDI12.read());
}
int val1;
int val2;
digitalWrite(7, HIGH);
val1=analogRead(0);
float length1 = val1 * (25/1023.0);
val2=analogRead(1);
float length2 = val2 * (25/1023.0);
Serial.print(now.day(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.year(), DEC);
Serial.print(',');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print(',');
Serial.write(mySDI12.read());
Serial.print(',');
Serial.print(length1*1,DEC);
Serial.print(',');
Serial.println(length2*1,DEC);
digitalWrite(7, LOW);
delay(10000);
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
// store data every 15 minute
if (now.minute()== 0 || now.minute()== 15 || now.minute()== 30 || now.minute()== 45)
{
dataFile.print(now.day(), DEC);
dataFile.print('/');
dataFile.print(now.month(), DEC);
dataFile.print('/');
dataFile.print(now.year(), DEC);
dataFile.print(',');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
dataFile.print(now.second(), DEC);
dataFile.print(',');
dataFile.print(mySDI12.read());
dataFile.print(',');
dataFile.print(length1,DEC);
dataFile.print(',');
dataFile.println(length2,DEC);
dataFile.close();
}
else {
dataFile.close();
}
}
else {
Serial.println("error opening datalog.txt");
}
}
The "power" wires are on the digital pin 7, the digital communication of the soil sensor is on the digital pin 2, the analog data from linear potentiometers are on analog pin A0 and A1, the ground of all sensors on GND.
The manual for the soil sensor is at this link [http://library.metergroup.com/Manuals/20854_TEROS21_Gen2_Manual_Web.pdf]
(http://library.metergroup.com/Manuals/20854_TEROS21_Gen2_Manual_Web.pdf)
Thank you in advance for any help!