if i want add a relay to make a pump work at 50% and turn off at 100%
i just need add this code ?! or must change something else
this is my code
#include <LiquidCrystal.h>
#define ECHOPIN 3 // Pin to receive echo pulse
#define TRIGPIN 4 // Pin to send trigger pulse
#define STATUSPIN 13 // Use for troubleshooting
int highWater = 20; // These values allow to calculate % of full
int lowWater = 100; // SRF04 hangs above water (lower distance = more water)
byte symbol[8] = { // Custom character for LCD display
B00000,
B11111,
B11111,
B11111,
B11111,
B11111,
B00000,
};
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // Assign pins
// Utility function for flashing STATUSPIN
void flashLed(int pin, int times, int wait) {
for (int i = 0; i < times; i++) {
digitalWrite(pin, HIGH);
delay(wait);
digitalWrite(pin, LOW);
if (i + 1 < times) {
delay(wait);
}
}
}
void setup() {
lcd.begin(16,2);
lcd.print(" AQUA LEVEL ");
lcd.createChar(0, symbol);
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
delay(3000); // Show application name for 3 seconds.
}
void loop() {
// Measure distance
digitalWrite(TRIGPIN, LOW); // Set the trigger pin to low for 2uS
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH); // Send a 10uS high to trigger ranging
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW); // Send pin low again
int distance = pulseIn(ECHOPIN, HIGH); // Read in times pulse
distance= distance/58; // divide by 58 gives cm.
// Convert measured value to value between 0-16, to display on LCD
// Use Arduino built-in map and constrain functions
int scaledValue = map(constrain(distance, highWater, lowWater), lowWater, highWater, 0, 11);
lcd.clear();
lcd.print("E | F");
lcd.setCursor(0,1);
while (scaledValue > 0) {
lcd.print((char)0);
scaledValue--;
}
delay(2000); // Wait 2 seconds before measuring again. We're in no hurry!
}
and this is the code of relay
if (distance >= 18) {
digitalWrite(relayPin, HIGH); // assumes HIGH switches the relay on
}
if distance <= 5) {
digitalWrite(relayPin, LOW);
}
and if that will work where i will add it in the first code ?!