relay code !!

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 ?!

I'm not being funny, but where do you think it should go?

lol i don't know :stuck_out_tongue: you tell me

I'm not asking you to take the risk of spending 1000000000 dollars on something that might not work and blow up the moon.... I'm asking you to think.

for me it cost alot there is no ebay here and i must wait for 1 month to buy these things again for that i am really careful with these things -_- !!

So test it with an led instead of relay.

Now you have the same question going in two related but different threads.... :stuck_out_tongue_closed_eyes:

loooool
i am in my lab from 4 days , i really not see the sun from long time =(
my brain really want explode, and to be honest my brain stop right now
so this is the last thing i must do it before seeing the light :astonished:
i want finish it and sleep for a week....

this is my relay
so i think i dont need any transestor or diod
i connect it with the aruino to a digit port everything ok, but i am wondering about the code, i think i need to call it at first code to make the aruino read it or something !!

make the aruino read it or something

I don't know what you mean: you don't "read" relays.

Put the code to turn it of and on, at the bottom of the code after you get the distance.

oh ok thaaaaaaaaanks alot :slight_smile:

narzan:
this is my relay
so i think i dont need any transestor or diod
i connect it with the aruino to a digit port everything ok, but i am wondering about the code, i think i need to call it at first code to make the aruino read it or something !!
http://pic01.uxsight.com/i/12b/ux_a12062900ux0249_ux_g.jpg

I have asked the moderator to merge this Thread with your other one. I gave you code to operate the relay in that other Thread. I have also commented in third Thread somewhere else - perhaps you can get the moderator to merge that one with these ones.

The relay in that photo seems to have its own transistor so it can probably be connected directly to the Arduino. For the future, it would be much more useful if you post links to the specifications or datasheet for a device rather than a picture.

...R

You have not defined your problem clearly enough for us to help you.

We need the specs of your relay in order to be able to help. A JPEG image is not enough information. What is the model/part number, and where did you get it?

A plain relay needs an external power supply to run the coil, plus a transistor to switch that power, and a flyback protection diode. Without those things it will destroy your Arduino.

Your pictures look like what you have a is a relay board, that might already have those components built in. Without the model number and specifications we can't tell for sure though.

It sounds like you want to turn a pump on based on some distance reading, and like you're using an ultrasonic sensor for the distance measurement. You need to tell us what kind of sensor it is (again model number), and why the distance measurement is used to control a pump.

You said something about 50%, but didn't give enough information for us to understand.

And don't post the same question in multiple threads. It just makes your readers want to ignore you completely.

narzan:
this is my relay
so i think i dont need any transestor or diod
i connect it with the aruino to a digit port everything ok, but i am wondering about the code, i think i need to call it at first code to make the aruino read it or something !!
http://pic01.uxsight.com/i/12b/ux_a12062900ux0249_ux_g.jpg

It sounds like you want to turn a pump on based on some distance reading, and like you're using an ultrasonic sensor for the distance measurement. You need to tell us what kind of sensor it is (again model number), and why the distance measurement is used to control a pump.

He did tell us, but in at least 2 other threads.... :stuck_out_tongue_closed_eyes: