I want to ask the reader, do you think all the options listed below are correct or is there something missing?
Please fix. If something goes wrong
Again, matching the first option (IF), how does the sensor multitask when the output is triggered? So how come the second choice (IF) doesn't wait until the first choice (IF) is satisfied?
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <SimpleTimer.h>
#define BLYNK_PRINT Serial
#define BLYNK_PRINT Serial
#define DHTPIN 4
#define DHTTYPE DHT22
char auth[] = "wb-nvghMkdLXO8xA_QjxuGLKW8W7gH6X";
char ssid[] = "realme C2";
char pass[] = "";
float temp;
float hum;
int cahaya;
int ldr = 35;
int soil1 = 33;
int soil2 = 32;
int kelembaban1;
int kelembaban2;
int relay = 25;
int relay2 = 26;
int t = 1000;
DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;
WidgetLCD lcd(V1);
BLYNK_READ (V2);
BLYNK_READ (V3);
void setup() {
Blynk.begin(auth, ssid, pass);
dht.begin();
pinMode(ldr, INPUT);
pinMode(relay, OUTPUT);
pinMode(relay2, OUTPUT);
digitalWrite(relay, LOW);
digitalWrite(relay2, LOW);
Serial.begin(115200);
WiFi.begin(ssid , pass);
Serial.print("menyambungkan");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("TERSAMBUNG :)");
Serial.print(WiFi.localIP());
}
void loop() {
// DHT sensor
hum = dht.readHumidity();
temp = dht.readTemperature();
//LDR sensor
cahaya = analogRead(ldr);
//sensor kelembaban tanah
kelembaban1 = analogRead(soil1);
kelembaban2 = analogRead(soil2);
//menampilkan ke serial monitor
Serial.print("Kelembaban tanah 1 =");
Serial.println(kelembaban1);
Serial.print("Kelembaban tanah 2 =");
Serial.println(kelembaban2);
Serial.print("suhu : ");
Serial.println(temp);
Serial.print("Nilai LDR :");
Serial.println(cahaya);
Blynk.run ();
Blynk.virtualWrite(V2, cahaya);
Blynk.virtualWrite(V3, temp);
lcd.print(0, 0, temp );
if (kelembaban1 >= 3500) {
while(kelembaban1 >= 3500){
lcd.clear();
lcd.print(0, 0, "Tanah 1 kering, air mengalir" );
digitalWrite(relay, HIGH);
}
while(kelembaban2 >= 3500) {
lcd.clear();
lcd.print(0, 1, "Tanah 2 kering, air mengalir");
digitalWrite(relay2, HIGH);
}
}
else {
lcd.clear();
lcd.print(0, 0, "Tanah 1 basah, air berhenti" );
lcd.print(0, 1, "Tanah 2 basah, air berhenti");
digitalWrite(relay, LOW);
digitalWrite(relay2, LOW);
}
Blynk.run();
timer.run();
delay(t);
}
The delay statement blocks all code from running... so most of the time you will miss the readings you expect to get. If things only change slowly (light, humidity, temperature) then that may be OK.
The while statement can also cause code to block ... if the while condition remains true then they will stay in the loop... and will be stuck there... again, nothing else can happen outside the loop.
so how do i get the first (if) to get a trigger, and run the output..., then how do i make the second (if) run concurrently without waiting for the first (if) statement to finish
A while statement starts a loop. Any code in between {} after while is executed until the condition of your while loop is False.
If it is true at the start if your loop, it will always be true (since the condition does not change within your loop...). So it will loop forever.
So you need ( inside each while loop) something that changes the variable value in the condition.
Something like:
kelembaban1 = analogRead(somePin) * 4;
Maybe you really mean to use an 'if' instead of a 'while'.
You do not understand what everyone is telling you.
If kelembana1 is greater than or equal to 3500 you will enter this while loop.
You will never leave the loop, because there is nothing in the loop that changes kelembana1.
so what function should I use so that when sensor 1 triggers the selection and sensor 2 is also the same simultaneously, without having to wait for sensor 1 to stop giving a trigger??
if anyone can solve me close this case