// defines pins numbers
const int trigPin = A0;
const int echoPin = A1;
// defines variables
long duration;
int distance;
#include <moving.h>
#include <Servo.h>
Servo servo;
moving car(6, 2, 3, 5, 4, 7, 11, 8, 9, 10, 12, 13); // setting up a new RC car with pins EN1, IN1, IN2, EN2, IN3, IN4, EN3, IN5, IN6, EN4, IN7, IN8
void setup() {
{
for (int i=2; i<14; i++)
{
pinMode(i, OUTPUT);
}
servo.attach(10);
servo.write(90);
}
{
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
}
void loop() {
if (distance) > 10; // this is the problem
car.set_speed(127);
car.forward();
delay(3000);
if (distance) < 10; // and this
car.set_speed(0);
car.forward();
delay(600);
car.set_speed(127);
car.right();
delay(700);
car.set_speed(127);
car.forward();
delay(700);
}
1 Answer
You need curly brackets around your if block.
if (distance > 10) { // this solves the problem
car.set_speed(127);
car.forward();
delay(3000);
}
etc Edit: Thanks to the commenter.
-
2That is not sufficient. The entire condition must be enclosed in parentheses
if (distance > 10) {
– CraigJun 29, 2018 at 21:41 -
Maybe you could expand upon your answer. Your answer is a little terse. Which statements need to be contained in the
{}
block and why..? Also you need to fix your suggestion as per @Craig's comment. Jun 30, 2018 at 22:05 -
@karlo: The error message refers to line 38, a badly-formed 'if' statement. You probably meant something like "if (distance < 10){ do something; } else { do somethingelse; }", but as written, it is not correct C or C++.– JRobertJul 2, 2018 at 19:47
if (distance > 10)
. What should be inif
condition?;
after it is an empty command. I am sure you do not want to have a condition to execute an empty command. arduino.cc/reference/en/language/structure/control-structure/ifcar.set_speed()
andcar.forward()
are meant to be executed conditionally? Otherwise, what is the point of theif()
statement? Also, is thedelay()
meant to be part of the conditionally executed code? Or not?