int sensorPin = A0;
int ledPin = 9;
void setup()
{
Serial.begin(9600);
// We'll set up the LED pin to be an output.
pinMode(ledPin, OUTPUT);
}
void loop()
{
int lightLevel = analogRead(sensorPin); //Reading the light intensity from the analog input
lightLevel = map(lightLevel, 0, 1023, 0, 255); //Adjusting the output digital port to the input reading from the analog input
lightLevel = constrain(lightLevel, 0, 255);
Serial.print("\n lightLevel =");
Serial.print(lightLevel);
if (lightLevel >= 50)
{
analogWrite(ledPin, HIGH); //Writing an analog value to the LED
delay(100);
}else{
analogWrite(ledPin, LOW); //Writing an analog value to the LED
delay(100);
}
// The above statement will brighten the LED along with the
// light level. To do the opposite, replace "lightLevel" in the
// above analogWrite() statement with "255-lightLevel".
// Now you've created a night-light!
}