I cant seem to find this error is:

elevator_bug_2.ino:38:1: error: expected unqualified-id before ‘if’

Here is the code:

// these constants won't change. They are the
// lowest and highest readings you get from your sensor:
const int sensorMin = 0;      // sensor minimum, discovered through experiment
const int sensorMax = 600;    // sensor maximum, discovered through experiment

void setup() {
  // initialize serial communication:

void loop() {
  // read the sensor:
  int sensorReading = analogRead(A0);
  // map the sensor range to a range of four options:
  int range = map(sensorReading, sensorMin, sensorMax, 0, 3);

  // do something different depending on the
  // range value:
  switch (range) {
    case 0:    // your hand is on the sensor
    case 1:    // your hand is close to the sensor
    case 2:    // your hand is a few inches from the sensor
    case 3:    // your hand is nowhere near the sensor
  delay(1);        // delay in between reads for stability

if (Serial.println("dark") == true)   // LINE 38

int led = 9;           // the PWM pin the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup() {
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);

// the loop routine runs over and over again forever:
void loop() {
  // set the brightness of pin 9:
  analogWrite(led, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  // wait for 30 milliseconds to see the dimming effect
  • Check where the function loop() ends. Seems likes there is a } too many or a function declaration is missing. Jan 10 '17 at 0:04

C is procedural language and code is compiled. You cannot put code beyond procedure as you try to do in mentioned if. Remaining code following this point is nonsense. Do you mean you can put second loop() based on if condition ? I recommend to start learning programming C before using Arduino. In C you can do such a idea using preprocessor directives IF, ELSE, ENDIF.


As Tma mentioned earlier, C is a procedural language. That means that all the code must be part of a defined function. So in your code you have the functions void loop() and void setup(). Wich are two functions you must always have in an arduino sketch, but you can also define others. What you cannot do is having two functions with the same name and type in C like you have there, because there is no way to tell them apart from each other. You have two functions called loop and setup...

One basic thing about Arduino code that you must know is the way it runs your code. Every arduinos sketch must have a setup and a loop function. Then when you start the program, it wil first execute the code in your setup function ONCE and then start at your loop function. The name of the loop function already indicates that it will LOOP all the code inside it, so when your program reaches the end of loop() is starts again at the top of loop(). Your program will never reach any point below loop, because you need to specifically call a function to redirect your program to another point. I think the best way to learn Arduino is to study some C first.


I'm no expert but when asking "if" I want you to do this. There should be an "else" I want you to do this.

For example: I ask you if the door is open, then close it. But what if the door is already closed, what do you do now? That's why an "if" needs an "else".

"If" the door is open, then close it. "Else" do nothing.

  • An if doesn't need an else. “If the door is open, please close it” is a complete sentence in English. if (door.is_open()) door.close(); is a complete statement in C++. Feb 2 '18 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy