Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Idea:
- Use direct port/pin control:
- #define MOTOR1_PIN1 9
- #define MOTOR1_PIN2 10
- #define MOTOR2_PIN1 11
- #define MOTOR2_PIN2 12
- void setup() {
- // Other stuff
- pinMode(MOTOR1_PIN1, OUTPUT);
- pinMode(MOTOR1_PIN2, OUTPUT);
- pinMode(MOTOR2_PIN1, OUTPUT);
- pinMode(MOTOR2_PIN2, OUTPUT);
- // More stuff
- }
- void loop() {
- int motor1Speed = // data
- int motor2Speed = // data
- controlMotor1(motor1Speed);
- controlMotor2(motor2Speed);
- delay(40);
- }
- // Create two new functions to handle motor control
- void controlMotor1(int speed) {
- // Set the direction of motor 1 based on the speed
- if (speed >= 0) {
- digitalWrite(MOTOR1_PIN1, HIGH);
- digitalWrite(MOTOR1_PIN2, LOW);
- } else {
- digitalWrite(MOTOR1_PIN1, LOW);
- digitalWrite(MOTOR1_PIN2, HIGH);
- speed = -speed; // Make the speed positive for analogWrite
- }
- analogWrite(MOTOR1_PIN2, speed);
- }
- void controlMotor2(int speed) {
- if (speed >= 0) {
- digitalWrite(MOTOR2_PIN1, HIGH);
- digitalWrite(MOTOR2_PIN2, LOW);
- } else {
- digitalWrite(MOTOR2_PIN1, LOW);
- digitalWrite(MOTOR2_PIN2, HIGH);
- speed = -speed; // Make the speed positive for analogWrite
- }
- analogWrite(MOTOR2_PIN2, speed);
- }
- // Rest of program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement