How i can disable the input button while other button pressed

Help me to solve the issue with input buttons and leds i need if one button pressed and related led glow for specific time the other out and input buttons not work. Thanks

const int button1 = 2;
const int button2 = 3;
const int led1 = 8;
int led2 = 9;
unsigned long interval = 800;
unsigned long startTime;
byte lastPress1;
byte lastPress2;

void setup()
{   
  Serial.begin(9600);              
 pinMode(led1, OUTPUT);
 pinMode(led2, OUTPUT);
 pinMode(button1, INPUT); 
 pinMode(button2, INPUT); 
}
//
void loop()
{
 byte pressed1 = digitalRead(button1);
 if (pressed1)
 {
   if (pressed1 != lastPress1)
   {
     digitalWrite(led1, HIGH);
     startTime = millis();
   }
 }
 lastPress1 = pressed1;
 if (digitalRead(led1))
 {
   if (millis() - startTime >= interval)
   {
     digitalWrite(led1, LOW);
     Serial.print(lastPress1);
   }
 }
byte pressed2 = digitalRead(button2);
 if (pressed2)
 {
   if (pressed2 != lastPress2)
   {
     digitalWrite(led2, HIGH);
     startTime = millis();
   }
 }
 lastPress2 = pressed2;
 if (digitalRead(led2))
 {
   if (millis() - startTime >= interval)
   {
     digitalWrite(led2, LOW);
   }
 }
}

You don't need to disable a button, just ignore its value.

Suppose that digitalRead(buttonpin) produces LOW when a button is pressed. Then if you only want to act on button2 when button1 is not pressed, use some code like this

button1State  = digitalRead(button1pin);
button2State = digitalRead(button2pin);

if (button1State == LOW) {
   // do the button1 stuff
}
else if (button2State == LOW) {
      // do the button2 stuff
}

...R

Try this: First read both button1 and button2.

if(pressed1 && ! pressed2) in the code for button1
if(pressed2 && ! pressed1) in the code for button2

What is happening with the current code you are using?

How are your buttons wired?

The code logic indicates that you should be using external pull down resistors. Are you doing that?

More typical is to use the INPUT_PULLUP which engages the internal 5v pullup. Connect the input pin across the switch to ground. Then a button press will read LOW when the input is connected to ground. The input pin will read HIGH when the switch is not closed.

I am using pull up resistor 12K at pin2 —////----- +
|
|___Button_____Gnd
when i press the button1 the the LED Glows for specified time but same time if i pressed the button2 the led 2 also glows i need to on one led at a time.

Good. However there are built in pull up resistors available using "INPUT_PULLUP" when declaring the input in Setup. Anyway, You have done the right thing.

I try to follow the suggestion from Railroader but i got error.

Given the external pullups to 5v and the switch to ground, the logic in your code is backwards.

You have initialized these variables to 0

byte lastPress1;
byte lastPress2;

These commands will be true when the button is not pressed first pass through loop.

byte pressed1 = digitalRead(button1);
 if (pressed1){}

byte pressed2 = digitalRead(button2);
if(pressed2){}

Both leds should light.

What kind of error? Attach both the code and the error message.

const int button1 = 2;
const int button2 = 3;
const int led1 = 8;
int led2 = 9;
unsigned long interval = 800;
unsigned long startTime;
byte lastPress1;
byte lastPress2;

void setup()
{   
  Serial.begin(9600);              
 pinMode(led1, OUTPUT);
 pinMode(led2, OUTPUT);
 pinMode(button1, INPUT); 
 pinMode(button2, INPUT); 
}
//
void loop()
{
 byte pressed1 = digitalRead(button1);
 if (pressed1 && ! pressed2)
 {
   if (pressed1 ! = lastPress1)
   {
     digitalWrite(led1, HIGH);
     startTime = millis();
   }
 }
 lastPress1 = pressed1;
 if (digitalRead(led1))
 {
   if (millis() - startTime >= interval)
   {
     digitalWrite(led1, LOW);
     Serial.print(lastPress1);
   }
 }
byte pressed2 = digitalRead(button2);
 if (pressed2 && ! pressed1)
 {
   if (pressed2 != lastPress2)
   {
     digitalWrite(led2, HIGH);
     startTime = millis();
   }
 }
 lastPress2 = pressed2;
 if (digitalRead(led2))
 {
   if (millis() - startTime >= interval)
   {
     digitalWrite(led2, LOW);
   }
 }
}

Error: [code]

Arduino: 1.8.9 (Windows Store 1.8.21.0) (Windows 10), Board: "Arduino/Genuino Uno"

WARNING: Category 'Display, Hangul' in library LiquidCrystal_I2C_Hangul is not valid. Setting to 'Uncategorized' C:\Users\Mohammed Shahid Nase\Documents\Arduino\Button_timer_2_BUTTONS-uno-test\Button_timer_2_BUTTONS-uno-test.ino: In function 'void loop()':

Button_timer_2_BUTTONS-uno-test:22:20: error: 'pressed2' was not declared in this scope

if (pressed1 && ! pressed2)

^

Button_timer_2_BUTTONS-uno-test:24:17: error: expected ')' before '!' token

if (pressed1 ! = lastPress1)

^

exit status 1 'pressed2' was not declared in this scope

This report would have more information with "Show verbose output during compilation" option enabled in File -> Preferences.

[/code]

Ok. One uninteional code must be this: if (digitalRead(led1))

Move byte pressed2 = digitalRead(button2); to just below the line reading button1.

Dear Railroader really :o my head is in space sorry but i cannot under stander if you can try to run and give me the correct code then easy for me to understand. Thanks a lot

Ok. An other version of the reply.

You attempt to read from a pin declared as an OUTPUT, LED1. That looks strange to me but maybe it's ok.

 if (digitalRead(led2))

In Your code You both declare and initiate the variables pressed1 and pressed2. Start loop by this way and drop the later coming Reading of button2.

void loop()
{
 byte pressed1 = digitalRead(button1);
 byte pressed2 = digitalRead(button2);

Dear Railroader
Thanks a lot for your help really i try to understand your advice’s, but i think that i need to do more hard work and spent more time on learning.
I cannot understand how to make it working.

sny2ksa: i need if one button pressed and related led glow for specific time the other out and input buttons not work.

Simplify. Put the other things aside for now and just get this part, i need if one button pressed and related led glow for specific time, working. Once you've done that the rest won't seem as hard.

sny2ksa:
I cannot understand how to make it working.

Have you considered the suggestion in Reply #1?

…R

I try try again and now its just ok, but when i add MP3-TF-16P its stop working no button or sound working Please have a look on the code and let me know where i am wrong. Thanks'

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
const int button1 = 2;
const int button2 = 3;
const int led1 = 8;
int led2 = 9;
unsigned long interval = 800;
unsigned long startTime;
bool lastPress1;
bool lastPress2;

void setup()
{  
  mySoftwareSerial.begin(9600);
  Serial.begin(115200);
 myDFPlayer.volume(20);  //Set volume value. From 0 to 30
             
 pinMode(led1, OUTPUT);
 pinMode(led2, OUTPUT);
 pinMode(button1, INPUT_PULLUP); 
 pinMode(button2, INPUT_PULLUP); 
}

void loop()
{
  
 bool pressed1 = digitalRead(button1);
 bool pressed2 = digitalRead(button2);
 if (pressed1)
 {
   if (pressed1 != lastPress1)
   {
    digitalWrite(led1, HIGH);
     myDFPlayer.play(1);  //Play the first mp3
     startTime = millis();
   }
 }
 lastPress1 = pressed1;
 if (digitalRead(led1))
 {
   if (millis() - startTime >= interval)
   {
     digitalWrite(led1, LOW);
     
   }
 }

 if (pressed2)
 {
   if (pressed2 != lastPress2)
   {
     digitalWrite(led2, HIGH);
     startTime = millis();
   }
 }
 lastPress2 = pressed2;
 if (digitalRead(led2))
 {
   if (millis() - startTime >= interval)
   {
     digitalWrite(led2, LOW);
   }
 }
}

Can you successfully run the stand alone library example code for DFRobotDFPlayerMini.h?

Yes i run successfully the example code and the buttons code also works separately.

I think that you need to add to setup() a .begin() statement for the DFPlayer instance. You need to pay closer attention to the library example.

void setup()
{
  mySoftwareSerial.begin(9600);
  Serial.begin(115200);
  myDFPlayer.begin(mySoftwareSerial);//add this line
  myDFPlayer.volume(20);

  //rest of setup()
}