Combining push button and Serial.read to make led on and off.

Hello, I am writing a code to control led with Push Button and Serial communication. When I press push button led is on. Then I want to send "R" through serial monitor and led should be off. But the strange thing is that when I only open serial monitor then led is off :o . It doesn't wait for "R". I need to know what should be the correction in my code. Here is the code

int switch1 = 2;
int motorled1 = 3;
int switch2 = 4;
int motorled2 = 5;
int d1=0;
int d2=0;
const int RX = 0;
const int TX = 1;
int reset1=0;
int reset2=0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  pinMode(2, INPUT);
  pinMode(4, INPUT);
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  d1 = digitalRead(2);
  d2 = digitalRead(4);

  while (d1 == 1)

{ 
    digitalWrite(motorled1, HIGH);
    digitalWrite(motorled2,LOW);

  
 while (Serial.available() > 0)

   {   (Serial.write(1));
  
    reset1 = (Serial.read());
  
     if(reset1 == 'R')
      {   digitalWrite(motorled1,LOW);
        digitalWrite(motorled2,LOW);
        
    }
}

}


  while (d2 == 1)
  { 
    digitalWrite(motorled2, HIGH);
    digitalWrite(motorled1, LOW);
  
   while(Serial.available() > 0)

 {(Serial.write(2));
  reset2 = (Serial.read());
  if(reset2=='R')
       
     {   digitalWrite(motorled1,LOW);
        digitalWrite(motorled2,LOW);
       
    }
        
    
}
  }
}

You enter a loop with no exit..
May use the break-statement to exit?

I would also be interested to know how your script can be modified to stop/start a countdown timer. I am trying to build a football scoreboard, and i'm a little tuck on the timer. Your script sounds like what i need, but to function as a start/stop for a timer. Hope you make out ok with your issue.

int switch1 = 2;
int motorled1 = 3;
int switch2 = 4;
int motorled2 = 5;
int d1 = 0;
int d2 = 0;
int buzzer;
char reset1;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);

pinMode(2, INPUT);
pinMode(4, INPUT);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);

}

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

d1 = digitalRead(2);
d2 = digitalRead(4);
if (d1 == 1 && buzzer == 0)

{
digitalWrite(motorled1, HIGH);
buzzer = 1;

}

else if (d2 == 1 && buzzer == 0)
{
digitalWrite(motorled2, HIGH);
buzzer = 2;
}

if (Serial.available() > 0)

{
(Serial.write(buzzer));

reset1 = (Serial.read());
buzzer = 0;

if (reset1 == 'R')
Serial.println("LED is off");
{ digitalWrite(motorled1, LOW);
digitalWrite(motorled2, LOW);

}
}
}

This is the answer. I modified the code.

Please edit it to use code tags like in you first post :wink:

And a little tip, once you start numbering variables, arrays are the answer :wink: See Gammons Tip 1 (and for more).

But worst, you give pins names but never use them! Go and use the names!

And don't just make all the variables global.

After some updates

const byte SwitchPins[] = {2, 4};
const byte MotorLedPins[sizeof(SwitchPins)] = {3, 5};
bool buzzer = false;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  for(byte i = 0; i < sizeof(SwitchPins); i++){
    pinMode(SwitchPins[i], INPUT);
  }
  
  for(byte i = 0; i < sizeof(MotorLedPins); i++){
    pinMode(MotorLedPins[i], OUTPUT);
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  
  for(byte i = 0; i < sizeof(SwitchPins); i++){
    if(digitalRead(SwitchPins[i]) && !buzzer){
      digitalWrite(MotorLedPins[i], HIGH);
      buzzer = 1;
    }
  }
  
  if (Serial.available() > 0)
  {
    Serial.print("Buzzer state: ");
    Serial.println(buzzer);

    if (Serial.read() == 'R')
    {
      Serial.println("LEDs set off");
      buzzer = false;
      for(byte i = 0; i < sizeof(MotorLedPins); i++)
      {
        digitalWrite(MotorLedPins[i], LOW);
      }
    }
  }
}