0
#include <AFMotor.h> 

AF_DCMotor motor(2,MOTOR12_64KHZ); 

void movefor ();

void moveback();


    void setup() 

    {



    Serial.begin(9600); 

    Serial.println("Motor test!");` `motor.setSpeed(200); 

    movefor ();
    moveback ();
    }

        void movefor( ){
        motor.run(FORWARD); 
    delay(1000); 
    motor.run(RELEASE); 
     delay(100);
    }

    void moveback( ) {
      motor.run(BACKWARD);
    delay(1000);  
    motor.run(RELEASE); 
     delay(100);
    }

While trying to compile this the error message is error compiling for board Arduino Uno

I want the functions outside loop so removed it.

Even if I keep void loop and leave it blank then my motors will like run forward, backward and forward afterwards continuously .

  • 1
    you get an linker error because the main function in core main.c calls the loop() function. it must exist. – Juraj Feb 12 at 9:35
  • Error is " cannot compile for the board Arduino Uno" – BJKShah Feb 12 at 9:47
  • turn on verbose mode in Preferences – Juraj Feb 12 at 9:50
1

You can't, and shouldn't, remove the loop function. Arduino sketches must have a setup function and a loop function. The setup function is called once and the loop function is called repeatedly.

Just make your loop function look like this:

void loop() {
    movefor ();
    moveback ();
}

Your motor will move forward, then move backwards, forever.

Note that you should read up on how to avoid using the delay function. You really need to use millis() to do your timing rather than delays. Do an internet search on "Arduino blink no delay." That's basic Arduino programming.

0

There must be something wrong within the motor, circuit or the motor class.

What happens is when you use a setup and loop is the following:

setup();
while (1) { loop(); }

So the loop function is executed forever, if it is empty, nothing will happen. If you say that the motor runs forward, it is probably because of a wrong implementation of the motor class.

Arduino IDE dictates there is always a loop, and having an empty loop is the same as having no loop at all.

What you can try is:

  • In the setup, only execute movefor() and check what happens
  • Than only execute moveback() and check what happens
  • Than make combinations and see if the problem is inside movefor or moveback

For the first steps you can add a delay to easier see if there is a side effect.

  • I have tried what you said and code is working good . I want forward motion then backward. After this I want the same to be continuously moving forward and backward. But the problem that occurs is that it will always move forward after 1 forward and backward motion. I think the loop is being continuously called and that is disturbing that's why I want to remove the loop. Maybe the problem is in header file but how can I check this file? – BJKShah Feb 12 at 9:31
  • you should get what you want when adding in setup: while(1) { movefor(); moveback(); } but I doubt that will work either. The header file can be found where you installed the library, you always can do a file search on your PC (crude way but it works; I don't know by head where the libraries are stored). – Michel Keijzers Feb 12 at 9:42
  • I have the AFMotor library in Documents-Arduino-libraries-AFMotor , everything looks fine here – BJKShah Feb 12 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.