WHEN THE CODE IN COLO IDE MAKES MISTAKES

Hello, I'm Eduardo from Brazil / Sao Paulo

And many times, when I copy a code and any lap in the ARDUINO IDE, gives several errors, but the code is correct, you know why this happens?

What errors?

Here is the code I am referring to, He is to trigger a PIR detector with LED

#define pir A0
#define led 11
pinMode(led,OUTPUT);
void setup() {

pinMode(pir, INPUT);
pinMode(led, OUTPUT);
Serial.begin(9600);

}

void loop() {

digitalWrite(led,
digitalRead(pir));
delay(100);

Serial.print(i);
Serial.print(" / ");
Serial.println(l);
analogWrite(led, l);

delay(100);
}

if (pirsensor <10)
digitalWrite(pirsensor,HIGH);
else
digitalWrite(buzzer,LOW);
if (distanciaCentimetros<10)
digitalWrite(led,HIGH);
else
digitalWrite(led,LOW);
delay(1000);

}

sketch_aug16a___LED_E_PIR:6: error: expected constructor, destructor, or type conversion before '(' token
sketch_aug16a___LED_E_PIR.ino: In function 'void loop()':
sketch_aug16a___LED_E_PIR:23: error: 'i' was not declared in this scope
sketch_aug16a___LED_E_PIR:25: error: 'l' was not declared in this scope
sketch_aug16a___LED_E_PIR.ino: At global scope:
sketch_aug16a___LED_E_PIR:31: error: expected unqualified-id before 'if'
sketch_aug16a___LED_E_PIR:33: error: expected unqualified-id before 'else'
sketch_aug16a___LED_E_PIR:35: error: expected unqualified-id before 'if'
sketch_aug16a___LED_E_PIR:37: error: expected unqualified-id before 'else'
sketch_aug16a___LED_E_PIR:39: error: expected constructor, destructor, or type conversion before '(' token
sketch_aug16a___LED_E_PIR:41: error: expected declaration before '}' token

digitalRead(pir));

Sticks out like a sore whatsit.

All code must be inside of setup() and loop():

pinMode(led,OUTPUT);                        

void setup() {

should be:

void setup() {

pinMode(led,OUTPUT);

Should probably be inside of loop():

if (pirsensor <10)

digitalWrite(pirsensor,HIGH);   
    else
    digitalWrite(buzzer,LOW);
    if (distanciaCentimetros<10)
    digitalWrite(led,HIGH);
    else
    digitalWrite(led,LOW);
  delay(1000);
 
}

  Serial.print(i);

The variable “i” isn’t declared or used anywhere.

Line 3 of the code contains a valid commandpinMode(led,OUTPUT);unfortunately it is not valid where it is and belongs in a function, probably setup()

if (pirsensor <10)
     digitalWrite(pirsensor,HIGH);     
     else
     digitalWrite(buzzer,LOW);
     if (distanciaCentimetros<10)
     digitalWrite(led,HIGH);
     else
     digitalWrite(led,LOW);
  delay(1000);

Read up on the syntax of if/else. In particular the use of curly brackets.

Thank you all, I will go back and see when calmly, congratulations willingly embrace

miragempro:
Thank you all, I will go back and see when calmly, congratulations willingly embrace

Translate of Google wonderous object being is not yes?

Greatest is features unused

Unused caps lock makes me happy.

I took another code from another tutorial and it worked, sometimes this error happens, some say it is the IDE ARDUINO THAT 'bad, but thank you and sorry for the google translator, maybe one day, in the world, not all pdoeremos communicate trough Esperanto. hug