Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.5 kB
1
Indexable
Never
#include <AFMotor.h>

// Create motor object for motor 1-2
AF_DCMotor motor1(1, MOTOR12_1KHZ); // Motor 1 on M1 port with 1KHz PWM frequency
AF_DCMotor motor2(2, MOTOR12_1KHZ); // Motor 2 on M1 port with 1KHz PWM frequency

void setup() {
    Serial.begin(9600); // Initialize serial communication at 9600 bps
    motor1.setSpeed(125); // Set speed to 200 (out of 255)
    motor2.setSpeed(125); // Set speed to 200 (out of 255)
    
}

void loop() {
    Serial.println("Motor running forward");
    motor1.run(FORWARD); // Run motor 1 forward
    motor2.run(FORWARD); // Run motor 1 forward
    delay(random(1, 11)); // Run for 5 second

    Serial.println("Motor stopped");
    motor1.run(RELEASE); // Stop the motor
    motor2.run(RELEASE); // Stop the motor
    delay(1000); // Wait for 1 second    

    Serial.println("Motor running backward");
    motor1.run(BACKWARD); // Run motor 1 backward
    motor2.run(BACKWARD); // Run motor 1 backward
    delay(3000); // Run for 1 second

    Serial.println("Motor stopped");
    motor1.run(RELEASE); // Stop the motor
    motor2.run(RELEASE); // Stop the motor
    delay(1000); // Wait for 1 second

    motor1.setSpeed(150); // Set speed to 200 (out of 255) // 
    motor1.run(FORWARD); // Run motor 1 forward
    delay(2900); // Wait for 1 second     
    motor1.run(RELEASE); // Stop the motor
    motor1.setSpeed(125); // Set speed to 200 (out of 255)




}
Leave a Comment