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);
}
}