user46450: This is my Sketch or code:
const int EchoPin = 5;
const int TriggerPin = 6;
const int LedPin = 13; 

void setup() {
 Serial.begin(9600);
 pinMode(LedPin, OUTPUT);
 pinMode(TriggerPin, OUTPUT);
 pinMode(EchoPin, INPUT);
 pinMode(13, OUTPUT);
}

void loop() {
 int cm = ping(TriggerPin, EchoPin);
 Serial.print("Distancia: ");
 Serial.println(cm);
 delay(1000);
}

int ping(int TriggerPin, int EchoPin) {
 long duration, distanceCm;
 digitalWrite(TriggerPin, LOW);
 delayMicroseconds(4);
 digitalWrite(TriggerPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(TriggerPin, LOW);
 duration = pulseIn(EchoPin, HIGH);
 distanceCm = duration * 10 / 292/ 2;
 return distanceCm;
}

if (distanceCm <= 20) {
 digitalWrite(13, HIGH);
}

if (distanceCm >= 20) {
 digitalWrite(13, LOW);
}

The error appears in the second if.

user37523: The error is occurring because you have 2 if statements outside of a function. You could move them into the ping() function which would fix the error but there seems to be a logic error with the 2 if statements as well.
If distanceCm is 20, both if statements execute one right after another, so I doubt you'll see the LED turn on. Perhaps an if/else might work.
if(distanceCm < 21){
  digitalWrite(13, HIGH);
}
else{
  digitalWrite(13, LOW);
}


user46448: Error is because you close your ping method  before the "if (distanceCm >= 20)" clausule. This is a correct code:
const int EchoPin = 5;
const int TriggerPin = 6;
const int LedPin = 13; 

void setup() {
 Serial.begin(9600);
 pinMode(LedPin, OUTPUT);
 pinMode(TriggerPin, OUTPUT);
 pinMode(EchoPin, INPUT);
 pinMode(13, OUTPUT);
}

void loop() {
 int cm = ping(TriggerPin, EchoPin);
 Serial.print("Distancia: ");
 Serial.println(cm);
 delay(1000);
}

int ping(int TriggerPin, int EchoPin) {
 long duration, distanceCm;
 digitalWrite(TriggerPin, LOW);
 delayMicroseconds(4);
 digitalWrite(TriggerPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(TriggerPin, LOW);
 duration = pulseIn(EchoPin, HIGH);
 distanceCm = duration * 10 / 292/ 2;
  if (distanceCm <= 20) {
 digitalWrite(13, HIGH);
}

if (distanceCm >= 20) {
 digitalWrite(13, LOW);
}
 return distanceCm;
}


46450: <p>This is my Sketch or code:</p>

<pre><code>const int EchoPin = 5;
const int TriggerPin = 6;
const int LedPin = 13; 

void setup() {
 Serial.begin(9600);
 pinMode(LedPin, OUTPUT);
 pinMode(TriggerPin, OUTPUT);
 pinMode(EchoPin, INPUT);
 pinMode(13, OUTPUT);
}

void loop() {
 int cm = ping(TriggerPin, EchoPin);
 Serial.print("Distancia: ");
 Serial.println(cm);
 delay(1000);
}

int ping(int TriggerPin, int EchoPin) {
 long duration, distanceCm;
 digitalWrite(TriggerPin, LOW);
 delayMicroseconds(4);
 digitalWrite(TriggerPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(TriggerPin, LOW);
 duration = pulseIn(EchoPin, HIGH);
 distanceCm = duration * 10 / 292/ 2;
 return distanceCm;
}

if (distanceCm &lt;= 20) {
 digitalWrite(13, HIGH);
}

if (distanceCm &gt;= 20) {
 digitalWrite(13, LOW);
}
</code></pre>

<p>The error appears in the second if.</p>
 37523: <p>The error is occurring because you have 2 <strong>if</strong> statements outside of a function. You could move them into the <strong>ping()</strong> function which would fix the error but there seems to be a logic error with the 2 if statements as well.</p>

<p>If <strong>distanceCm</strong> is 20, both if statements execute one right after another, so I doubt you'll see the LED turn on. Perhaps an <strong>if/else</strong> might work.</p>

<pre><code>if(distanceCm &lt; 21){
  digitalWrite(13, HIGH);
}
else{
  digitalWrite(13, LOW);
}
</code></pre>
 46448: <p>Error is because you close your ping method  before the "if (distanceCm >= 20)" clausule. This is a correct code:</p>

<pre><code>const int EchoPin = 5;
const int TriggerPin = 6;
const int LedPin = 13; 

void setup() {
 Serial.begin(9600);
 pinMode(LedPin, OUTPUT);
 pinMode(TriggerPin, OUTPUT);
 pinMode(EchoPin, INPUT);
 pinMode(13, OUTPUT);
}

void loop() {
 int cm = ping(TriggerPin, EchoPin);
 Serial.print("Distancia: ");
 Serial.println(cm);
 delay(1000);
}

int ping(int TriggerPin, int EchoPin) {
 long duration, distanceCm;
 digitalWrite(TriggerPin, LOW);
 delayMicroseconds(4);
 digitalWrite(TriggerPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(TriggerPin, LOW);
 duration = pulseIn(EchoPin, HIGH);
 distanceCm = duration * 10 / 292/ 2;
  if (distanceCm &lt;= 20) {
 digitalWrite(13, HIGH);
}

if (distanceCm &gt;= 20) {
 digitalWrite(13, LOW);
}
 return distanceCm;
}
</code></pre>
