Need help for button/relay project.

Hey guys, need some guidence for one project. I need to operate three relays in three different ways when use two buttons as:
State 1: no pressed button
State 2: when press button 1
State 3: when press button 2
Is that possible if i want to use same relay in every state? :o

For now i'm trying with only one button:

const int buttonPin = 2;
int in1 = 3;    
int in2 = 4;   
int buttonState = 0;        

void setup() {

  pinMode(in1, OUTPUT);
  digitalWrite(in1, HIGH);
  pinMode(in2, OUTPUT);
  digitalWrite(in2, HIGH);
  pinMode(buttonPin, INPUT);
}

void loop() {

 buttonState = digitalRead(buttonPin);

  if (buttonState == LOW) 
   {digitalWrite(in1, HIGH);
    digitalWrite(in2, HIGH);
      }
 delay(1000);
   {digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
      }
   }

Unfortunately when i press the button, relays power up but won't power down after a seccond :frowning:

Your indentation and code formatting suck...

But what are these braces doin here?

  delay(1000);
   {digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
      }

It’s like you intended these to be part of an “else” clause for the preceding “if” but decided to replace “else” with “delay(1000)”.

Now you code does the delay and LOW writes to the in1 and in2 pins every time through loop.

And please don’t call output pins “in”. First “in” and “out” aren’t at all meaningful as to what the pin does, besides the fact that an output pin has no business being called in!

Take a look at how to use millis() for timing.

Perhaps this link may help https://forum.arduino.cc/index.php?topic=223286.0, a thing do about several things at the same time.

PCBBC

Now you code does the delay and LOW writes to the in1 and in2 pins every time through loop.

Yeah no, my outputs in1 and in 2 (i dont know why the hell i call them this way in first place) getting LOW as in my realy board means “ON” and not get HIGH after second, instead they just stay at LOW state for the time i pressing the button. And yes, i dont need “else”, i just need to ignite counter when button is pressed and hold and end counter when release it. Thanks anyway :slight_smile:

Idahowalker, thanks toy too.

Try

pinMode(buttonPin, INPUT_PULLUP);

mittko:
Thanks anyway :slight_smile:

I wasn’t complaining about which way you were setting the logic.
I’m telling you that with buttonState HIGH you are energising and deenergising the relays Each time though loop.
Now possibly the LOW state isn’t active for long enough to effect anything as the field from the relay coil won’t collapse immediately. But I t’s still “wrong” in my book.

Hey guys,

I had no time for testing but acheive my goal by using millis again. In this scenario void loop cycle takes zero seconds with no pressed button and in this case it works immediately when I press either of two buttons. Thanks again to all of you for the quick answers and advices. I'm going to take a cours of C/C++/C# as soon as i can, maybe after that will have the ground to built my projects easely :slight_smile: Take care!

Here's my code:

const int buttonPin1 = 2; //NO contact 1
const int buttonPin2 = 3; //NO contact 2
int out1 = 4;  //relay 1  
int out2 = 5;  //relay 2
int out3 = 6;  //relay 3
int buttonState1 = 0; //Turns relay 2 for 20 seconds in every 2.5 minutes 
int buttonState2 = 0; //Made around one hour cycle with all relays 

void setup() {

  pinMode(out1, OUTPUT);
  digitalWrite(out1, HIGH);
  pinMode(out2, OUTPUT);
  digitalWrite(out2, HIGH);
  pinMode(out3, OUTPUT);
  digitalWrite(out3, LOW);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);

}

void loop() {

 buttonState1 = digitalRead(buttonPin1);
 buttonState2 = digitalRead(buttonPin2);

if (buttonState2 == HIGH) {
    digitalWrite(out3, HIGH);   
    digitalWrite(out2, LOW);    
    delay(20000) ;              
    digitalWrite(out2, HIGH);   
    delay(150000) ;             
    digitalWrite(out2, LOW);    
    delay(20000) ;              
    digitalWrite(out2, HIGH);   
    delay(150000) ;             
    digitalWrite(out2, LOW);    
    delay(20000) ;              
    digitalWrite(out2, HIGH);   
    delay(150000) ;             
    digitalWrite(out2, LOW);    
    delay(20000) ;              
    digitalWrite(out2, HIGH);   
    delay(150000) ;             
    digitalWrite(out3, LOW);    
    digitalWrite(out1, LOW);    
    delay(600000) ;             
    digitalWrite(out1, HIGH);   
    delay(2400000) ;            
} else {     
    digitalWrite(out3, LOW);  
    digitalWrite(out2, HIGH); 
    digitalWrite(out1, HIGH); 
    }
if (buttonState1 == HIGH) {
    digitalWrite(out2, LOW);    
    delay(20000);               
    digitalWrite(out2, HIGH);   
    delay(150000);              
} else {
    digitalWrite(out2, HIGH); 
    }
 }

Looking at that code above I am dumbfounded. That is probably the worst code I have seen for some time