*
Robotix 2019



Robot à ultrasons
Il évite les obstacles



     HC-SR04 détecteur utrasons (40 Khz)


Connexions sur Arduino :
Vcc : 5V
Trig : borne digitale 2
Echo : borne digitale 3
Gnd : masse (GND)


Correspondance des connexions du L293D avec Arduino UNO

  • Téléchargez le code ici





  • /* Utilisation du capteur Ultrason HC-SR04 */

    // motor
    int motor1_enable = 11; //pwm
    int motor1_in1Pin = 12;
    int motor1_in2Pin = 13;

    int motor2_enable = 10; //pwm
    int motor2_in1Pin = 8;
    int motor2_in2Pin = 9;

    int vel = 255;

    // sonar
    int trig = 2;
    int echo = 3;
    long lecture_echo;
    long cm;

    int tst1 = 11;
    int tst2 = 12;

    void setup()
    {
    pinMode(tst1, OUTPUT);
    pinMode(tst2, OUTPUT);
    pinMode(trig, OUTPUT);
    digitalWrite(trig, LOW);
    pinMode(echo, INPUT);
    Serial.begin(9600);
    }

    void loop()
    {
    sonar();

    if (cm > 20){fwd(vel,vel);}

    if (cm < 20) {
    lft(150,150); delay(300);
    }

    if (cm > 55) {stall();}

    delay(100);
    }

    void sonar(){
    digitalWrite(trig, HIGH);
    delayMicroseconds(10);
    digitalWrite(trig, LOW);
    lecture_echo = pulseIn(echo, HIGH);
    cm = lecture_echo / 58;
    Serial.print("Distance cm : ");
    Serial.println(cm);
    delay(100);
    }

    //////// functions //////////

    void fwd(int spd1, int spd2){motors(1,0,0,1,spd1,spd2);}

    void bkw(int spd1, int spd2){motors(0,1,1,0,spd1,spd2);}

    void lft(int spd1, int spd2){motors(0,1,0,1,spd1,spd2);}

    void rgt(int spd1, int spd2){motors(1,0,1,0,spd1,spd2);}

    void stall(){motors(0,0,0,0,0,0);}

    void motors(int MA1,int MA2,int MB1,int MB2,int spd1,int spd2){
    analogWrite(motor1_enable, spd1);
    analogWrite(motor2_enable, spd2);
    digitalWrite(motor1_in1Pin, MA1);
    digitalWrite(motor1_in2Pin, MA2);
    digitalWrite(motor2_in1Pin, MB1);
    digitalWrite(motor2_in2Pin, MB2);
    }




    )