Can someone take a look at this? My first "If" statement

Whatsup. New on Arduino. Im trying to write a sketch that reads the distance from the ultrasonic sensor and turns on an led depending on the distance.... I get this issue...

IfTestCode:21:1: error: expected unqualified-id before 'if'
if (Distance >= 70) {
^
IfTestCode:26:1: error: expected unqualified-id before 'else'
else {
^
exit status 1
expected unqualified-id before 'if'

Here is my code.....

const int Ledpin = 8;
#include "SR04.h"
#define TRIG_PIN 12
#define ECHO_PIN 11
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long a;

void setup() {
Serial.begin(9600);
delay(1000);
pinMode(Ledpin, OUTPUT);
}

void loop() {
a=sr04.Distance();
Serial.print(a);
Serial.println("cm");
delay(1000);
}

if (Distance >= 70) {
digitalWrite(LedPin, HIGH);

}

else {
digitalWrite(LedPin, LOW);
}

delay(1000);
}

Move the } to the end of your code.

And also change (Distance >= 70) to either (a >= 70), or (sr04.Distance() >= 70)

Distance by itself has not been

  • Been declared anywhere in your code
  • Been assigned a value anywhere in your code

larryd:
delay(1000);
}

Move the } to the end of your code.

Thank you for responding so fast Larry! Would It be possible for you to show me what you mean? I tried what you suggested with no avail. Thanks a bunch!

Show us what you changed.

.

larryd:
Show us what you changed.

.

const int Ledpin = 8;
#include "SR04.h"
#define TRIG_PIN 12
#define ECHO_PIN 11
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long a;

void setup() {
Serial.begin(9600);
delay(1000);
pinMode(Ledpin, OUTPUT);
}

void loop() {
a=sr04.Distance();
Serial.print(a);
Serial.println("cm");
delay(1000);

if (a >= 70) {
digitalWrite(LedPin, HIGH);

}

else {
digitalWrite(LedPin, LOW);
}

The error I receive now is that its not recognizing the second half of the code. Here it is

In function 'void loop()':
IfTestCode:22:16: error: 'LedPin' was not declared in this scope
digitalWrite(LedPin, HIGH);
^
IfTestCode:26:16: error: 'LedPin' was not declared in this scope
digitalWrite(LedPin, LOW);
^
exit status 1
'LedPin' was not declared in this scope

C/C++ is case sensitive. You declared Ledpin and later use LedPin; not the same.

And, from what I can see, you're missing a final closing brace.

The last two lines of your code should be

  }
}
const int Ledpin = 8;
#include "SR04.h"
#define TRIG_PIN 12
#define ECHO_PIN 11
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long a;

void setup() 
{
   Serial.begin(9600);
   delay(1000);
   pinMode(Ledpin, OUTPUT);
}

void loop() 
{
   a = sr04.Distance();
   Serial.print(a);
   Serial.println("cm");
   delay(1000);


if (a >= 70) 
{
  digitalWrite(Ledpin, HIGH);

}

else 
{
  digitalWrite(Ledpin, LOW);
}

}

Awesome. Thank you all! What an amazing community. Looking forward to sticking around here!

Also:

Use CTRL T to format the sketch.

Please use code tags.
Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]

larryd:
Also:

Use CTRL T to format the sketch.

Please use code tags.
Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]

Thanks! Will do