Why does this #include <AFMotor.h> doesn't work

when I use
#include <AFMotor.h>
it doesn't work
I used it from this videos description

I got the code from this

https://drive.google.com/drive/folders/1yrxMWt89B1cXgT3YPp83pRh4Lho36o2S

motor shield control

I am making a robot for my school project

Hi,

Please post YOUR code in code tags, not an external link.

Please attach a schematic of YOUR circuit, not the tutorial.

Most likely you haven't downloaded the library, but what do you mean by "it doesn't work" Is there an error when you compile? Or does the code not run as expected?

this code

#include <Servo.h>
#include <AFMotor.h>
Servo servo1;
AF_DCMotor M1(1);
AF_DCMotor M2(2);
AF_Stepper motor1(48, 2);

void setup() {
M1.setSpeed(150);
M2.setSpeed(150);
servo1.attach(10);
motor1.setSpeed(10);
}
void loop() {
servo();
//gearmotor();
//stepper();
}

void gearmotor() {
M1.run(FORWARD);
M2.run(FORWARD);
delay(2000);
M1.run(BACKWARD);
M2.run(BACKWARD);
delay(2000);
M1.run(RELEASE);
M2.run(RELEASE);
delay(500);
}
void servo() {
for (int a = 0; a <= 180; a++ ) {
servo1.write(a);
delay(20);
}
for (int b = 179; b > 0; b--) {
servo1.write(b);
delay(20);
}
}
void stepper() {

motor1.step(100, FORWARD, SINGLE);
motor1.step(100, BACKWARD, SINGLE);

motor1.step(100, FORWARD, DOUBLE);
motor1.step(100, BACKWARD, DOUBLE);

motor1.step(100, FORWARD, INTERLEAVE);
motor1.step(100, BACKWARD, INTERLEAVE);

motor1.step(100, FORWARD, MICROSTEP);
motor1.step(100, BACKWARD, MICROSTEP);
}

I asked you to post it in code tags. Please do so.

#include <Servo.h>
#include <AFMotor.h>
Servo servo1;
AF_DCMotor M1(1);
AF_DCMotor M2(2);
AF_Stepper motor1(48, 2);


void setup() {
  M1.setSpeed(150);
  M2.setSpeed(150);
  servo1.attach(10);
  motor1.setSpeed(10);
}
void loop() {
  servo();
  //gearmotor();
  //stepper();
}

void gearmotor() {
  M1.run(FORWARD);
  M2.run(FORWARD);
  delay(2000);
  M1.run(BACKWARD);
  M2.run(BACKWARD);
  delay(2000);
  M1.run(RELEASE);
  M2.run(RELEASE);
  delay(500);
}
void servo() {
  for (int a = 0; a <= 180; a++ ) {
    servo1.write(a);
    delay(20);
  }
  for (int b = 179; b > 0; b--) {
    servo1.write(b);
    delay(20);
  }
}
void stepper() {

  motor1.step(100, FORWARD, SINGLE);
  motor1.step(100, BACKWARD, SINGLE);

  motor1.step(100, FORWARD, DOUBLE);
  motor1.step(100, BACKWARD, DOUBLE);

  motor1.step(100, FORWARD, INTERLEAVE);
  motor1.step(100, BACKWARD, INTERLEAVE);

  motor1.step(100, FORWARD, MICROSTEP);
  motor1.step(100, BACKWARD, MICROSTEP);
}

2 Likes

like this ?

1 Like

Awesome. Thank you.

Just waiting for these now. :grinning: :wink:

KiCAD is great for schematics, Fritzing works at a pinch.

both

Both what? Please elaborate. If you have an error on compilation, please post in code tags

I have an error

I looked at the drawings you posted, and the square 9V PP3 battery won’t do much for very long.

Suggest a real power supply, or battery pack to supply the required voltage & current.

1 Like

Than In that case post that error; use code tags again.

Did you install the AFMotor library?

1 Like

@acodechess-legend_123
Download the master ZIP file in your link in Post #1.
IDE >> SKETCH >> INCLUDE LIBRARY >> ADD ZIP LIBRARY >> (find AFMotor ZIP file) >> OPEN

Try again to compile and run your program.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.