Professional Documents
Culture Documents
Living
Outside
Play
Technology
Workshop
http://www.instructables.com/id/Color-Changing-Nightlight-Arduino-Microcontrolle/
http://www.instructables.com/id/Color-Changing-Nightlight-Arduino-Microcontrolle/
Step 2: Programming
Here is the program if used, you may have to changes sometimes to adapt it to your microcontroller. /* NightLight */ int sensorValue = 0; // Variable to store the value coming from the photoresistor int ledPin1 = 0; // Connect an LED to PWM pin 0 int ledPin2 = 1; // '' pin 1 int ledPin3 = 2; // '' pin 2 int fadeValue2 = 0; // Variable for fading 2 colors at once int threshold = 850; // Threshold value for the photoresistor void lightup(void); void setup() { pinMode(20, INPUT_ANALOG); // Setup pin 20 to read a voltage pinMode(ledPin1, PWM); // setup the pin as PWM pinMode(ledPin2, PWM); pinMode(ledPin3, PWM); } void loop() { sensorValue = analogRead(20); // Read the analog value if(sensorValue < threshold) { lightup(); // If less than threshold call lightup } if(sensorValue > threshold) { pwmWrite(ledPin1, 0); // If not less than threshold turn off LED pwmWrite(ledPin2, 0); pwmWrite(ledPin3, 0); } delay(10); } void lightup(void) { pwmWrite(ledPin1, 65535); for (int fadeValue = 0; fadeValue <= 65535; fadeValue += 1280) { // Sets the value (range from 0 to 65535): yellow pwmWrite(ledPin2, fadeValue); // Wait for 30 milliseconds to see the dimming effect: delay(1000); } for (int fadeValue = 0; fadeValue <= 65535; fadeValue += 1280) { // Sets the value (range from 0 to 65535): white pwmWrite(ledPin3, fadeValue); // Wait for 30 milliseconds to see the dimming effect: delay(1000); } for (int fadeValue = 65535 ; fadeValue >= 0; fadeValue -= 1280) { // Sets the value (range from 0 to 1280): violet pwmWrite(ledPin2, fadeValue); // Wait for 30 milliseconds to see the dimming effect: delay(1000); }
http://www.instructables.com/id/Color-Changing-Nightlight-Arduino-Microcontrolle/
for (int fadeValue = 65535 ; fadeValue >= 0; fadeValue -= 1280) { // Sets the value (range from 0 to 1280): blue pwmWrite(ledPin1, fadeValue); // Wait for 30 milliseconds to see the dimming effect: delay(1000); } for (int fadeValue = 65535 ; fadeValue >= 0; fadeValue -= 1280) { // Sets the value (range from 0 to 1280): green pwmWrite(ledPin3, fadeValue); pwmWrite(ledPin2, fadeValue2); fadeValue2 += 1280; // Wait for 30 milliseconds to see the dimming effect: delay(1000); sensorValue = analogRead(20); } for (int fadeValue = 0; fadeValue <= 65535; fadeValue += 1280) { // Sets the value (range from 0 to 65535): lt blue pwmWrite(ledPin3, fadeValue); // Wait for 30 milliseconds to see the dimming effect: delay(1000); sensorValue = analogRead(20); } fadeValue2 = 65535; for (int fadeValue = 0; fadeValue <= 65535; fadeValue += 1280) { // Sets the value (range from 0 to 65535): red pwmWrite(ledPin1, fadeValue); pwmWrite(ledPin2, fadeValue2); pwmWrite(ledPin3, fadeValue2); fadeValue2 -= 1280; // Wait for 30 milliseconds to see the dimming effect: delay(1000); } } Adjusting the value for threshold will change the sensitivity to ambient light.
Related Instructables
How to make an Arduino based night light with infrared remote control! by timotet
http://www.instructables.com/id/Color-Changing-Nightlight-Arduino-Microcontrolle/