![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxXlyF1cb0n5p_1RkLDqRuU1eFaN2x3AoC9TLUhC_pwKYgHjRSp8zKlXOa6VWU8L2q8BqjQxS1zovz8wIkSmhOnCCUcchBsysoEWoYOUHe0-vOlpPzq2Q0JGWcXTM2mRcvyctF4s3KuAFu/s400/nightlightComplete.jpg)
horizontally i hooked up multiple LEDs and the wires like the yellow one you see, those wires are hooked up to the digital PWM numbers (3, 5, 6, 9, 10, 11)
then i have 2 codes that i followed
First the Photo cell
/*
*A simple Programme that will change the
*intensity of an LED based on the amount of
* light incident on the photo resistor.
*
*/
//photoresistor pin
int lightpin = 0; //the ana log pin the
//photoresistor is
//connected to
//the photo resistor is not
//calibrated to any units so
//this is simply a raw sensor value (relative light)
//LED pin
int ledpin = 9;//the pin the LED is conneced to
//we are conrtolling the brigtness so
//we use one of the PWM (pulse
//width modulation pins)
void setup()
{
pinMode(ledpin, OUTPUT); //sets the led pin to
//output
}
/* loop() - this funtion will start after setup
*finishes and then repeat
*/
void loop()
{
int lightLevel = analogRead(lightpin); //Read the
// light level
lightLevel = map(lightLevel, 0, 900, 0, 255);
//adjust the value 0 to 900 to
lightLevel = constrain(lightLevel, 0, 255);
//make sure the value is between 0 and 255
analogWrite(ledpin, lightLevel); //Wright the value
}
Then i worked with the LED pattern code
/*This sketch is intended to approximate the blinking of fireflies. It varies the delay between
blinks and varies the total blink time. I've used the random() function to vary which PWM output
is chosen for the next blink.
Hope you get some enjoyment out of it. I created it as a fun night light for my kids.
Chad Richardson -- Chad@ChadsCustomWood.net
*/
int value;
int pwmPin = 11; // light connected to digital pin 11-- I just chose an initial value
//int ledpin2 = 9;
long time=0;
int period = 500;
int i = 0;
long blink_delay = 1000; // these must be declared as long due to the random() operation
long blink = 3;
long random_led = 55;
const byte pwmPins [] = {3, 5, 6, 9, 10, 11};
void setup()
{ //nothing to setup
}
void loop()
{
choose_firefly();
fade();
blink_delay = random(500, 4001);
delay(blink_delay);
blink = random(2, 5);
}
void fade()
{
for(i=0; i<255;)
{
time = i;
value = abs(-127+127*cos(4*PI/period*i)); //the -127 value shifts the cosine curve negative with a zero initial value; abs shifts everything positive
analogWrite(pwmPin, value); // sets the value (range from 0 to 255)
delay(blink);
i++;
}
}
void choose_firefly()
{
pwmPin = pwmPins [random (0, 6)];
}
No comments:
Post a Comment