3 push button for 3 operation

Hello all,
I want to add 3 push button on my project to preform 3 different condition, see the code below.

But this code is working for only button i.e A5 and other button and its condition not working.

please help



int s1 = 0;
int s2 = 0;
int s3 = 0;

int relay1 = A1; //relay1
int relay2 = A2; //relay1
int relay3 = A3; //relay3




void setup() {
  
pinMode(A5, INPUT_PULLUP);// define pin two as input for push button
pinMode(A6, INPUT_PULLUP);// define pin two as input for push button
pinMode(A7, INPUT_PULLUP);// define pin two as input for push button
  
pinMode(relay1,OUTPUT);
pinMode(relay2,OUTPUT);
pinMode(relay3,OUTPUT);

digitalWrite(relay1,LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
  
}




void loop() {
  
//// conditions    //// conditions   //// conditions   //// conditions   //// conditions  

 
if (s1==0 && s2==1 && s3==0){
    digitalWrite(relay1, HIGH);
    }
  
if (s1==1 && s2==1 && s3==0){
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
    }  

if (s1==1 && s2==1 && s3==1){
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
    digitalWrite(relay3, HIGH);
    }  



//// buttons    //// buttons //// buttons //// buttons   

   int b1 = digitalRead(A7);
   if (b1 == LOW) 
  {
    s1=1;
  } 
 
  
   int b2 = digitalRead(A6);
   if (b2 == LOW) 
  {
    s3=1;
  } 
 
  int b3 = digitalRead(A5);
   if (b3 == LOW) 
  {
    s2=1;
  } 
  
}

Hello abhixs
Take here a view to get some ideas.

What experience do you have in programming in c++?

Hi, @abhixs
Is there a reason you are using the analog input pins, they can be used for digital, instead of the D digital pins.

What model Arduino are you using?

If it is a Nano, A6 and A7 are not fitted with internal pullup resistors.
You will have to fit 10K pullup resistors externally.

What happens if b1, b2 or b3 are == HIGH ?

Tom... :smiley: :+1: :coffee: :australia:

I am using ARDUINO NANO board , and i can use D pin but it is not working on D pin also.

if b1, b2 or b3 are == HIGH it start working directly upon switch on.

I think i need to put 10K pullup resistors as you suggested.

Hi,
You need to code for the HIGH input case as well.

//// buttons    //// buttons //// buttons //// buttons   

   int b1 = digitalRead(A7);
   if (b1 == LOW) 
  {
    s1=1;
  } 
else
{
  s1 = 0;
}
 
  
   int b2 = digitalRead(A6);
   if (b2 == LOW) 
  {
    s3=1;
  } 
else
{
    s3 = 0;
}
 
  int b3 = digitalRead(A5);
   if (b3 == LOW) 
  {
    s2=1;
  } 
else
{
  s2 = 0;
}

This might be why it didn't work when you used the digital pins.
A6 and A7 do not work as digital pins.

Rewrite your code for D pins.
There is an easier way to write this code, but lets use yours for starters.

Tom... :smiley: :+1: :coffee: :australia:

Hi @TomGeorge ,

I updated the code with digital pin, but this is still not working, only last condition

if (s1==1 && s2==1 && s3==1){
    digitalWrite(relay1, HIGH);
    delay (1000);
    digitalWrite(relay1, LOW);
    delay (1000);
    digitalWrite(relay1, HIGH);
    s2=0;
    s1=0;
    s3=0;
    }  

is working upon pressing D6 connected button

Updated code with digital pin

int s1 = 0;
int s2 = 0;
int s3 = 0;

int relay1 = A1; //relay1
int relay2 = A2; //relay1
int relay3 = A3; //relay3




void setup() {
  
pinMode(4, INPUT_PULLUP);// define pin two as input for push button
pinMode(5, INPUT_PULLUP);// define pin two as input for push button
pinMode(6, INPUT_PULLUP);// define pin two as input for push button
  
pinMode(relay1,OUTPUT);
pinMode(relay2,OUTPUT);
pinMode(relay3,OUTPUT);

digitalWrite(relay1,LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
  
}




void loop() {
  
//// conditions    //// conditions   //// conditions   //// conditions   //// conditions  

 
if (s1==0 && s2==1 && s3==0){
    digitalWrite(relay1, HIGH);
    s2=0;
    }
  
if (s1==0 && s2==1 && s3==1){
    digitalWrite(relay1, HIGH);
    delay (1000);
    digitalWrite(relay1, LOW);
    s2=0;
    s1=0;
    }  

if (s1==1 && s2==1 && s3==1){
    digitalWrite(relay1, HIGH);
    delay (1000);
    digitalWrite(relay1, LOW);
    delay (1000);
    digitalWrite(relay1, HIGH);
    s2=0;
    s1=0;
    s3=0;
    }  



//// buttons    //// buttons //// buttons //// buttons   

  int b1 = digitalRead(4);
   if (b1 == LOW) 
  {
    s1=1;
  } 
else
{
  s1 = 0;
}
 
  
   int b2 = digitalRead(5);
   if (b2 == LOW) 
  {
    s3=1;
  } 
else
{
    s3 = 0;
}
 
  int b3 = digitalRead(6);
   if (b3 == LOW) 
  {
    s2=1;
  } 
else
{
  s2 = 0;
}}

Hi,
Remove these from the if statements.

if (s1==0 && s2==1 && s3==0){
    digitalWrite(relay1, HIGH);
//    s2=0;
    }
  
if (s1==0 && s2==1 && s3==1){
    digitalWrite(relay1, HIGH);
    delay (1000);
    digitalWrite(relay1, LOW);
//    s2=0;
//    s1=0;
    }  

if (s1==1 && s2==1 && s3==1){
    digitalWrite(relay1, HIGH);
    delay (1000);
    digitalWrite(relay1, LOW);
    delay (1000);
    digitalWrite(relay1, HIGH);
//    s2=0;
//    s1=0;
//    s3=0;
    }  

You don't need to intitialise the s variables now that you have the "else" statements when you read the buttons.

Tom... :smiley: :+1: :coffee: :australia:

Removed s variables but still it is having same issue, only last if statement is working by pressing D6 button :roll_eyes:

not sure if you tried pressing more than one button at a time because the only condition that requires just one button to be pressed to energies a relay is when s2 is 1 and the s1/s3 are 0.

the 2nd condition requires that s1 is 1 and A7 is pressed AND s2/A5

the 3rd condition requires all 3 buttons to be pressed

Yes you are right, and i am facing this error and unable to solve at my end.

Hi,
can you please suggest other easier way to write this code..please

How are your buttons wired to the input pins? Which button is wired to which input pin? Which relay board are you using? Are the relays enabled with a HIGH signal or a LOW signal? Where does the relay board power come from? If from the Nano's 5V pin, it will not work correctly.

So what happens when you press S1, S2, and S3 TOGETHER?
Does relay1 go ON for 1second, OFF for 1second, then ON.

Can you tell us what sequence you want for each combination of button presses.
Make a table.

NOTE for your if statements to work, you must operate the relevant buttons TOGETHER, NOT one after the other.

Tom.... :smiley: :+1: :coffee: :australia:

Currently i am not using relay, it is connected with led.


Hello abhixs
And again:
Post a schematic and a timing diagram.

do you want each of your conditions to depend on multiple button presses or just a single button?

if the one case works, your buttons are wires properly

Yes i need exactly this ...attaching my schematic.

My updated code

int s1 = 0;
int s2 = 0;
int s3 = 0;

int led1 = A1; //led1
int led2 = A2; //led1
int led3 = A3; //led3




void setup() {
  
pinMode(D2, INPUT_PULLUP);// define pin two as input for push button
pinMode(D3, INPUT_PULLUP);// define pin two as input for push button
pinMode(D4, INPUT_PULLUP);// define pin two as input for push button
  
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);

digitalWrite(led1,LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
  
}




void loop() {
  
//// conditions    //// conditions   //// conditions   //// conditions   //// conditions  

 
if (s1==0 && s2==1 && s3==0){
    digitalWrite(led1, HIGH);
    }
  
if (s1==1 && s2==1 && s3==0){
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    }  

if (s1==1 && s2==1 && s3==1){
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
    }  



//// buttons    //// buttons //// buttons //// buttons   

   int b1 = digitalRead(D4);
   if (b1 == LOW) 
  {
    s1=1;
  } 
 
  
   int b2 = digitalRead(D3);
   if (b2 == LOW) 
  {
    s3=1;
  } 
 
  int b3 = digitalRead(D2);
   if (b3 == LOW) 
  {
    s2=1;
  } 
  
}


so what happens when you press all 3 buttons at the same time and when you press buttons attached to A5 and A7 at the same time?

nothing happening, my requirement is different i.e:

1. when b1 is pressed then nothing will happen
2. when b1 and after b2 is pressed then this should triggered

if (s1==1 && s2==1 && s3==0){
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    }  

3. when only b2 is pressed then this should triggered

if (s1==0 && s2==1 && s3==0){
    digitalWrite(led1, HIGH);
    }

4. when b3 and after b2 and after b1 is pressed then this should triggered

if (s1==1 && s2==1 && s3==1){
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
    }  

you still don't reset your variables to 0 if the button gets released.

untested:

const byte s1 = 0;
const byte  s2 = 0;
const byte  s3 = 0;

const byte led1 = A1; //led1
const byte led2 = A2; //led1
const byte led3 = A3; //led3

void setup() {
  pinMode(D2, INPUT_PULLUP);// define pin two as input for push button
  pinMode(D3, INPUT_PULLUP);// define pin two as input for push button
  pinMode(D4, INPUT_PULLUP);// define pin two as input for push button
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
}

void loop() {
  //// buttons    //// buttons //// buttons //// buttons

  int b1 = digitalRead(D4);
  if (b1 == LOW)
    s1 = 1;
  else
    s1 = 0;

  int b2 = digitalRead(D3);
  if (b2 == LOW)
    s3 = 1;
  else
    s3 = 0;

  int b3 = digitalRead(D2);
  if (b3 == LOW)
    s2 = 1;
  else
    s2 = 0;

  delay(100); // dirty delay to overcome the problem of not parallel pressing the buttons

  //// conditions    //// conditions   //// conditions   //// conditions   //// conditions
  if (s1 == 0 && s2 == 1 && s3 == 0) {
    digitalWrite(led1, HIGH);
  }

  if (s1 == 1 && s2 == 1 && s3 == 0) {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
  }

  if (s1 == 1 && s2 == 1 && s3 == 1) {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
  }
}

this will check that the buttons are pressed (nearly) at the same time.
If you need a sequence/order ... you will need something different.
And a lot of more definitions, what should happen in edge cases ... like pressing a wrong order, timeouts etc ...