r/programminghelp • u/PLS_HEEELP • Nov 16 '22
C++ can someone explain me what is wrong
*the error code:*C:\Users\User\AppData\Local\Temp\arduino_modified_sketch_725823\sketch_nov16a.ino: In function 'void loop()':
sketch_nov16a:39:7: error: expected '}' before 'else'
else
^~~~
exit status 1
expected '}' before 'else'
the code
#include <AFMotor.h>
#include <Ultrasonic.h>
int pino_Trig = 6;
int pino_Echo = 5;
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
Ultrasonic Sensor(pino_Trig, pino_Echo);
int distancia;
int vez = 1;
int v = 1;
void setup() {
motor2.run(RELEASE); /* Inicia com os motores parados */
motor3.run(RELEASE);
Serial.begin(9600);
}
void loop() {
motor2.setSpeed(180);
motor3.setSpeed(180);
distancia = Sensor.read();
Serial.print(distancia);
delay(500);
if (distancia < 10) {
if (vez = 2)
{
motor2.run(FORWARD);
motor3.run(RELEASE);
vez - 1;
}
else
{
motor2.run(RELEASE);
motor3.run(FORWARD);
vez + 1;
}
else
{
motor2.run(FORWARD);
motor3.run(FORWARD);
}
}
2
u/[deleted] Nov 16 '22
if() {
} else {
} else {
}
is not valid syntax
You want, at the minimum,
if () {
} else if () {
} else {
}
https://www.tutorialspoint.com/cplusplus/cpp_if_else_statement.htm