Issues with "digital read" for two sensors

hie i am trying to use two switches as bumper switches on my robot., one on each side. The problem is that one switch only works. Each motor initially is running at maximum speed. After each switch is hit each motor reduces its speed for 3 seconds before each returns to its maximum speed.

EDIT: only limswitch2 works the other just keeps running at max speed even if its switch has been hit.

//I am using the Arduino Mega 2560 R3
int limswitch =41;
int limswitch2=39;// define limit switch and allocate its pin
int ENA=51;//Enable for motor controller at pin 51
int RPWMA =45;//RPWM
int LPWMA =43;//Left pwm
int ENB = 53;
int RPWMB = 47;
int LPWMB = 51;
int led= 13;//default arduino led.

void setup()
{
 pinMode(ENA,  OUTPUT);
 pinMode(RPWMA,OUTPUT);
 pinMode(LPWMA,OUTPUT);
 pinMode(led,OUTPUT);
  digitalWrite(ENA,HIGH);   
 analogWrite(RPWMA,0);  
 digitalWrite(LPWMA,HIGH); 
 digitalWrite(ENB,HIGH);   
  analogWrite(RPWMB,0);  
  digitalWrite(LPWMB,HIGH); 
  
}
void loop()
{
  
int switch1 = digitalRead(limswitch2);//define the limit switch as switch 1
  if (switch1 == HIGH)//if the pin reads 5v
  {
    //run motor for 3 seconds at 50% power
  digitalWrite(ENA,HIGH);   
  analogWrite(RPWMA,100);  
  digitalWrite(LPWMA,HIGH); 
  delay(3000); 
  //return to max speeed
  digitalWrite(ENA,HIGH);   
  analogWrite(RPWMA,0);  
  digitalWrite(LPWMA,HIGH); 
}
int switch2 = digitalRead(limswitch);//define the limit switch as switch 2
  if (switch2 == HIGH)//if the pin reads 5v
  {
    //run motor for 3 seconds at 50% power
  digitalWrite(ENB,HIGH);   
  analogWrite(RPWMB,100);  
  digitalWrite(LPWMB,HIGH); 
  delay(3000); 
  // return to max speed.
  digitalWrite(ENB,HIGH);   
  analogWrite(RPWMB,0);  
  digitalWrite(LPWMB,HIGH);  
  }
}

EDIT: only limswitch2 works the other just keeps running at max speed even if its switch has been hit.

Don't you suppose that it matters how the switches are wired? I do.

Don't you suppose that it matters how the switches are wired? I do.

each motor has its own limit switch. limswitch 1 is not working limswitch 2 works. i have even put limswich 1 in the pins of limswitch 2 and it worked. i think the issue is with the code.

tawandapro:

Don't you suppose that it matters how the switches are wired? I do.

each motor has its own limit switch. limswitch 1 is not working limswitch 2 works. i have even put limswich 1 in the pins of limswitch 2 and it worked. i think the issue is with the code.

I'm with Paul, I think the issue is with your wiring. You need to add a pull-down resistor (say 10K ohms) from each switch's digital input pins to ground. Your are most likely suffering from 'floating input pin condition'.

Lefty

tawandapro:

Don’t you suppose that it matters how the switches are wired? I do.

each motor has its own limit switch. limswitch 1 is not working limswitch 2 works. i have even put limswich 1 in the pins of limswitch 2 and it worked. i think the issue is with the code.

PaulS was asking a perfectly valid question. Do you think it matters HOW the switches are wired? I think it matters too.

Tell us how thay are wired. Details such as 'is there a resistor in the switch circuit?" will tell us a LOT about your problem. Hint: If the answer is “No”, you have it wired wrong. If you tell us how it’s wired, we’ll tell you how to do it right, if you have it wrong.