Help variables

Hi I am REALLY new and I cannot understand why would the below not work. I tried change the line that reads "if(test = 1)" to "if(test == 1)" because I think I read somewhere that to read a value one needs to use double while to assign one then only one is needed. Nevertheless the LED on PIN 13 stays on and the display test thing shows always 1...
I know the answer is probably REALLY basic but still... I cannot figure out :frowning:
Thank you so much!

#include <IRremote.h>

int IRpin = 11; // pin for the IR sensor
int LED = 13; // LED pin
IRrecv irrecv(IRpin);
decode_results results;

int test = 1;

void setup()
{
pinMode(13, OUTPUT);
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}

void loop() {
if (irrecv.decode(&results)) {
// Serial.println(results.value);
irrecv.resume();

if(results.value == 551520375) // Your ON button value
{
Serial.println(test);
if(test = 1)
{
int test = 2;
digitalWrite(13,HIGH);

}
else
{
digitalWrite(13,LOW);
int test = 1;
}
delay(100);
Serial.println(test);

}
}
}

You have caused yourself problems by declaring several different versions of the variable named test.

When you do

int test = 1;

you declare a new variable and give it a value but the variable is only available to the program inside the code block where it was declared. More formally, this describes the scope of the variable. When that block ends then if you have previously declared a variable of the same name in the scope to which the program has now moved then the program will use that value for the variable.

So, what to do about it ?
The easy solution is to declare the variable once outside of any function, usually at the start of the program. This will mean that it is usable throughout the program and has what is known as global scope. When you want to assign a value to the variable somewhere in the program do not precede its name with a type specifier, simply use

test = 1;

for instance

A secondary problem in your program is this

     if (test = 1)

I assume that you want to test the value of the test variable but by using = and not == you have actually assigned a value to it.

...and note that it is the result (non-zero) of the assignment which is tested.
It is, of course, always true

AWOL:
...and note that it is the result (non-zero) of the assignment which is tested.
It is, of course, always true

Unless you assign 0...

So, what to do about it ?
The easy solution is to declare the variable once outside of any function, usually at the start of the program. This will mean that it is usable throughout the program and has what is known as global scope. When you want to assign a value to the variable somewhere in the program do not precede its name with a type specifier, simply use

test = 1;

[/quote]

If I do this I get an error when I verify or upload:

exit status 1
'test' does not name a type

We can't see the code that gives you that error message.

oh ya, I figured it out meanwhile! :slight_smile:
I first create the variable "int test = 1" and then after I just change it's value without "int" otherwise I am creating new ones which are only available inside the blocks.
I got that error because I did not put "int" in the very first one.
I fixed it and it works now.
Thank you, very appreciated :slight_smile: