My code
#include <Servo.h>
#define trig 2
#define echo 4
#define right 11
#define front 12
#define left 13
Servo servo;
void setup() {
servo.attach(9);
pinMode (trig, OUTPUT);
pinMode (echo, INPUT);
Serial.begin (9600);
}
void loop() {
//All
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
long t = pulseIn(echo, HIGH);
long inches = t / 74 / 2;
long cm = t / 29 / 2;
servo.write(90);
delay(1000);
if (cm > 10){ // front check
digitalWrite(11, HIGH);
servo.write(90);
digitalWrite(front, HIGH);
}
else{
servo.write(20); // turn to left
delay(100);
}
if (cm > 10);{ //check left
digitalWrite(left, HIGH);
}
else{
servo.write(180); //turn to right
delay(100);
}
if (cm > 10){ // check right
digitalWrite(right, HIGH);
}
else{
digitalWrite(front, HIGH);
digitalWrite(front, LOW);
digitalWrite(front, HIGH);
digitalWrite(front, LOW);
digitalWrite(front, HIGH);
digitalWrite(front, LOW);
digitalWrite(front, HIGH);
digitalWrite(front, LOW);
}
}
Error
exit status 1
'else' without a previous 'if'
What's that?
This is your full error; it indicates the line where it fails.
C:\Users\WIMSTU~1\AppData\Local\Temp\arduino_3431cf36d2090c05fd30f5c5b5b2b264\sketch_oct28a.ino: In function 'void loop()':
sketch_oct28a:50: error: 'else' without a previous 'if'
else{
^
Using library Servo at version 1.1.1 in folder: C:\Program Files (x86)\Arduino\libraries\Servo
exit status 1
'else' without a previous 'if'
The IDE even highlights the line where the error occurs. And the section of the code
if (cm > 10);{ //check left
digitalWrite(left, HIGH);
}
else{
servo.write(180); //turn to right
delay(100);
}
Looking at that part, your if statement ends with a semi-colon.
PS
Please edit your post and change the [quote] and [/quote] to [code] and [/code].
sterretje:
This is your full error; it indicates the line where it fails.
C:\Users\WIMSTU~1\AppData\Local\Temp\arduino_3431cf36d2090c05fd30f5c5b5b2b264\sketch_oct28a.ino: In function 'void loop()':
sketch_oct28a:50: error: 'else' without a previous 'if'
else{
^
Using library Servo at version 1.1.1 in folder: C:\Program Files (x86)\Arduino\libraries\Servo
exit status 1
'else' without a previous 'if'
The IDE even highlights the line where the error occurs. And the section of the code
if (cm > 10);{ //check left
digitalWrite(left, HIGH);
}
else{
servo.write(180); //turn to right
delay(100);
}
Looking at that part, your if statement ends with a semi-colon.
PS
Please edit your post and change the `[quote] and [/quote] to [code] and [/code]`.
ok.
Thanx..
The "if" can only contain one statement. That semicolon counts as your one statement. What the compiler sees is:
if (cm > 10)
;
{ //check left
digitalWrite(left, HIGH);
}
else {
servo.write(180); //turn to right
delay(100);
}