Objective:
Using a photoresistor to control the brightness of an LED.
Parts:
Photo resistor
LED
10k ohm Resistor
330 ohms Resistor
Sketch:
const int
sensorPin = 0;
const int
ledPin = 9;
int lightLevel;
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
lightLevel = analogRead(sensorPin);
Tune();
analogWrite(ledPin, lightLevel);
}
void Tune()
{
lightLevel = map(lightLevel, 400, 1023, 0,
255);
lightLevel = constrain(lightLevel, 0, 255);
}
Analysis:
The LED[9] responds to how much
light the sensor[0] receives. The sensorPin determines the LED’s
brightness [lightLevel]. The function “Tune” tunes the number
received through sensorPin so that it is only
between 0 and 255 because analogWrite will only recognize values between
those numbers.
No comments:
Post a Comment