2 Push buttons to toggle Same Relay

Hi,

I am trying to have 2 push buttons to toggle the state of a relay.
Basically what i want to do is use the default deb ounce script but instead of taking input from one button have 2 buttons.
Pressing either one would turn ON/OFF the relay.

I'm sure its a simple task but i don't really understand how i would go about coding it.

if someone could guide me where to look or help with the code, i would appreciate it a lot.

Thanks

Basically what i want to do is use the default deb ounce script

As opposed to the sue pound one?

I'm sure its a simple task but i don't really understand how i would go about coding it.

Read the state of the 1st switch. If it has become pressed, and the relay is off, turn the relay on.
Read the state of the 2nd switch. If it has become pressed, and the relay is off, turn the relay on.

:o :o I don't get the joke... :confused:

As for my problem, if I was to read the state of the push button they will always return a low.

:o :o I don't get the joke... :confused:

I did.

chachu1:
As for my problem, if I was to read the state of the push button they will always return a low.

Why? This implies that you have written some code. Show it to us.
Post it in your next reply, formatted and placed between code tags.

In the IDE, you can correctly format your code using >Tools >Auto Format. Then, in the forum "Reply" dialog window, use </> to generate code tags. (You can paste your code into the "Reply" dialog, then select it and press </>, and it will be automatically enclosed within code tags.)

chachu1:
I'm sure its a simple task but i don't really understand how i would go about coding it.

Read and save the values from the two buttons (or 10 buttons if you wish).
Decide what to do based on the saved values.

Separate the business of reading the buttons from the business of switching the relays. The saved values should be the only thing the processes have in common.

...R

Hello,

Sorry haven't replied earlier, so this is what i have right now,
but this doesn't seem to work.

const int buttonPin = 2;
const int espPin = 4; // this is the line i Added
const int ledPin = 13;

int ledState = HIGH;
int buttonState;
int lastButtonState = LOW;

long lastDebounceTime = 0;
long debounceDelay = 50;

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(espPin, INPUT); // i added this bit as well

  digitalWrite(ledPin, ledState);
}

void loop() {
  int reading1 = digitalRead(buttonPin);
  int reading2 = digitalRead(espPin) * 2; //I added this bit .. esp pin needs to have priority over the other one, in my case
  
  int reading;
  if (reading2 > reading 1){
  	reading = digitalRead(espPin);
  	}
  	else
  	{
  	reading = digitalRead(buttonPin);
  	}

  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;

      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }

  digitalWrite(ledPin, ledState);

  lastButtonState = reading;
}
  if (reading2 > reading 1){

What is that space doing in there?

  if (reading != lastButtonState) {

Wouldn't comparing currButtonState to lastButtonState make more sense? reading is a rather useless name.

You are trying to mix the state change detection example to work with 2 switches. The way you are doing it is not going to work.

Determine if switch one has become pressed. If it has, set a flag, switchOneBecamePressed, to true.
Determine if switch one has become released. If it has, set a flag, switchOneBecamePressed, to false.
Determine if switch two has become pressed. If it has, set a flag, switchTwoBecamePressed, to true.
Determine if switch two has become released. If it has, set a flag, switchTwoBecamePressed, to false.

Use meaningful names for the current and previous state of the two switches.

Then, you can decide what to do based on the two flags.

Hi,

So i took your advice and started reading the state of the 2 buttons and giving them different value and based of the value i can either have an LED ON or OFF

Below is where i reached,
unfortunately it doesn't seem to work.

i Know it still needs the debounce bit but once i have logic correct i can add that in.(right?)

const int buttonPin = 5;
const int espPin = 4;
const int ledPin = 7;

int ledState = LOW;
int master; //this will hold a value based on which i decide what action needs to be taken.
int espState;
int buttonState;
int LastEspState = LOW;
int lastButtonState = LOW;


void setup() {
  // put your setup code here, to run once:
pinMode(buttonPin, INPUT);
pinMode(espPin, INPUT);
pinMode(ledPin, OUTPUT);

digitalWrite(ledPin, ledState);
}

void loop() {
  // put your main code here, to run repeatedly:

espState = digitalRead(espPin);
buttonState = digitalRead(buttonPin);

if (espState != LastEspState){
  if (espState == HIGH){
    master = 11;
    espState = LastEspState;
    }
    else if (espState == LOW){
      master = 12;
      espState = LastEspState;
      }
}
if (buttonState != LastPushButtonState){
  if (buttonState == HIGH){
    master = 21;
    buttonState = LastPushButtonState;
    }
    else if (buttonState == LOW){
      master = 22;
      buttonState = LastPushButtonState;
}
}
if (master == 11){
 digitalWrite(ledPin, HIGH);}
if (master == 21){ 
  digitalWrite(ledPin, HIGH);}
if (master == 12){ 
  digitalWrite(ledPin, LOW);} 
if (master == 22){ 
  digitalWrite(ledPin, LOW);}
  
}

chachu1:
unfortunately it doesn't seem to work.

Tell us what you mean by that. What does it actually do? What would you like it to do?

...R

How ARE the switches wired? Do you have external pulldown resistors?

Robin2:
Tell us what you mean by that. What does it actually do? What would you like it to do?

...R

Doesn't do anything at all. Light stays on all the time.

PaulS:
How ARE the switches wired? Do you have external pulldown resistors?

Umm the push button are connected via resistor,

I have made a diagram of it, hope that helps.

Use the internal pullup resistor. Connect one leg of the switch to ground. Connect the other leg to a digital pin. Set the pin mode to INPUT_PULLUP.

Add some Serial.print() statements to the code, to learn what it is actually doing.

I'm sure its a simple task but i don't really understand how i would go about coding it.

Use the forum search box in the upper right of this page and search for "toggle". There have been past discussions on similar subjects. Have you considered putting the switches in parallel?