So, I'm making a project on my own and I'm somewhat not satisfied of my own program, any suggestions that you can provide to improve my code?
#include <DHT.h>
#include <DHT_U.h>
#include "thingProperties.h"
#define DHTpin 13
#define DHTTYPE DHT11
#define pH_sensor_pin 35
int soil_sensor = 33; // Define the analog pin for the soil moisture sensor
DHT dht(DHTpin, DHTTYPE);
float calibration_value = -107.45;
unsigned long int avgval;
int buffer_arr[10], temp;
void setup() {
Serial.begin(9600);
delay(1500);
initProperties();
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
dht.begin();
}
void loop() {
ArduinoCloud.update();
dht_sensor_getdata();
}
void dht_sensor_getdata() {
// pH sensor reading
pH = getPhValue(); // Update the pH variable for the cloud
Serial.print("pH Value: ");
Serial.println(pH);
delay(1000);
// Soil moisture sensor reading
sm_sensor = analogRead(soil_sensor); // Raw sensor value
// Convert raw value to percentage
delay(500);
// DHT sensor readings
float hm = dht.readHumidity();
Serial.print("Humidity: ");
Serial.println(hm);
float temp = dht.readTemperature();
Serial.print("Temperature: ");
Serial.println(temp);
humidity = hm; // Update the humidity variable for the cloud
temperature = temp; // Update the temperature variable for the cloud
delay(500);
// Implementing the result function
if (pH >= 5.5 && pH <= 6.8 && sm_sensor >= 2200 && sm_sensor <= 2457 && temperature >= 24 && temperature <= 26) {
msg = "Eggplant is most applicable in this soil";
} else if (pH >= 5.5 && pH <= 8.0 && sm_sensor >= 2300 && sm_sensor <= 2457 && temperature >= 23 && temperature <= 25) {
msg = "Tomato is most applicable in this soil";
} else if (pH >= 6.3 && pH <= 6.8 && sm_sensor >= 2900 && sm_sensor <= 3276 && temperature >= 28 && temperature <= 31) {
msg = "Okra is most applicable in this soil";
} else if (pH >= 6.4 && pH <= 6.6 && sm_sensor >= 2000 && sm_sensor <= 2500 && temperature >= 27 && temperature <= 28) {
msg = "Pepper is most applicable in this soil";
} else if (pH >= 6.0 && pH <= 6.7 && sm_sensor >= 2000 && sm_sensor <= 2457 && temperature >= 34 && temperature <= 36) {
msg = "Ampalaya is most applicable in this soil";
} else {
msg = "No specific crop recommendation for these sensor values";
}
Serial.println(msg);
}
float getPhValue() {
for (int i = 0; i < 10; i++) {
buffer_arr[i] = analogRead(pH_sensor_pin); // Reading pH sensor data
delay(30);
}
// Sorting buffer_arr in ascending order
for (int i = 0; i < 9; i++) {
for (int j = i + 1; j < 10; j++) {
if (buffer_arr[i] > buffer_arr[j]) {
temp = buffer_arr[i];
buffer_arr[i] = buffer_arr[j];
buffer_arr[j] = temp;
}
}
}
// Calculating average of middle 6 values
avgval = 0;
for (int i = 2; i < 8; i++)
avgval += buffer_arr[i];
float volt = (float)avgval * 5.0 / 1023 / 6.0; // Corrected conversion
float ph_act = 5.70 * volt + calibration_value; // Corrected linear equation
return ph_act;
}
void onMsgChange() {
// Add your code here to act upon Msg change
}
void onPHChange() {
// Add your code here to act upon PH change
}
void onHumidityChange() {
// Add your code here to act upon Humidity change
}
void onTemperatureChange() {
// Add your code here to act upon Temperature change
}