Paso 7: código de
int DIST; //distance//motor A connected between A01 and A02 //motor B connected between B01 and B02int STBY = 10; //standby//Motor A int PWMA = 3; //Speed control int AIN1 = 9; //Direction int AIN2 = 8; //Direction//Motor B int PWMB = 5; //Speed control int BIN1 = 11; //Direction int BIN2 = 12; //Directionvoid setup(){ pinMode(STBY, OUTPUT); pinMode(PWMA, OUTPUT); pinMode(AIN1, OUTPUT); pinMode(AIN2, OUTPUT); pinMode(PWMB, OUTPUT); pinMode(BIN1, OUTPUT); pinMode(BIN2, OUTPUT); }void loop(){ // read the input on analog pin 0: int sensorValue = analogRead(A0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V): float DIST = sensorValue * (5.0 / 1023.0); if(DIST < 2) { move(1, 0, 1); //direction move(2, 255, 1); //full speed front } if(DIST > 2) { move(1, 150, 1); //direction: right move(2, 200, 0); // back } }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 digitalWrite(STBY, HIGH); //disable standby boolean inPin1 = LOW; boolean inPin2 = HIGH; if(direction == 1){ inPin1 = HIGH; inPin2 = LOW; } if(motor == 1){ digitalWrite(AIN1, inPin1); digitalWrite(AIN2, inPin2); analogWrite(PWMA, speed); }else{ digitalWrite(BIN1, inPin1); digitalWrite(BIN2, inPin2); analogWrite(PWMB, speed); } }void stop(){ //enable standby digitalWrite(STBY, LOW); }