// Set direction to reverse and accelerate from zero to maximum speed
for (int i = 0; i <= 100; i++)
{
Motor('C', 'R', i);
delay(ACCEL_DELAY);
}
delay (2000);
// Decelerate from maximum speed to zero
for (int i = 100; i >= 0; --i)
{
Motor('C', 'R', i);
delay(ACCEL_DELAY);
}
// Turn off motors
Motor('C', 'F', 0);
delay (3000);
}
/*
* Motor function does all the heavy lifting of controlling the motors
* mot = motor to control either 'A' or 'B'. 'C' controls both motors.
* dir = Direction either 'F'orward or 'R'everse
* speed = Speed. Takes in 1-100 percent and maps to 0-255 for PWM control.
* Mapping ignores speed values that are too low to make the motor turn.
* In this case, anything below 27, but 0 still means 0 to stop the motors.
*/
void Motor(char mot, char dir, int speed)
{
// remap the speed from range 0-100 to 0-255
int newspeed;
if (speed == 0)
newspeed = 0; // Don't remap zero, but remap everything else.
else
newspeed = map(speed, 1, 100, MIN_SPEED, 255);
switch (mot) {
case 'A': // Controlling Motor A
if (dir == 'F') {
digitalWrite(INA, HIGH);
}
else if (dir == 'R') {
digitalWrite(INB, LOW);
}
analogWrite(ENA, newspeed);
break;
case 'B': // Controlling Motor B
if (dir == 'F') {
digitalWrite(INB, HIGH);
}
else if (dir == 'R') {
Need help?
Do you have a question about the Uno and is the answer not in the manual?