Go Down

Topic: Photo Resistor question (Read 429 times) previous topic - next topic

Drew Davis


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

guix

Hello, look there is a comment at the right of that line :)

Lakes

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.

Drew Davis

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

AWOL

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

Drew Davis

I just deleted it and the program worked fine!

Thanks for your help,
Drew Davis

wildbill

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.

Go Up