Hello to everyone!
I am not a deep programmer, i am not interstead to go deeper in my age.
I have mutiple same type soil sensors each one interfacing in diffrent pins.
I want to avoid writing duplicate codes for the same reading function. How can i change the following code and make it smaller and simpliest and read data every 6 hours using rtc withour overflow problems? CAN SOMEONE HELP ME ?
Any help it will appreciate
#include <SoftwareSerial.h>
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int sensorPin = A0; // select the input pin for the Soil moisture sensor
int sensorValue = A0; // variable to store the value coming from the sensor
int sensor1Pin = A1; // select the input pin for the Soil moisture sensor
int sensor1Value = A1; // variable to store the value coming from the sensor
int sensor2Pin = A2; // select the input pin for the Soil moisture sensor
int sensor2Value = A2; // variable to store the value coming from the sensor
int sensor3Pin = A3; // select the input pin for the Soil moisture sensor
int sensor3Value = A3; // variable to store the value coming from the sensor
int sensor4Pin = A4; // select the input pin for the Soil moisture sensor
int sensor4Value = A4; // variable to store the value coming from the sensor
void setup() {
Serial.begin(9600);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
rtc.adjust(DateTime(2018, 11, 11, 04, 45, 0)); // <----------------------SET TIME AND DATE: YYYY,MM,DD,HH,MM,SS
}
delay(100);
}
void loop() {
{
DateTime now = rtc.now();
Serial.print(now.day(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.year(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
delay(3000); //Print date and time every 3 sec
}
sensorValue = analogRead(A0); // read the value from the sensor1:
delay(1000);
Serial.print("sensor = " );
Serial.println(sensorValue);
delay(20000);
sensor1Value = analogRead(A1); // read the value from the sensor2
delay(1000);
Serial.print("senso1r = " );
Serial.println(sensor1Value);
delay(20000);
sensor2Value = analogRead(A2); // read the value from the sensor1:
delay(1000);
Serial.print("sensor2 = " );
Serial.println(sensor2Value);
delay(20000);
sensor3Value = analogRead(A3); // read the value from the sensor1:
delay(1000);
Serial.print("sensor3 = " );
Serial.println(sensor3Value);
delay(20000);
sensor4Value = analogRead(A4); // read the value from the sensor1:
delay(1000);
Serial.print("sensor4 = " );
Serial.println(sensor4Value);
}