Triggering the push button on the analog input of the L293D motorshield I'm trying to drive a 12v dc motor using three push buttons, one to turn clockwise, the other counter-clockwise and the other to stop the motor completely.

However the code below is not working, apparently it feels like I'm doing something wrong in the code on the connect buttons on the analog part of the engine shield L293D.

The Arduino is powered by a 9v battery and motor shield **L293D with an external power.

#include &lt;AFMotor.h&gt;

    AF_DCMotor motor(1);

    int val; 

    #define btnleft A0 
    #define btncenter A1 
    #define btnright A2 


    void setup() 
    { 
      pinMode(btnleft, INPUT);      
      analogWrite(btnleft, HIGH);  
      pinMode(btncenter, INPUT);
      analogWrite(btncenter, HIGH);
      pinMode(btnright, INPUT);
      analogWrite(btnright, HIGH);
    } 

    void loop() 
    { 
     val=digitalRead(btnleft);  
       if(val!=1) 
        {
        Serial.print("LEFT: ");
        motor.setSpeed(255); 
        motor.run(FORWARD); 
        delay(3800); 
        }

     val=digitalRead(btncenter);
       if(val!=1)
        {
        Serial.print("CENTER"); 
        motor.setSpeed(255); 
        motor.run(BACKWARD);
        delay(3800); 
        }

     val=digitalRead(btnright);
       if(val!=1)
        {
        Serial.print("RIGHT"); 
        motor.run(RELEASE);
        delay(3000);    
        }
    }
As noted in Nick Gammon's comment, digitalWrite(btnleft, INPUT) (etc.) would make a lot more sense than does analogWrite(btnleft, INPUT) (etc.).



Edit: INPUT is a constant defined in wiring.h (as in following extract), and is intended for use in pinMode() calls that set an IO pin's mode to one of INPUT, OUTPUT, or INPUT_PULLUP.  It's inept to use it in a call of either digitalWrite() or analogWrite().

    #define HIGH 0x1
    #define LOW  0x0    
    #define INPUT 0x0
    #define OUTPUT 0x1
    #define INPUT_PULLUP 0x2




Note that digitalWrite to an input-mode pin leaves the pin in input-mode, while an analogWrite to an input-mode pin changes the pin to output-mode.

When you want a pin to be input-mode with pullup, it is simpler to use INPUT_PULLUP as the mode parameter, rather than using two separate calls.  That is, instead of saying pinMode(pin, INPUT); followed by digitalWrite(pin, HIGH);, just say

pinMode(pin, INPUT_PULLUP);