Offline
Newbie
Karma: 0
Posts: 11
|
 |
« on: January 23, 2013, 03:07:48 pm » |
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
Online
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: January 23, 2013, 03:11:43 pm » |
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.
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 31
Posts: 2930
I only know some basic electricity....
|
 |
« Reply #2 on: January 23, 2013, 03:13:50 pm » |
Try moving the line inside setup() to above setup(). Then val will be global scope.
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #3 on: January 23, 2013, 03:20:11 pm » |
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
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #4 on: January 23, 2013, 03:22:21 pm » |
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
Sr. Member
Karma: 7
Posts: 389
|
 |
« Reply #5 on: January 23, 2013, 03:22:42 pm » |
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
Sr. Member
Karma: 7
Posts: 389
|
 |
« Reply #6 on: January 23, 2013, 03:24:22 pm » |
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
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #7 on: January 23, 2013, 03:28:22 pm » |
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
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13897
Lua rocks!
|
 |
« Reply #8 on: January 23, 2013, 03:43:51 pm » |
I found this reply (by robtillaart) about that: To connect an LDR correctly it must be part of a voltage divider +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
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13897
Lua rocks!
|
 |
« Reply #9 on: January 23, 2013, 03:44:48 pm » |
... 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
Full Member
Karma: 2
Posts: 237
Arduino rocks
|
 |
« Reply #10 on: January 23, 2013, 03:47:33 pm » |
I believe inside setup you need: Serial.begin(baud) Put int val; inside your loop.
|
|
|
|
|
Logged
|
Arduino Uno; Mega328
|
|
|
|
|