Go Down

Topic: Why won't this work? (Read 550 times) previous topic - next topic

Robotbuilder9999

Why won't this code work.
There's an error and it says val was not declared in scope.


Here's the code:


void setup() {

int val = 0;

}

void loop () {
 
val = analogRead(0);

Serial.print(val);

}


AWOL

Scope.
The variable is defined inside setup, so can't be seen in any other function.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

GoForSmoke

Try moving the line inside setup() to above setup(). Then val will be global scope.
I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Robotbuilder9999

#3
Jan 23, 2013, 09:20 pm Last Edit: Jan 23, 2013, 09:23 pm by Robotbuilder9999 Reason: 1
Thanks !  But I am trying to make a LDR tell my computer the light level through the serial port.  And it won't display the light level when I click serial monitor.  What do I do to make it work?

Robotbuilder9999

Heres the code for you guys to look at so you can help me:

int val = 0;

void setup() {
}

void loop () {
 
val = analogRead(0);

Serial.print(val);

}

Go Up