relay codes

i have a problem , i need help from here
i need to switch relay but stays on
but its blinking ,, dont know how to stop those blinking

i have 3 relays
2nd , 3rd relays have to switch on
after 2 sec the 2nd relay turn off & 1st relay1 turn on
finally rly1 & rly3 stay on,,, but 2nd rly blinking
is there anyone to help me,,,???

i need help from here :confused: :confused: :confused:

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR 0x3F // <<----- Add your address here. Find it from I2C Scanner
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
#define trigPin 12
#define echoPin 13
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

int Relay1 = 7;
int Relay2 = 8;
int Relay3 = 9;

void setup() {
Serial.begin(9600);

pinMode(Relay1, OUTPUT);
digitalWrite(Relay1,HIGH);

pinMode(Relay2, OUTPUT);
digitalWrite(Relay2, HIGH);

pinMode(Relay3, OUTPUT);
digitalWrite(Relay3, HIGH);

lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.begin (16,2);

}

void loop() {

float duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) * 0.0344;

if (distance >= 150 || distance <= 2){
lcd.print("Out of range");
Serial.print("Out of range");
lcd.setCursor(0,1);
delay(500);
digitalWrite(Relay2, LOW);
digitalWrite(Relay3, LOW);
delay(1000);
digitalWrite(Relay2, HIGH);
digitalWrite(Relay1, LOW);

}else {
lcd.print("WATER LEVEL");
lcd.setCursor(0,1);
digitalWrite(Relay2, HIGH);
digitalWrite(Relay3, HIGH);
digitalWrite(Relay1, HIGH);

lcd.print(distance);
lcd.print(" cm");
delay(500);
}
delay(500);
lcd.clear();

}

original_copy.ino (1.57 KB)

#define D7_pin  7
LiquidCrystal_I2C  lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);
int Relay1 = 7;

You seem to be trying to use pin 7 for 2 different things at the same time. What is connected to it ?

By the way, naming pin 7 as D7_pin is not exactly helpful, is it ?

pin 7 connected to relay 1 :slight_smile: :slight_smile:
itz conflict ?

pin 7 connected to relay 1

...and to the LCD

Please remember to use code tags when posting code

im sorry for code tags

how to correct the pin for lcd,,,?? tq

Find another pin that isn't being used.

satiya:
im sorry for code tags

how to correct the pin for lcd,,,?? tq

Use a different (currently unused) pin for either the relay or the LCD

yeah thank you...nw i changed

but it still blinking :frowning: :frowning:
how to post with code tags,,,tq

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR 0x3F // <<----- Add your address here.
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
#define trigPin 12
#define echoPin 13
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

int Relay1 = 10;
int Relay2 = 8;
int Relay3 = 9;

void setup() {
Serial.begin(9600);

pinMode(Relay1, OUTPUT);
digitalWrite(Relay1,HIGH);

pinMode(Relay2, OUTPUT);
digitalWrite(Relay2, HIGH);

pinMode(Relay3, OUTPUT);
digitalWrite(Relay3, HIGH);

lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.begin (16,2);

}

void loop() {

float duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) * 0.0344;

if (distance >= 150 || distance <= 2){
lcd.print("Out of range");
Serial.print("Out of range");
lcd.setCursor(0,1);
delay(500);
digitalWrite(Relay2, LOW);
digitalWrite(Relay3, LOW);
delay(1000);
digitalWrite(Relay2, HIGH);
digitalWrite(Relay1, LOW);

}else {
lcd.print("WATER LEVEL");
lcd.setCursor(0,1);
digitalWrite(Relay2, HIGH);
digitalWrite(Relay3, HIGH);
digitalWrite(Relay1, HIGH);

lcd.print(distance);
lcd.print(" cm");
delay(500);
}
delay(500);
lcd.clear();

}

Are you getting "Out of range" prints while blinking?
HowToPostCode

yeah....when " out of range " the relays should pickup,,, but i dont know why the blink ...
mayb loop cycle?

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

  if (distance >= 150 || distance <= 2) {
      lcd.print("Out of range");
      Serial.print("Out of range");
      lcd.setCursor(0,1);
      delay(500);
      digitalWrite(Relay2, LOW);
      digitalWrite(Relay3, LOW);
      delay(1000);
      digitalWrite(Relay2, HIGH);
      digitalWrite(Relay1, LOW);
   }else....

Can you see why - if distance is out of range - that Relay2 will click on and off?
... and curiously, why do you set the cursor position AFTER you print to the LCD ?

but i delete tat code & i let it runs but it still keep the same blinking ,,wont stay on

As asked, post your diagram how everything is connected. Photo/scan of handdrawn one is fine.

And as you're using a I2C lcd, the D7 pin should not conflict.