/ /
Photocells
While a potentiometer is handy when someone is around to turn the knob, having a photocell is ideal for environmentally controlled interactions. You use the same principles with a photocell as you do with a pot.A low value will occur when the sensor is well lit and a high value will ocur when it is in the dark.
Driving 2 LEDs
This schematic will not work with blue LEDs. Blue LEDs have a voltage drop of around 3.4V.
If you wired up two in series, the voltage drop would be around 6.8V, which is greater than the 5V
the Arduino puts out.
Combine the above circuit with a photocell:In order to fade the leds, you need to use analogWrite() on one of the PWM pins (3,5,6,9,10,11).
here's code to create an auto dimmer:
Option 1:
int photoCell=__;
int led=___;
int val=__;
void setup(){
//set up the led pin
}
void loop(){
val=______;
val=val/4;
________(led,val);
}
Option 2 (a better way):
int photoCell=__;
int led=___;
int lightLevel=__;
void setup(){
//set up the led pin
}
void loop(){
lightLevel=______;
lightLevel=map(lightLevel,0,900,0,255);
lightLevel=constrain(lightLevel,0,255);
________(led,lightLevel);
}
Want the opposite response?
int photoCell=__;
int led=___;
int lightLevel=__;
void setup(){
//set up the led pin
}
void loop(){
lightLevel=______;
lightLevel=map(lightLevel,0,900,0,255);
lightLevel=constrain(lightLevel,0,255);
________(led,255-lightLevel);
}
int photoCell=__;
int led=___;
int lightLevel=__;
int threshold=300;
void setup(){
//set up the led pin
}
void loop(){
if(analogRead(photoCell)>threshold){
________(led,HIGH);
}else{
_______________;
}
}
How would you get the LEDs to glow bright and then fade?
0 is off
127 is half on
255 is full bright
Here's code to get the glow:
int photoCell=__;
int led=___;
int val=__;
byte brightTable={30,30,30,40,50,70,80,90,100,110,
120,130,140,150,160,170,180,190,200,210,
220,230,240,250,250,250,240,230,220,210,
200,190,180,170,160,150,140,130,120,110,
100,90,80,70,60,50,40,30,30,30};
int maxCount=50;
int count=0;
void setup(){
//set up the led pin
}
void loop(){
analogWrite(_____,brightTable[count]);
count++;
if(count>maxCount){
//reset count
}
val=______;
val=val/4;
delay(val);
}
Make something spooky!