3 button and 2 Relays

Hallo All
i am new to Arduino
i'm trying to get button1 to use relay1 and button2 to use relay2 and button3 to use both relays. relays is only on when button is presed.

i got code to work with relay 1 and 2 but i need help with button 3.

i started 2 hours ago looking at this forum boy you can learn mutch looking at codes.

Thanks in front for your help.


const int buttonPin_1 = 7;     // the number of the pushbutton pin
const int buttonPin_2 = 6;
const int buttonPin_3 = 5;     // Use for both relays?

const int Light_1 =  2;    // Relay 2 pin
const int Light_2 = 3;     // Relay 3 pin
const int Light_3 = 8;     // Relay 8 pin


// variables will change:
boolean buttonState = LOW;         // variable for reading the pushbutton status
boolean buttonState2 = LOW;        //variable for reading the second buttons state
boolean buttonState3 = LOW;


void setup() {
  // initialize the LED pin as an output:
  pinMode(Light_1, OUTPUT);
  pinMode(Light_2, OUTPUT);
  pinMode(Light_3, OUTPUT);
  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin_1, INPUT);
  pinMode(buttonPin_2, INPUT);
  pinMode(buttonPin_3, INPUT);
}

void loop() {
  
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin_1);
  buttonState2 = digitalRead(buttonPin_2);
  buttonState3 = digitalRead(buttonPin_3);

  
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  // Light 1
  
  if (buttonState == HIGH) {
    // turn relay on:
    digitalWrite(Light_1, HIGH);
  }
  else {
    // turn relay off:
    digitalWrite(Light_1, LOW);
  }


  
  // Light 2
    if (buttonState2 == HIGH) {
    // turn relay 2 on:
    digitalWrite(Light_2, HIGH);
  }
  else {
    // turn relay off:
    digitalWrite(Light_2, LOW);


   // Light 3
     if (buttonState3 == HIGH) {
    // turn relay 3 on:
    digitalWrite(Light_3, HIGH);
  }
  else {
    // turn turn off:
    digitalWrite(Light_3, LOW);
  }


}
}

Welcome to the forum

You seem to have 3 relay outputs but your requirements refer to 2 relays. Which is it ?

On a practical note, what is keeping the input pins LOW when the buttons are not pressed ?

How are the outputs wired to the relays and which Arduino board are you using ?

A suggestion.
Use INPUT_PULLUP in pinMode() for the inputs to turn on the built in pullup resistors to keep the inputs normally HIGH. Wire the buttons to take the inputs to GND when pressed and detect LOW as a button press

1 Like

Button 1 = relay 1
Button 2 = relay 2
Button 3 = relay 1 AND 2

Hello

Welcome to the best Arduino forum ever :slight_smile:

What is the task of the sketch in real life?

Check this note on the interconnection of the buttons and leds to an Arduino.

You have Light_3 stuck inside Light_2 "if" condition.

This...

  else {
    // turn relay off:
    digitalWrite(Light_2, LOW);

Should be this...

  else {
    // turn relay off:
    digitalWrite(Light_2, LOW);
  }

(and then you have an extra close brace at the end of the sketch)... Here is the result..

const int buttonPin_1 = 7;     // the number of the pushbutton pin
const int buttonPin_2 = 6;
const int buttonPin_3 = 5;     // Use for both relays?

const int Light_1 =  2;    // Relay 2 pin
const int Light_2 = 3;     // Relay 3 pin
const int Light_3 = 8;     // Relay 8 pin


// variables will change:
boolean buttonState = LOW;         // variable for reading the pushbutton status
boolean buttonState2 = LOW;        //variable for reading the second buttons state
boolean buttonState3 = LOW;


void setup() {
  // initialize the LED pin as an output:
  pinMode(Light_1, OUTPUT);
  pinMode(Light_2, OUTPUT);
  pinMode(Light_3, OUTPUT);

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin_1, INPUT);
  pinMode(buttonPin_2, INPUT);
  pinMode(buttonPin_3, INPUT);
}

void loop() {

  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin_1);
  buttonState2 = digitalRead(buttonPin_2);
  buttonState3 = digitalRead(buttonPin_3);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:

  // Light 1
  if (buttonState == HIGH) {
    // turn relay on:
    digitalWrite(Light_1, HIGH);
  }
  else {
    // turn relay off:
    digitalWrite(Light_1, LOW);
  }

  // Light 2
  if (buttonState2 == HIGH) {
    // turn relay 2 on:
    digitalWrite(Light_2, HIGH);
  }
  else {
    // turn relay off:
    digitalWrite(Light_2, LOW);
  }

  // Light 3
  if (buttonState3 == HIGH) {
    // turn relay 3 on:
    digitalWrite(Light_3, HIGH);
  }
  else {
    // turn turn off:
    digitalWrite(Light_3, LOW);
  }
}
1 Like
digitalWrite(Light_1, buttonState || buttonState3);
digitalWrite(Light_2, buttonState2 || buttonState3);

Or, if using low side switching with INPUT_PULLUP.

digitalWrite(Light_1, ! buttonState || ! buttonState3);
digitalWrite(Light_2, ! buttonState2 || ! buttonState3);

If you are going to switch relays on button presses, you really need to debounce the buttons to not bounce the relays and heat them up. Note that rapidly switching a relay is how to wear it out quickly!

I leave that to your expert help.

You could do this, keeping it to beginner code:

  buttonStates = digitalRead(buttonPin_3);
  buttonStates =  buttonStates * 2;
  buttonStates =  buttonStates + digitalRead(buttonPin_2);
  buttonStates =  buttonStates * 2;
  buttonStates =  buttonStates + digitalRead(buttonPin_1);
  delay(10);  // cheap, quick&dirty debounce all 3 buttons

and at the end, buttonStates will be 0 to 7 that a switch-case can use

0 - no button pressed
1 - button 1 pressed
2 - button 2 pressed
3 - buttons 1 and 2 pressed
4 - button 3 pressed
5 - buttons 1 and 3 pressed
6 - buttons 2 and 3 pressed
7 - all buttons pressed

then look Ma, no if-else mess needed!

switch ( buttonStates )
{
  case 1 :  
  // code for button 1 only
  break;

  case 2 :  
  // code for button 2 only
  break;

//  etc till case 7 code for all pressed
}

Connect your pushbuttons between input pins and GND, then try this:

const byte buttonPin_1 = 7,    // the number of the pushbutton pin
           buttonPin_2 = 6,
           buttonPin_3 = 5;    // Use for both relays?

const byte Light_1 = 2,   // Relay 2 pin
           Light_2 = 3,   // Relay 3 pin
           Light_3 = 8;   // Relay 8 pin


// variables will change:
boolean buttonState1 = true,         // variable for reading the pushbutton status
        buttonState2 = true,        //variable for reading the second buttons state
        buttonState3 = true;


void setup() 
{
  // initialize the LED pin as an output:
  pinMode(Light_1, OUTPUT);
  pinMode(Light_2, OUTPUT);
  pinMode(Light_3, OUTPUT);
  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin_1, INPUT_PULLUP);
  pinMode(buttonPin_2, INPUT_PULLUP);
  pinMode(buttonPin_3, INPUT_PULLUP);
}

void loop() 
{
  // read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin_1);
  buttonState2 = digitalRead(buttonPin_2);
  buttonState3 = digitalRead(buttonPin_3);

  digitalWrite(Light_1, ! buttonState1 || ! buttonState3);
  digitalWrite(Light_2, ! buttonState2 || ! buttonState3);  
}
1 Like

still needs debounce as bounce won't be seen with leds because bounce is faster than eyesight (but not print statements) or relay stuttering when a none-the-wiser coder adds relays.

yould do this

  digitalWrite(Light_1, ! buttonState1 || ! buttonState3);
  digitalWrite(Light_2, ! buttonState2 || ! buttonState3);

or this:

  // ! is logical NOT, turns 0 to 1 and not-0 to 0,  it reverses T/F
  buttonState1 = !digitalRead(buttonPin_1);
  buttonState2 = !digitalRead(buttonPin_2);
  buttonState3 = !digitalRead(buttonPin_3);
  delay(10);  // to debounce the pins in case any just changed

  digitalWrite(Light_1, buttonState1 || buttonState3);
  digitalWrite(Light_2, buttonState2 || buttonState3);  

You are correct, If the OP has that problem, help R us. :grin:

Thanks All i got my solution now i just got to find a bluetooth solution.
to pull an input.

My final code for now is

const byte buttonPin_1 = 7,    // the number of the pushbutton pin
           buttonPin_2 = 6,
           buttonPin_3 = 5;    // Use for both relays?

const byte Light_1 = 2,   // Relay 2 pin
           Light_2 = 3,   // Relay 3 pin
           Light_3 = 8;   // Relay 8 pin


// variables will change:
boolean buttonState1 = true,         // variable for reading the pushbutton status
        buttonState2 = true,        //variable for reading the second buttons state
        buttonState3 = true;


void setup() 
{
  // initialize the LED pin as an output:
  pinMode(Light_1, OUTPUT);
  pinMode(Light_2, OUTPUT);
  pinMode(Light_3, OUTPUT);
  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin_1, INPUT_PULLUP);
  pinMode(buttonPin_2, INPUT_PULLUP);
  pinMode(buttonPin_3, INPUT_PULLUP);
}

void loop() 
{
// ! is logical NOT, turns 0 to 1 and not-0 to 0,  it reverses T/F
  buttonState1 = !digitalRead(buttonPin_1);
  buttonState2 = !digitalRead(buttonPin_2);
  buttonState3 = !digitalRead(buttonPin_3);
  delay(10);  // to debounce the pins in case any just changed

  digitalWrite(Light_1, buttonState1 || !buttonState3);
  digitalWrite(Light_2, buttonState2 || !buttonState3);
}

Did you see @GoForSmoke's post #10?

After i added the last of his code it works as i intented.
i did not understand what he means byt it works.
now i just need to make a PCB with my setup.
Know somebody that can help me with that :slight_smile:

Ralph S. Bacon has a few videos on making a PCB from your schematic...

1 Like

Relays could maybe use a little more protection from switching too often but if the buttons are not pressed with manic speed then it won't be a problem.

Hallo All
i want to add more fungtiones
button 4 and button five
funtions descriped in code.

const byte buttonPin_1 = A2,    // the number of the pushbutton pin
           buttonPin_2 = A1,
           buttonPin_3 = A0;    // Use for both relays?
           buttonPin_4 = A3;    // Use for Relay1 hold. delay 2 sec. relay2 hold totaly 4 sec release both relays?
           buttonPin_5 = A4;    // first press light_1 and hold. second press Light_2 hold for 3 sec. when relese Light_1 and Light_2?

const byte Light_1 = 2,   // Relay A2 pin
           Light_2 = 3,   // Relay A1 pin
           Light_3 = 8;   // 


// variables will change:
boolean buttonState1 = true,         // variable for reading the pushbutton status
        buttonState2 = true,        //variable for reading the second buttons state
        buttonState3 = true;
        buttonState4 = true;
        buttonState5 = true;


void setup() 
{
  // initialize the LED pin as an output:
  pinMode(Light_1, OUTPUT);
  pinMode(Light_2, OUTPUT);
  pinMode(Light_3, OUTPUT);
  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin_1, INPUT_PULLUP);
  pinMode(buttonPin_2, INPUT_PULLUP);
  pinMode(buttonPin_3, INPUT_PULLUP);
  pinMode(buttonPin_4, INPUT_PULLUP);
  pinMode(buttonPin_5, INPUT_PULLUP);
}

void loop() 
{
// ! is logical NOT, turns 0 to 1 and not-0 to 0,  it reverses T/F
  buttonState1 = !digitalRead(buttonPin_1);
  buttonState2 = !digitalRead(buttonPin_2);
  buttonState3 = !digitalRead(buttonPin_3);
  buttonState4 = !digitalRead(buttonPin_4);
  buttonState5 = !digitalRead(buttonPin_4);
  delay(10);  // to debounce the pins in case any just changed

  digitalWrite(Light_1, buttonState1 || !buttonState3); // Buttonstate4 hold 5 sec.               // Guttonstate5 first press hold on
  digitalWrite(Light_2, buttonState2 || !buttonState3); // Buttonstate4 wait 2 sec. hold 3 sec.   // Buttonstate5 Second press hold 3 sec. release Light_1 and Light_2
}

Hope you can help.

Morten

The semi-colons should be commas.

The Arduino Reference Page -- bookmark this!

Reference link on Arrays -- will help you greatly!

What arrays do is let you make sets of variables that all have the same code name and a number for each one that a variable can hold and change which one to use... the same code can use any of them and ypu can add more, make the array bigger and the same code can refer to those with bigger index values.

const byte buttonPin_1 = A2,    // the number of the pushbutton pin
           buttonPin_2 = A1,
           buttonPin_3 = A0;    // Use for both relays?
           buttonPin_4 = A3;    // Use for Relay1 hold. delay 2 sec. relay2 hold totaly 4 sec release both relays?
           buttonPin_5 = A4;    // first press light_1 and hold. second press Light_2 hold for 3 sec. when relese Light_1 and Light_2?

becomes

const byte buttonPin[ 5 ] = { A2, A1, A0, A3, A4 };

// now buttonPin[ 0 ] is what buttonPin_1 was

  // initialize the pushbutton pins as INPUT_PULLUP:

for ( byte i = 0; i < 5; i++ ) // this is a for-next loop, i becomes 0 to 4
{ 
  // they all have the same code name, one line for all!
  pinMode( buttonPin[ i ], INPUT_PULLUP );
}

You can array booleans, any variable, even structs you don't know about yet --- structs are a way to group different types of variable as named make-your-own-variables!

Organizing data (constants and variables) by name lets you write less code. You don't have to copy-paste-edit the same lines to deal with different names when you make them name[number].

You can have

byte x[ 3 ][ 3 ] to have a rows x columns arrangement
byte y[ 3 ][ 3 ][ 3 ] to describe a volume.....

Arrays are basic level code power tools.... even BASIC language has them.

Mess with and learn arrays.. save your typing fingers!

1 Like

Hello mikefie

What is the task of the sketch in real life?

I had a quick look at the programme and I think that the project calls for a button manager based on structs and arrays.

The IPO model will give the base concept:

  • Input: read and debounce button
  • Process: detect state changes
  • Output: take action on state changes
1 Like

I want to make a board that have 5 relays to automate some things in garage some with foto sensor and remote from my car. and some switches in my garage