0
int mf11=13;//motor1 1st terminal  
int mf12=12;//motor1 2nd terminal
int mf21=9;//motor2 1st terminal 
int mf22=8;//motor2 2nd  terminal 
int fr=2;//front key 
int bk=3;//back key 
int rt=4;//right key  
int lt=5;//left key

void setup() {   
    pinMode(mf11,OUTPUT);
    pinMode(mf12,OUTPUT);
    pinMode(mf21,OUTPUT);
    pinMode(mf22,OUTPUT);
    pinMode(fr,INPUT);
    pinMode(bk,INPUT);
    pinMode(rt,INPUT);
    pinMode(lt,INPUT);
}

void loop() {  
    int s = digitalRead (fr);
    int h = digitalRead (bk);
    int i = digitalRead (rt);
    int v = digitalRead (lt);
    if(s==HIGH) {   
         digitalWrite(mf11,HIGH);
         digitalWrite(mf12,LOW);
         digitalWrite(mf21,HIGH);
         digitalWrite(mf22,LOW);
    }   
    if (h==HIGH) {   
        digitalWrite(mf11,LOW);
        digitalWrite(mf12,HIGH);
        digitalWrite(mf21,LOW);
        digitalWrite(mf22,HIGH);
    }
    if (i==HIGH) {   
         digitalWrite(mf11,LOW);
         digitalWrite(mf12,HIGH);
         digitalWrite(mf21,HIGH);
         digitalWrite(mf22,LOW);
    }
   if (v==HIGH) {  
        digitalWrite(mf11,HIGH);  
        digitalWrite(mf12,LOW);  
        digitalWrite(mf21,LOW);  
        digitalWrite(mf22,HIGH);
   } else {   
       digitalWrite(mf11,LOW);
       digitalWrite(mf12,LOW);
       digitalWrite(mf21,LOW);
       digitalWrite(mf22,LOW);
  }
}

closed as unclear what you're asking by MatsK, sempaiscuba, Greenonline, gre_gor, Dmitry Grigoryev Aug 2 '18 at 8:38

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Would need schematics to be able to tell whats going on.... Also I am guessing you really want to use else if statements rather then separate if statements as the else only applies to the if(v==HIGH) statement (this may be part/all of your issue, as that is the condition you say works) – Chad G Jul 31 '18 at 18:51
3

The reason your code only works for left, is because when v is LOW your else block gets executed and sets all pins to low! You need to use else if.

if(condition1)
{
  ...
}
else if (condition2)
{
  ...
}
else
{
  ...
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.