Go Down

Topic: Mini ambient lightning (Read 213 times) previous topic - next topic

Aug 29, 2014, 09:13 pm Last Edit: Aug 30, 2014, 07:20 pm by EndLessMind Reason: 1
    Hello everyone!

    This is a small project i've been working on for a few hours, and I'd like to share it with you guys :)

    So, I got some LED from a friend that didn't need them anymore.
    2 of them where really long (17cm or ~6.2") and where pretty strong white once.
    So, i decided to make some ambient lightning for my balcony with them.

    Partlist:

    • 2pcs TO-220 N-channel mosfets

    • 3pcs 10k ohm resistors

    • 1pcs Arduino Mini Pro 5v woth 328

    • 1pcs prototyping PCB

    • 1pcs photoresistor that i recovered from an old surveillance camera

    • Some 16 and 18 gauge wires and the wire from an old headset

    • pcs 17cm LED get them here


    I don't have any schematics, but the mosfets is connected the standard way for N-channel mosfets, with a 10k ohm resistor that pulls the gate to ground. That's so

    it will automatically close when the power from the Arduino is removed.

    Also the photoresistor is connected the standard way, with a 10k ohm resistor to ground.




    The code:
    I hope I've commented the code well enough for everyone to understand it :)
    It's programmed to fade between then brightnesslevels.
Code: [Select]
#include <Metro.h>

int led1 = 3;
int led2 = 5;
int sensorPin = A0; //A photoresistor that I recovered from an old surveillance camera
int sensorValue = 0;
int led1Value = 0;
int led2Value = 0;
int nextValue = 0;

Metro ledMetro = Metro(20);
Metro updateMetro = Metro(2000);

void setup() {                
 Serial.begin(57600); //Used for debugging
 Serial.println("Start up"); // Used for debugging, will let me know if the Arduino resets due to error in the code.
 pinMode(led1, OUTPUT);
 
 pinMode(led2, OUTPUT); //The hardware supports 2 channles (2 mosfets), but only one is used for now. So we just set that output to 0 (LOW) for now.
 analogWrite(led2, 0);
}

void fadeUp(int ledPin) {
 
 if (led1Value < nextValue) {
   led1Value++;
   analogWrite(ledPin, led1Value);
 }

}

void fadeDown(int ledPin) {
 
 if (led1Value > nextValue) {
   led1Value--;
   analogWrite(ledPin, led1Value);
 }

}

//This gives us a smooth transaction between that brightness levels.
//My usage did not require that, but I added it anyway. Might need it later on
void fadeBrightness(int ledPin) {
 
 //Check with Metro to see if the time has lapsed.
 //we could use delay(20);, but that will lock down the processor for 20ms
 //and we want to be able to run other stuff while the brightness is still adjusting.
 //For example, I'll add a radio transmitter & receiver later on (for remote control)
 //So for that I want to be able to lister for incomming data and not have to wait for the brightness to be fully adjusted.
 //Because that could cause the Arduino to miss some data
 if (ledMetro.check() == 1) {
     if (led1Value < nextValue) {
       fadeUp(ledPin);
  } else if (led1Value > nextValue) {
       fadeDown(ledPin);
  }
 }
 

}

void loop() {
 //Check with our update-Metro to see if the time has lapsed.
 //In this case we only read new data every 2 seconds.
 //We don't need to read more, and if we read it every time the loop runs,
 //That would cause the LED to flicker, due to small constant changes in the
 //photoresistors resistans.
 if (updateMetro.check() == 1) {
    sensorValue = analogRead(sensorPin);
 }

 //Here we evaluate the sensors value.
 //I my case, i want the LED's to dim up when it's getting dark (lighting up my balcony).
 if (sensorValue < 400) {
   //Because the sensors value is below 400, then we need to convert that in to a value that will not exeed 255 (8-bit PWM supported by Arduino).
   //This works just like map(sensorValue, 0, 1023, 0, 255) would, but I didn't remember that function when i wrote the code,
   //and as my code works fine, I don't see any reason to change it.
   nextValue = (int)(255 - ((int) sensorValue * 0.6375));
   fadeBrightness(led1);
 } else if (sensorValue > 399) { //If the value is 400 or higher, then it's to bright outside (my case), so we will turn the lights of (smoothly)
   nextValue = 0;
   fadeBrightness(led1);
 }
 
}



Some pictures of the build:
Don't mind the solder-joints, the headset wires didn't like having tin on them.
Also, don't mind how I've secured the cable :P That was just for testing :)
I don't got many pictures, but if you want, I'll take more.

Here is a video of how it works. Sorry for my bad english :P

Go Up