Hello All
I am facing a problem while working on Relay project, I want to operate 2 relay when i press the button. 2 relays having 50msec delay in it. can anybody solve this. I am using this following code. I am using 8 relay module, all relays are working fine, but when they are in the combinations they didn't work.
i want to combinations like this (S=switch R= Relay)
S1 - R1 (50ms delay) R2
S2 - R2 (50ms delay) R3
S3 - R1 (50ms delay) R3
S4 - R5 (50ms delay) R6
S5 - R2 (50ms delay) R4
delay() does exactly what it says. It stops everything until the delay is finished.
That makes button presses and other relays slow/unresponsive.
Learn to code without delay() calls. Use millis) timing instead.
Study the BlinkWithoutDelay sketch, found in the examples of the IDE.
Leo..
Thank You sir. I am changing the code as per requirement it doesn't work, my humble request is will you write code for me that fulfill my requirements.
It does something. What does "it didn't work" mean?
Think what happens when BV1 is LOW? You will activate two relays. Next you test BV2 and it's HIGH so you switch your relays off again.
On top of that, loop() loops so the next time that BV1 is checked it might be HIGH and both relays will be off again. Or it is still LOW and you will activate the first relay again.
What needs to happen? Do the relays have to stay in the last state? Till the button is released again? If so, look at the state change detection example that comes with the IDE; you're not interested in the fact that the button is pressed but when it becomes pressed. Same for the release.
When i hold the button 1,1st relay gets energized and then after 50ms 2nd relay gets energized as soon as i lift off my finger both relay should get de-energized at the same time.
when i press the button 3 and 4, the 3rd relay didn't gets energized but 4th relay gets energized. My power supply is proper in connection. I am using millis function,its not working please anybody correct it.
int Relay1 = 2;
int Relay2 = 3;
const unsigned int interval = 50;
unsigned long previousTime = 0;
int Button1 = 7;
int BV1 = 0;
void setup()
{
pinMode(Relay1,OUTPUT);
pinMode(Relay2,OUTPUT);
pinMode(Button1,INPUT_PULLUP);
}
void loop()
{
unsigned long currentTime = millis();
BV1 = digitalRead(Button1);
if(BV1 == LOW)
{
digitalWrite(Relay1,LOW);
if(currentTime - previousTime >= interval)
{
digitalWrite(Relay2,LOW);
}
previousTime = currentTime;
}
else
{
digitalWrite(Relay1,HIGH);
digitalWrite(Relay2,HIGH);
}
}
Button1 = digitalRead(Button1);
if(Button1 == LOW)
{
BV1 = !BV1 ; // toggles BV1 to the opposite state with each button press
// WARNING, your must release button before the loop re-scans
// delay(100) is required and your button press has to be less than 100mS
}
What is the application that requires relays to operate with less than 50mS precision?
What relays are you using?
How are you measuring the 50mS delays?
here is an example using debounce as the main format.
as you can see the practice of separating the sketch into 3 parts,
gather data / analyze and perform logic / output
have been used.
/* Debounce
Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's a
minimum delay between toggles to debounce the circuit (i.e. to ignore noise).
http://www.arduino.cc/en/Tutorial/Debounce
*/
int Relay1 = 2;
int Relay2 = 3;
const unsigned int interval = 50;
unsigned long previousTime = 0;
// constants won't change. They're used here to set pin numbers:
const int Button1 = 7;
// const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(Button1, INPUT);
pinMode(ledPin, OUTPUT);
// set initial LED state
digitalWrite(ledPin, ledState);
}
unsigned long currentTime ;
// int lastButtonState ;
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(Button1);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
// this only occurs while the button is being pressed
if (buttonState == HIGH) {
ledState = !ledState;
}
}
} // end if debounce
// now the values get passed to the output
// set the LED:
digitalWrite(ledPin, ledState);
if ( ledState == HIGH ) {
// if(BV1 == LOW) from the OP's sketch
{
digitalWrite(Relay1, LOW);
if (currentTime - previousTime >= interval)
{
digitalWrite(Relay2, LOW);
}
previousTime = currentTime;
}
}
else
{
digitalWrite(Relay1, HIGH);
digitalWrite(Relay2, HIGH);
}
// this is housekeeping to give the next loop a reference
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
}
I think this will do what you want. When a button is pressed, turn on a pair of relays 50 milliseconds apart and keep them turned on until that button is released. Note: While a button is pressed, other buttons are ignored.
Hi Tom, my application is to control approx 50 to 60 LEDs (12v) by using relays. I have 8 relay module that connects with Arduino (GND,In1,In2,....In8,Vcc) .My aim is that when i pushed 1st button then 1st relay gets on and after 50 or 100ms 2nd relay gets on.There are multiple combinations such as R1R2,R5R8,R4R5,R6R8,etc
Problem has been solved. Just replacing if() with while() and the code was working fine, Johnwasser helped me. When I was using if() the voltage drop was not occurring in Arduino that operate relays. I tried using mosfets too but it didn't work out, as soon as the the function got replaced mosfets and relays were working good.