Error of AFMotor.h when compiling

Hi. I am new in arduino and I am trying to build a robot with arduino leonardo. I have a motor shield , Aihasd l293d motor drive shield. I download the AFMotor.h library, but when i compile the sketch I have some errors about the library. I don?t know what to do. someone help me?

In file included from c:\users\danie\appdata\local\arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7\avr\include\avr\io.h:99:0,
                 from c:\users\danie\appdata\local\arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7\avr\include\avr\pgmspace.h:90,
                 from C:\Users\danie\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\cores\arduino/Arduino.h:28,
                 from C:\Users\danie\AppData\Local\Temp\arduino\sketches\D7B9A865359F9D7C30AC03C74F983471\sketch\MotorTest4wd.ino.cpp:1:
C:\Users\danie\Documents\Arduino\libraries\Adafruit_Motor_Shield_library/AFMotor.h:46:30: error: 'CS22' was not declared in this scope
     #define MOTOR12_1KHZ _BV(CS22)              // divide by 64
                              ^
C:\Users\danie\Downloads\MotorTest4wd\MotorTest4wd.ino:15:31: note: in expansion of macro 'MOTOR12_1KHZ'
 AF_DCMotor Motor_Left_Rear(1, MOTOR12_1KHZ);    // Motor 1
                               ^~~~~~~~~~~~
C:\Users\danie\Documents\Arduino\libraries\Adafruit_Motor_Shield_library/AFMotor.h:46:30: note: suggested alternative: 'CS12'
     #define MOTOR12_1KHZ _BV(CS22)              // divide by 64
                              ^
C:\Users\danie\Downloads\MotorTest4wd\MotorTest4wd.ino:15:31: note: in expansion of macro 'MOTOR12_1KHZ'
 AF_DCMotor Motor_Left_Rear(1, MOTOR12_1KHZ);    // Motor 1
                               ^~~~~~~~~~~~
C:\Users\danie\Documents\Arduino\libraries\Adafruit_Motor_Shield_library/AFMotor.h:46:30: error: 'CS22' was not declared in this scope
     #define MOTOR12_1KHZ _BV(CS22)              // divide by 64
                              ^
C:\Users\danie\Downloads\MotorTest4wd\MotorTest4wd.ino:16:32: note: in expansion of macro 'MOTOR12_1KHZ'
 AF_DCMotor Motor_Right_Rear(2, MOTOR12_1KHZ);   // Motor 2
                                ^~~~~~~~~~~~
C:\Users\danie\Documents\Arduino\libraries\Adafruit_Motor_Shield_library/AFMotor.h:46:30: note: suggested alternative: 'CS12'
     #define MOTOR12_1KHZ _BV(CS22)              // divide by 64
                              ^
C:\Users\danie\Downloads\MotorTest4wd\MotorTest4wd.ino:16:32: note: in expansion of macro 'MOTOR12_1KHZ'
 AF_DCMotor Motor_Right_Rear(2, MOTOR12_1KHZ);   // Motor 2
                                ^~~~~~~~~~~~

exit status 1

Compilation error: exit status 1

The sketch is

const int LED_PIN = 13;
const int speed = 60;  // percent of maximum speed

#include <AFMotor.h>                            // adafruit motor shield library (modified my mm)
AF_DCMotor Motor_Left_Front(4, MOTOR34_1KHZ);   // Motor 4
AF_DCMotor Motor_Right_Front(3, MOTOR34_1KHZ);  // Motor 3
AF_DCMotor Motor_Left_Rear(1, MOTOR12_1KHZ);    // Motor 1
AF_DCMotor Motor_Right_Rear(2, MOTOR12_1KHZ);   // Motor 2

int pwm;

void setup() {
  Serial.begin(9600);
  blinkNumber(8);  // open port while flashing. Needed for Leonardo only

  // scale percent into pwm range (0-255)
  pwm = map(speed, 0, 100, 0, 255);
  Motor_Left_Front.setSpeed(pwm);
  Motor_Right_Front.setSpeed(pwm);
  Motor_Left_Rear.setSpeed(pwm);
  Motor_Right_Rear.setSpeed(pwm);
}

// run over and over
void loop() {
  Serial.println("rotate cw");
  Motor_Left_Front.run(FORWARD);
  Motor_Left_Rear.run(FORWARD);

  Motor_Right_Front.run(BACKWARD);
  Motor_Right_Rear.run(BACKWARD);

  delay(5000);  // run for 5 seconds
  Serial.println("stopped");
  Motor_Left_Front.run(RELEASE);  // stop the motors
  Motor_Right_Front.run(RELEASE);
  Motor_Left_Rear.run(RELEASE);  // stop the motors
  Motor_Right_Rear.run(RELEASE);

  delay(5000);  // stop for 5 seconds
}

// function to indicate numbers by flashing the built-in LED
void blinkNumber(byte number) {
  pinMode(LED_PIN, OUTPUT);  // enable the LED pin for output
  while (number--) {
    digitalWrite(LED_PIN, HIGH);
    delay(100);
    digitalWrite(LED_PIN, LOW);
    delay(400);
  }
}

[sterretje edit]
Code tags fixed
[sterretje end]

Perhaps this is the problem: This library is old and deprecated - and the hardware disconinued years ago. V2 of the shield uses i2c only and works with anything that has I2C support (e.g. all arduinos) without endless incompatibilities and porting requirements! :slight_smile:
-> Adafruit Motor/Stepper/Servo Shield for Arduino v2 Kit [v2.3] : ID 1438 : Adafruit Industries, Unique & fun DIY electronics and kits.

A cursory look through AFMotor.h (which is from a library that hasn't been updated in over a decade, when the board it supported was discontinued by Adafruit) reveals the following:

#if defined(__AVR__)
.
.
.
    #define DC_MOTOR_PWM_RATE   MOTOR34_8KHZ    // PWM rate for DC motors
.
.
.
#elif defined(__PIC32MX__)
.
.
.
    #define DC_MOTOR_PWM_RATE   MOTOR34_39KHZ
.
.
.
#endif

The library will only work with microcontrollers from the AVR (and based on those errors you're seeing, basically an Uno R3 or a Mega) and PIC32 families. The Leonardo is apparently not supported.

I'd ask "why do people keep buying this obsolete board" but I know what the answer is; it was cheap. Spend your money on the v2 motor shield as recommended by @Paul_KD7HB and get something that will work.

Edit: and here's a topic from the Adafruit forums from 12 years ago where it was discovered that the Leonardo didn't work with the AFMotor library.

I did see an attempt at code tags but it did not work out; I fixed it.

Code tags for a block of code or a block of error messages are 3 back ticks (```) before and after the block; they need to be on their own line.

Thanks for the answers. What do you recommend me to do? Do I buy an Arduino uno to continue the project and will I use the Leonardo for something else, or do I buy a new Motor shield ? If so, do you have any advice? The idea is to build a 4-wheeled robot Which avoids obstacles by itself. and then gradually implement the various sensores.

To make use of the motor shield, you could get an Arduino (I would), or you can wire the Leonardo to the Shield (I would not).

You could also use the Leonardo on its own to control your robot, not using the motor shield, but using supporting devices to drive motors, read sensors, and more.

You will find a good use for both microcontrollers in the shield and out. You could try getting a low cost, "knock off" Uno so you can keep that Uno inside your robot without needing to take it apart to do other work... and use the Leonardo for other work and testing.

You have many good options.