Arduino NEVER able to compile this project

I have been working with Arduino for about 2 weeks now, and have successfully created and compiled many programs. However, this is one is different. I’ve tried different machines, deleting the folder with the project and creating a new one, and just simply waiting, but this program refuses to compile at all. The progress bar gets stuck at about one quarter, and this is the only code I have written that does this. Any help would be greatly appreciated, and while it’s here, any critiques of the code would also be wonderful!

My code take input from a photocell and if the light level reaches a certain threshold, it activates a servo:

// This code will be driving a human representation that detects light. If it is dark, the "human" would react, and if it is dark then suddenly light, the "human" will use servo 1 to move an "arm" up and servo 2 to move a "head" away.


#include <Servo.h>

int LDR = 1;
int LDRval = 0;
int LightThreshold = 150;
bool light = true;

Servo hobbyServo1; // Initialize our servos
int pos1 = 0;      // Declare a variable to store the position of each servo

Servo hobbyServo3;
int pos3 = 0;

void setup() {
  
  Serial.begin(9600); // Establish a com with the computer serial
  
  hobbyServo1.attach(10);
  hobbyServo2.attach(11);
  
  hobbyServo1.write(90); // Set the servos to their initial position
  hobbyServo2.write(1); 

}


void loop() {
  
  int LDRval = analogRead(LDR);// Read the sensor input
  Serial.println(LDRval);
  delay(50);
  
  if (LDR < LightThreshold){
  
    Serial.println("IT'S DARK! ", LDRval); 
    light = false;
    delay(50)
  }
 
  else if (bool light == false && LDR >= LightThreshold) { // This function acts if it is dark and suddenly the photocell detects light above the threshold (i.e. if it was dark and a light turned on, you would be briefly blinded.)
  
    Serial.println("THE LIGHT IS DAZZLING!", LDRval); 
    
    
    hobbyServo3.write(1);
    delay(500);
      
    
    hobbyServo1.write(179);
    delay(500)
      
    
    
    bool light = true;
    delay(50)
    
  }
  
  else if (bool light == true && LDR >= LightThreshold){ // If this loop is executed, the light level is normal and no response in generated
  
    Serial.println(LDval);
    delay(50)
    
  }
  
}

Thanks!

Well for a start I see that many of your delay()s don't have a ; on the end and that you create servos 1 and 3 then use servos 1, 2 and 3.

  else if (bool light == false && LDR >= LightThreshold) {

You are declaring the variable 'light' as a boolean in the if statement. First of all, this does not initialize it - it will have whatever value was on the stack at the time. Odds are it will be non-zero and therefore true and the conditional will almost always fail. Secondly, changing the value of 'light' will have no effect because it will disappear as soon as the program is outside the scope of the if statement.
Declare 'light' to be a global and initialize it properly.

Pete

Serial.println("IT'S DARK! ", LDRval);

What's with the value after the comma? I doubt it means what you think it means. If you want to print two things you'll need to use two statements.

if (LDR < LightThreshold){

1 is always less than 150

hi here had a look thought your code theirs a few things that is coursing the error.

// This code will be driving a human representation that detects light. If it is dark, the "human" would react, and if it is dark then suddenly light, the "human" will use servo 1 to move an "arm" up and servo 2 to move a "head" away.


#include <Servo.h>

int LDR = 1;
int LDRval = 0;
int LightThreshold = 150;
bool light = true;

Servo hobbyServo1; // Initialize our servos
int pos1 = 0;      // Declare a variable to store the position of each servo

Servo hobbyServo2; //you missed out hobbyServo2 complely
int pos2 = 0;

Servo hobbyServo3;
int pos3 = 0;

void setup() {
  
  Serial.begin(9600); // Establish a com with the computer serial
  
  hobbyServo1.attach(10);
  hobbyServo2.attach(11);
  
  hobbyServo1.write(90); // Set the servos to their initial position
  hobbyServo2.write(1); 

}


void loop() {
  
  int LDRval = analogRead(LDR);// Read the sensor input
  Serial.println(LDRval);
  delay(50);
  
  if (LDR < LightThreshold){
  
    Serial.print("IT'S DARK! "); Serial.println(LDRval); //<<need to separate
    light = false;
    delay(50); //< here you missed ;
  }
 
  else if (!light && LDR >= LightThreshold) { // This function acts if it is dark and suddenly the photocell detects light above the threshold (i.e. if it was dark and a light turned on, you would be briefly blinded.) //<<!light will return true if light is false
  
    Serial.println("THE LIGHT IS DAZZLING! "); Serial.println(LDRval); //<<need to separate
    
    
    hobbyServo3.write(1);
    delay(500);
      
    
    hobbyServo1.write(179); //<< here you using hobbyServol you have missed a number "hobbServol2"? unsure which servo you wanted to use
    delay(500); //< here you missed ;
      
    
    
    bool light = true;
    delay(50); //< here you missed ;
    
  }
  
  else if (!light && LDR >= LightThreshold){ // If this loop is executed, the light level is normal and no response in generated <<!light
  
    Serial.println(LDRval);
    delay(50); //< here you missed ;
    
  }
  
}

i have put //<< on all my edits this compiles if it works the way you want it to i dont know

1 is still less than 150

hehehe as i said i only checked if it compiles but good point

if (LDR < LightThreshold){

probs needs to be

if (LDRval < LightThreshold){

The hang during compile is caused by a bug in Arduino IDE 1.6.4 that has been fixed but is only currently available in the hourly build. You can also use 1.6.3 to avoid this issue.

    bool light = true;
    delay(50)
    
  }

I don't see any point to this. "bool light" immediately goes out of scope, so who cares if it is true or false?

This "bool light" is a totally different variable to the "bool light" you declared at global scope.

Just as an example, if you have a cat in your house called "Barack Obama", that is not the same Barack Obama that resides in the White House.

   Serial.println(LDval);
    delay(50)

You might also want to read up on C++ syntax. Statements end with semicolons.

postCount++

...won't work, you need a semi-colon.

postCount++;

:art: