#include <Servo.h> Servo myservo; //Motor A int PWMA = 3; //Speed control int AIN1 = 9; //Direction //Motor B int PWMB = 5; //Speed control int BIN1 = 11; //Direction int key ; int item; void setup(){ myservo.attach(13); int pos = 90; item = 0; Serial.begin(115200); pinMode(PWMA, OUTPUT); pinMode(AIN1, OUTPUT); pinMode(PWMB, OUTPUT); pinMode(BIN1, OUTPUT); pinMode(7,INPUT); key=0; } void loop(){ item = analogRead(A1); item = map(item, 0, 1023, 20, 55); Serial.println(item); myservo.write(item); key=digitalRead(7); if(key==1){ move(1, 200, 1); //motor 1, full speed, left move(2, 200, 1); //motor 2, full speed, left delay(1200); //go for 1.2 second } else{ move(1, 0, 1); //motor 1, full speed, left move(2, 0, 1); } } void move(int motor, int speed, int direction){ /*Move specific motor at speed and direction *motor: 0 for B 1 for A *speed: 0 is off, and 255 is full speed *direction: 0 clockwise, 1 counter-clockwise */ boolean inPin1 = LOW; boolean inPin2 = HIGH; if(direction == 1){ inPin1 = HIGH; inPin2 = LOW; } if(motor == 1){ digitalWrite(AIN1, inPin1); analogWrite(PWMA, speed); } else{ digitalWrite(BIN1, inPin1); analogWrite(PWMB, speed); } }