1
// 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);
}
2
  • 4
    if (distance > 10). What should be in if 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/if
    – Juraj
    Jun 29, 2018 at 19:02
  • 1
    @Juraj is 100% correct... Presumably car.set_speed() and car.forward() are meant to be executed conditionally? Otherwise, what is the point of the if() statement? Also, is the delay() meant to be part of the conditionally executed code? Or not? Jun 29, 2018 at 21:17

1 Answer 1

2

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.

3
  • 2
    That is not sufficient. The entire condition must be enclosed in parentheses if (distance > 10) {
    – Craig
    Jun 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++.
    – JRobert
    Jul 2, 2018 at 19:47

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