#include // -------------------- Pins -------------------- const int trigPin = 6; const int echoPin = 5; const int waterLevelSensorPin = A0; const int waterSensorPower = 3; const int switchPin = 8; const int motorPin1 = A1; const int motorPin2 = A2; const int enaPin = 5; // -------------------- Variables -------------------- float duration; float distance; int SyncWord = 0x26; unsigned long previousMillis = 0; const long interval = 10000; int sensorValue = 0; float waterLevelCM = 0.0; float WaterLevel = 0.0; // from sonar when used // Calibration / geometry constants — you must measure for your setup const int sensorMin = 0; // analog reading when sensor is “dry” (adjust) const int sensorMax = 600; // analog reading when sensor is “fully immersed” (adjust) const float probeLength = 4.0; // “length” in cm that sensor maps over const float sensorToBottom = 30; // distance from ultrasonic sensor to bottom (cm), adjust void setup() { Serial.begin(9600); while (!Serial); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(waterSensorPower, OUTPUT); digitalWrite(waterSensorPower, LOW); pinMode(switchPin, INPUT_PULLUP); Serial.println("LoRa Sender"); if (!LoRa.begin(433E6)) { Serial.println("Starting LoRa failed!"); while (1); } LoRa.setSpreadingFactor(12); LoRa.setSignalBandwidth(62.5E3); LoRa.setCodingRate4(8); LoRa.setSyncWord(SyncWord); } void loop() { unsigned long currentMillis = millis(); // 1) Read the water sensor (analog) — power it just for the reading if (sensorValue <= 400) { digitalWrite(waterSensorPower, HIGH); delay(3000); // brief stabilization sensorValue = analogRead(waterLevelSensorPin); Serial.println(sensorValue); } waterLevelCM = (float)(sensorValue - sensorMin) * probeLength / (sensorMax - sensorMin); // Map analog value to “cm” scale if (waterLevelCM >= 3) { // Trigger ultrasonic digitalWrite(trigPin, LOW); delayMicroseconds(5); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH, 30); // timeout of 30 ms distance = duration * 0.034 / 2.0; WaterLevel = sensorToBottom - distance; Serial.println(distance); // 3) Send via LoRa at intervals if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; LoRa.beginPacket(); if (sensorValue >= 3) { LoRa.print("11,"); LoRa.print(WaterLevel); } if (WaterLevel >= 15){ LoRa.print("22"); } if (WaterLevelCM >= 0.50) { LoRa.print(waterLevelCM); } LoRa.endPacket(); // (Optional) also print to Serial for debugging } }