[SOLVED]Variable scope help!!

I am attempting to make an interactive led circuit that automatically calibrates to the light in a room on startup. I have code which I will post after this question…

I am using a infrared receiver to receive the the “threshold” amount of light in the room, I want to find this value just once, in the setup, then, I want to USE this value to compare to the constant Infrared values being received in the loop value, but I do not know how to transfer the value received in the setup scope to the loop scope… Here is my code

void setup()

{

pinMode(13,OUTPUT);
pinMode(12,OUTPUT);
int threshold = analogRead(A5);
Serial.begin(9600);
Serial.println(" “);
Serial.println(” Threshold =");
Serial.println(threshold);
Serial.println(" ");
delay(2000);

}
void loop()

{

int threshold;
int Receiver = analogRead(A5);
digitalWrite(12,HIGH);
if(Receiver < threshold)
{
digitalWrite(13,HIGH);
}
else
{
digitalWrite(13,LOW);
}
Serial.println(Receiver);

}

So basically, I am not getting that threshold value to appear in the loop scope. What do/can I do???

Please use code tags (the button with the # symbol) to make your code easier to read :wink:

int threshold; // declare threshold inside program scope instead of in one function's stack frame

void setup() { 
 
   pinMode(13,OUTPUT);
   pinMode(12,OUTPUT);
   threshold = analogRead(A5);
   Serial.begin(9600);
   Serial.println("   ");
   Serial.println(" Threshold =");
   Serial.println(threshold);
   Serial.println("   ");
   delay(2000);

}

void loop() {
 
   // int threshold; now that threshold is in a greater scope, don't re-declare it

   int Receiver = analogRead(A5);
   digitalWrite(12,HIGH);
   if(Receiver < threshold) {
     digitalWrite(13,HIGH);
   }
   else {
     digitalWrite(13,LOW);
   }
   Serial.println(Receiver);
}

I fixed your braces and indentation for you, too :wink:

So basically, I am not getting that threshold value to appear in the loop scope.

Since you know what scope is, I wonder why you didn’t just read an article on variable scoping to find the answer to this? Just knowing what variable scope means puts you sooo close to knowing how to resolve this.

Well golly! I have read some articles on variable scoping they don't tell me how to take a value from setup and use it in loop. I know I'm close I need not to be close but there!

help

never mind.. solved. Just needed to remove the " int " in the setup :)

thanks