Pages: [1]   Go Down
Author Topic: Photo Resistor question  (Read 378 times)
0 Members and 1 Guest are viewing this topic.
Florida
Offline Offline
God Member
*****
Karma: 2
Posts: 559
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Hello,

I got the program below off of this website and I'm wondering if somebody could explain what 
"Serial.println(analogRead(lightPin))" does in the program.


Thanks
Drew Davis


int lightPin = 0;  //define a pin for Photo resistor
int ledPin=11;     //define a pin for LED

void setup()
{
    Serial.begin(9600);  //Begin serial communcation
    pinMode( ledPin, OUTPUT );
}

void loop()
{
    Serial.println(analogRead(lightPin)); //Write the value of the photoresistor to the serial monitor.
    analogWrite(ledPin, analogRead(lightPin)/4);  //send the value to the ledPin. Depending on value of resistor
                                                //you have  to divide the value. for example,
                                                //with a 10k resistor divide the value by 2, for 100k resistor divide by 4.
   delay(10); //short delay for faster response to light.
}
Logged

France
Offline Offline
God Member
*****
Karma: 34
Posts: 987
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, look there is a comment at the right of that line smiley
Logged

Offline Offline
Sr. Member
****
Karma: 6
Posts: 429
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

lightpin is defined here as pin 0 (zero) on the arduino board, this is the arduino pin that has the photoresistor connected to it.
The resistance of the photoresistor varies with the amount of light that falls on it, pin zero reads this value same as it would with a pot (variable resistor).

Serial.print prints out this value to the serial monitor as well as controlling the brightness of the LED.
Logged

Florida
Offline Offline
God Member
*****
Karma: 2
Posts: 559
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Let me rephrases my question…

I understand that it prints the value to the serial monitor but how does that effect the program?
Also, what is a serial monitor? Sorry to ask such basic questions.


Thanks,
Drew Davis
Logged

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

Quote
I understand that it prints the value to the serial monitor but how does that effect the program?
It doesn't affect the program in any way apart from slowing it down a bit.

The serial monitor is one means by which you may monitor the output of your sketch.
It's a type of basic terminal emulator.
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.

Florida
Offline Offline
God Member
*****
Karma: 2
Posts: 559
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I just deleted it and the program worked fine!

Thanks for your help,
Drew Davis
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Deleting it does no harm as AWOL remarked, but don't forget it's existance - Serial.print and Serial.println are your primary weapons for debugging your sketches. You will be needing them before long I suspect.
Logged

Pages: [1]   Go Up
Jump to: