newbie question on a conditional statement (knob value)

Hello, I am a beginner and I am using grove shield. I post here because it looks like a bug or something missing on my side concerning programming. I just wanted to test a knob on analog A0 input and output to serial port only when the value changes… The code is this:

int knob = A0;
int test_value = 0.;
int knob_level = 0.;
void setup() {
Serial.begin(9600);
}
void loop() {
knob_level = analogRead(knob);
if (test_value != knob_level) {
int test_value = knob_level;
Serial.print("test_value = ");
Serial.println(test_value);
Serial.print("knob_level = ");
Serial.println(knob_level);
}
}

It just keep sending out values even if the knob is not moving (and so the new and old value are the same) it works only for ZERO… am i missing something?
Thanks a lot, sorry for the newbie question

Screen Shot 2015-04-30 at 21.48.44.png

Look at the scope of the two variables called "test_value",

I'm surprised it compiled with the extra periods. Are they int or float?
int test_value = 0.;
int knob_level = 0.;

I'm surprised it compiled with the extra periods.

int PI = 3.14159;

will compile. The value is interpreted as a float, and then truncated on assignment to the int.

Thanks AWOL for the reply but... wasn't correct to declare the variable in the global scope to make it accessible in the loop function? I checked the fundamentals of Arduino/C language and it seemed the good way to go... am i totally dumb...? (i fear the answer....)

Sorry for the int/float mismatching, it was part of some debugging i tried to do...

No, it wasn't correct.
If the new reading and the (global) old reading are not the same, you create a new "test_value" which is only in scope for the duration of the "if" compound block, so the global "test_value" never changes.

Thanks. I am quite lost (i was used only to BASIC and JavaScript before). Any clue on how avoid it? Thanks again...

Yes, it was correct to declare test_value in global scope. It was INCORRECT to declare it again inside the if() {}. The brackets () part of the if only sees the global one, which never gets updated by your sketch. The sketch only modifies the other variable called test_value inside the braces {}.

Don't use "int" every time you use the variable. That is a declaration, meaning you are creating a new variable.

Thanks Morgan, i thought it was this...but it does not work even with the corrected code...

const int knob = A0;
int test_value = 0;
int knob_level = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);

}

void loop() {

knob_level = analogRead(knob);
if (test_value != knob_level) {
test_value = knob_level;
Serial.print("test_value = ");
Serial.println(test_value);
Serial.print("knob_level = ");
Serial.println(knob_level);
}
}

ratox:
Thanks. I am quite lost (i was used only to BASIC and JavaScript before). Any clue on how avoid it? Thanks again...

Learn about scope and extent of variables? Scope is the textual area of the source code in
which a variable's name is meaningful, in short.
Extent is the period of time during execution or which a variable exists. In C local variables
have scope limited to the currently enclosing block (ie between { and }), and extent is
for the duration of execution of the same block. A local variable hides (technically "shadows") any other variables of the same name in blocks further out or at top level.

Astute reading of the above means that you should never pass the address of a local
variable out of scope, because the extent will have ended. Other languages don't have
this draconian limit and the extent exists for as long as the address is visible.

Having a clear idea of scope and extent is necessary if you switch between several
programming languages, as this one area where there are fundamental differences
between programming languages.

.but it does not work

Around here, it's best to be sparking with that phrase.