Pages: [1]   Go Down
Author Topic: Why won't this work?  (Read 422 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);

}

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24321
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Scope.
The variable is defined inside setup, so can't be seen in any other function.
Logged

"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.

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4002
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try moving the line inside setup() to above setup(). Then val will be global scope.
Logged

Examples can be found in your IDE.

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: January 23, 2013, 03:23:30 pm by Robotbuilder9999 » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);

}
Logged

Offline Offline
Edison Member
*
Karma: 18
Posts: 1170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Also, the way you have loop you will read (and print!) val hundreds or thousands of times a second. For this toy application include a delay(1000) statement to read only once per second.
Logged

Offline Offline
Edison Member
*
Karma: 18
Posts: 1170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What light are you trying to display? Is it attached to the Arduino? How do you have the LDR hooked up? LDR's do not provide voltage of their own, you need to hook it up to a voltage through a resistor.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have LDR connected to a 5V port on the arduino and the other leg on the LDR connected to ground and analog in put port "A0".
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I found this reply (by robtillaart) about that:

Quote
To connect an LDR correctly it must be part of a voltage divider

Code:
+5V <----> [ LDR ] <-----> [A0]  <------> [Resistor] <-----> GND

The value of resistor must be in the same order as the LDR when in the dark. (a 10 K will work probably too)
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

... the other leg on the LDR connected to ground and analog in put port "A0".

If A0 is connected to Gnd, clearly it will always read zero.
Logged

0
Offline Offline
Sr. Member
****
Karma: 3
Posts: 268
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I believe inside setup you need:

Code:
Serial.begin(baud)

Put

Code:
int val;

inside your loop.
Logged

Arduino Uno;
Mega328

Pages: [1]   Go Up
Jump to: