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
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:
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().
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.