Definition : By using photo resistor, we can change the resistor value and send different amount of voltage to the leds base on our light level ! We arrange lightlevel value and give different values of each led so they react different to light as you see.
Circuit Diagram
Circuit Diagram
Components
- 1 x Photo Resistor
- 3 x Led ( Green, Yellow, Red)
- 3 x 330 Ω Resistor
- Wires
- Uno Board
Code
const int sensorPin = 0;
const int ledPin = 9;
int ledPin2 = 10;
int ledPin3 = 11;
// We'll also set up some global variables for the light
level:
int lightLevel, high = 0, low = 1023;
void setup()
{
// We'll set up the
LED pin to be an output.
// (We don't need to
do anything special to use the analog input.)
pinMode(ledPin,
OUTPUT);
pinMode(ledPin2,
OUTPUT);
pinMode(ledPin3,
OUTPUT);
}
void loop()
{
lightLevel =
analogRead(sensorPin);
manualTune(); // manually change the range from light to
dark
analogWrite(ledPin,
lightLevel);
analogWrite(ledPin2,
lightLevel+1);
analogWrite(ledPin3,
lightLevel+100);
}
void manualTune()
{
lightLevel =
map(lightLevel, 0, 10, 0, 255);
lightLevel =
constrain(lightLevel, 0, 255);
}
void autoTune()
{
if (lightLevel <
low)
{
low = lightLevel;
if (lightLevel >
high)
{
high = lightLevel;
}
lightLevel =
map(lightLevel, low+30, high-30, 0, 255);
lightLevel =
constrain(lightLevel, 0, 255);
}
Video
Hiç yorum yok:
Yorum Gönder