Problem with calling void function. [SOLVED]

Hi, i write to you because i have programming problem. I have Arduino for 2 months so I am beginner.
Im from Poland and i hope you'll understand what I mean :slight_smile:

I'm using Motor Shield and i'm going to connect Ultrasonic sensor. In my program i've set distance 100 cm. I want engine stop if distance is less than 20 cm.
This is my code:

Here

OR

#include <AFMotor.h> //bibloteka silnika

AF_DCMotor motor(1); // create motor



void setup ()
{

  Serial.begin(9600);
  Serial.println("Testing!");
  motor.setSpeed(100);
  Serial.println("Speed:100");
}

void loop ()
{

  [b]void stopEngine(); [/b]//how to stop the engine?
  {
    motor.run(RELEASE);
    Serial.println("Engine STOP!");
    delay(1000);
  }


  motor.run(FORWARD); //turn engine on
  Serial.println("Engine ON!");

  int distance = 100;

  if (distance<20) //distance check
  {
    stopEngine();
    delay(2000);
  }


  delay(100);

  Serial.println("New Loop!");
}

All i got IS:
Testing!
Speed:100
Engine STOP!
Engine ON!
New Loop!
Engine STOP!
Engine ON!

etc...

Im wondering why void stopEngine(); is ALWAYS working, independly from "if" function? I want to use it when it's called!

P.S
It didn't compile when stopEngine() function was after if function

Please help :)!

Functions must be defined outside the loop() function, and there is no ; after the name of the function.

Once it is defined it can be called when you use it's name with a ;

You have a couple of problems.

  1. You need to move the void stopEngine() function outside of the void loop() function, and remove the semicolon and the end of the line.
  void stopEngine(); //how to stop the engine?
  {
    motor.run(RELEASE);
    Serial.println("Engine STOP!");
    delay(1000);
  }

because the semicolon is there, it is terminating the function definition, and the lines between braces are considered as a block of code that is part of loop().

  1. You have this...
  int distance = 100;

  if (distance<20) //distance check

You set distance to 100, then check if it is less than 20. Obviously, it never will be, until you connect up the ultrasonic sensor and get readings from it.

Furthermore, you set 'distance' to 100 just before you test for <20:

  int distance = 100;

  if (distance<20) //distance check
  {
    stopEngine();
    delay(2000);
  }

There nothing in your code that changes/updated 'distance', so the test always fails.

Thank you for fast replays!

I moved

void stopEngine() //how to stop the engine?
  {
    motor.run(RELEASE);
    Serial.println("Engine STOP!");
    delay(1000);
  }

outside from loop function, declared it in void setup and it's no longer starting without my will :slight_smile:

i specially used 100cm distance value to force "always on" engine mode :wink:

Thanks once again!

I'm glad it's working, but it should be outside both setup() and loop().

void stopEngine() //how to stop the engine?
  {
    motor.run(RELEASE);
    Serial.println("Engine STOP!");
    delay(1000);
  }

void setup()
{
  // setup code
}

void loop()
{
  // loop code
}

Misunderstand, by "declared it in void setup " i meant put declaration like "void stopEngine();" Without it it isn't working:)

Thanks for post.