lundi 27 avril 2020

Arduino : Motor Speed Control With L293D and Joystick

Contrôler la vitesse d'un moteur avec le circuit L293D et Joystick





Schéma du Montage : (Version avec deux moteurs)





Photos :



Code :

//https://bou-tarek-my-arduino.blogspot.com/
//Inspiré de : https://electropeak.com/learn/l293d-control-drive-stepper-servo-dc-motors-by-arduino/


#define MOTOR_EN_1_2  10
#define MOTOR_IN1     9
#define MOTOR_IN2     8

#define MOTOR_EN_3_4  11
#define MOTOR_IN3     12
#define MOTOR_IN4     13

#define slow 64
#define normal 128
#define fast 255

// Joystick pin numbers
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = A0; // analog pin connected to X output
const int Y_pin = A1; // analog pin connected to Y output

void setup() {
  Serial.begin(9600);
  Serial.println("L293D DC motor");
  
  pinMode(MOTOR_EN_1_2, OUTPUT);
  pinMode(MOTOR_IN1, OUTPUT);
  pinMode(MOTOR_IN2, OUTPUT);
  digitalWrite(MOTOR_IN1, HIGH);
  digitalWrite(MOTOR_IN2, LOW);

  pinMode(MOTOR_EN_3_4, OUTPUT);
  pinMode(MOTOR_IN3, OUTPUT);
  pinMode(MOTOR_IN4, OUTPUT);
  
  //Joystick
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);

}

void loop() {
  int joystick = analogRead(X_pin);
  int vitesse;
  if (joystick < 510 ){
    Serial.print("< ");
    vitesse = map(joystick, 0, 512,255, 0);
    analogWrite(MOTOR_EN_1_2, vitesse);
    digitalWrite(MOTOR_IN1, HIGH);
    digitalWrite(MOTOR_IN2, LOW);
  }else if (joystick >520 ){
     Serial.print("> ");
     vitesse = map(joystick, 500, 1020, 0, 255);
     analogWrite(MOTOR_EN_1_2, vitesse);
     digitalWrite(MOTOR_IN1, LOW);
     digitalWrite(MOTOR_IN2, HIGH);
  } else {
    Serial.print("= ");
    vitesse = 0;
    analogWrite(MOTOR_EN_1_2, vitesse);
    digitalWrite(MOTOR_IN1, HIGH);
    digitalWrite(MOTOR_IN2, HIGH);
  }
  Serial.print("Motor 1 : ");
  Serial.println(vitesse);
  joystick = analogRead(Y_pin);
  if (joystick < 510 ){
    Serial.print("< ");
    vitesse = map(joystick, 0, 512,255, 0);
    analogWrite(MOTOR_EN_3_4, vitesse);
    digitalWrite(MOTOR_IN3, HIGH);
    digitalWrite(MOTOR_IN4, LOW);
  }else if (joystick >520 ){
     Serial.print("> ");
     vitesse = map(joystick, 520, 1020, 0, 255);
     analogWrite(MOTOR_EN_3_4, vitesse);
     digitalWrite(MOTOR_IN3, LOW);
     digitalWrite(MOTOR_IN4, HIGH);
  } else {
    Serial.print("= ");
    vitesse = 0;
    analogWrite(MOTOR_EN_3_4, vitesse);
    digitalWrite(MOTOR_IN3, HIGH);
    digitalWrite(MOTOR_IN4, HIGH);
  }
  Serial.print("Motor 2 : ");
  Serial.println(vitesse);
}


Aucun commentaire:

Enregistrer un commentaire