Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Here's an example code for controlling a DC motor using the MX1919 driver and an Arduino:
- This code will rotate both DC motors clockwise at full speed for 2 seconds, stop for 1 second, rotate counterclockwise at half speed for 2 seconds, and stop for 1 second. The cycle will then repeat.
- Connect the MX1919-DIP's IN1 and IN2 inputs for Motor 1 to the Arduino's digital pins 3 and 2, respectively, and connect the IN3 and IN4 inputs for Motor 2 to the Arduino's digital pins 5 and 4, respectively. Make sure to provide an appropriate power supply to the driver's motor voltage and ground pins, and connect the motors to the driver's output terminals (Motor 1 to OUT1 and OUT2, Motor 2 to OUT3 and OUT4).
- */
- // Motor 1
- const int pwmPin1 = 3; // PWM pin connected to the driver's IN1
- const int dirPin1 = 2; // Direction pin connected to the driver's IN2
- // Motor 2
- const int pwmPin2 = 5; // PWM pin connected to the driver's IN3
- const int dirPin2 = 4; // Direction pin connected to the driver's IN4
- void setup() {
- pinMode(pwmPin1, OUTPUT);
- pinMode(dirPin1, OUTPUT);
- pinMode(pwmPin2, OUTPUT);
- pinMode(dirPin2, OUTPUT);
- }
- void loop() {
- // Rotate both motors clockwise at full speed
- digitalWrite(dirPin1, HIGH);
- digitalWrite(dirPin2, HIGH);
- analogWrite(pwmPin1, 255); // 255 is the maximum value for analogWrite (full speed)
- analogWrite(pwmPin2, 255); // 255 is the maximum value for analogWrite (full speed)
- delay(2000); // Run the motors for 2 seconds
- // Stop both motors for 1 second
- analogWrite(pwmPin1, 0);
- analogWrite(pwmPin2, 0);
- delay(1000);
- // Rotate both motors counterclockwise at half speed
- digitalWrite(dirPin1, LOW);
- digitalWrite(dirPin2, LOW);
- analogWrite(pwmPin1, 127); // 127 is half of the maximum value for analogWrite (half speed)
- analogWrite(pwmPin2, 127); // 127 is half of the maximum value for analogWrite (half speed)
- delay(2000); // Run the motors for 2 seconds
- // Stop both motors for 1 second
- analogWrite(pwmPin1, 0);
- analogWrite(pwmPin2, 0);
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement