0% found this document useful (0 votes)
34 views1 page

Motor Test Code

The document contains an Arduino sketch for controlling two DC motors using the AFMotor library. It initializes the motors, accelerates and decelerates them in both forward and backward directions, and finally releases the motors. The loop function continuously runs this sequence with a delay between speed changes.

Uploaded by

Waqas Salman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views1 page

Motor Test Code

The document contains an Arduino sketch for controlling two DC motors using the AFMotor library. It initializes the motors, accelerates and decelerates them in both forward and backward directions, and finally releases the motors. The loop function continuously runs this sequence with a delay between speed changes.

Uploaded by

Waqas Salman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <AFMotor.

h>
AF_DCMotor motor_1(1);
AF_DCMotor motor_2(2);
void setup()
{
//Set initial speed of the motor & stop
motor_1.setSpeed(300);
motor_1.run(RELEASE);
motor_2.setSpeed(300);
motor_2.run(RELEASE);
}
void loop()
{
uint8_t i;
// Turn on motor
motor_1.run(FORWARD);
motor_2.run(FORWARD);
// Accelerate from zero to maximum speed
for (i=0; i<255; i++)
{
motor_1.setSpeed(i);
motor_2.setSpeed(i);
delay(5);
}
// Decelerate from maximum speed to zero
for (i=255; i!=0; i--)
{
motor_1.setSpeed(i);
motor_2.setSpeed(i);
delay(5);
}
// Now change motor direction
motor_1.run(BACKWARD);
motor_2.run(BACKWARD);
// Accelerate from zero to maximum speed
for (i=0; i<255; i++)
{
motor_1.setSpeed(i);
motor_2.setSpeed(i);
delay(5);
}
// Decelerate from maximum speed to zero
for (i=255; i!=0; i--)
{
motor_1.setSpeed(i);
motor_2.setSpeed(i);

delay(5);
}
// Now turn off motor
motor_1.run(RELEASE);
motor_2.run(RELEASE);

delay(100);
}

You might also like