Arduino Forum

Community => Exhibition / Gallery => Topic started by: roy18 on Feb 11, 2011, 08:11 am

Title: Arduino based Ambilight for you computer :)
Post by: roy18 on Feb 11, 2011, 08:11 am
I wanted to make a really cheap yet working implementation of an ambient light system (like those behind the new fancy TVs) for computers. I used Processing to capture screenshots, get an average screen color, and send it to the Arduino which controls an RGB led strip based on the values. I am glad it works great and am happy to share it with everyone!

Check out my setup and demo:
http://www.youtube.com/watch?v=Am55k0k9eq8&feature=player_embedded

Also I have posted my code, materials and schematic:
http://siliconrepublic.blogspot.com/2011/02/arduino-based-pc-ambient-lighting.html

Enjoy!! New ideas and comments please!  :)
Title: Re: Arduino based Ambilight for you computer :)
Post by: bld on Feb 11, 2011, 01:58 pm
Looking good, an improvement to this would be to split the screen up in multiple fields, and then use an mega or multiplexing to control 4 strips instead of one. :)
Title: Re: Arduino based Ambilight for you computer :)
Post by: majordecibel on Feb 11, 2011, 02:07 pm
I visited your blog - looks very nice, congrats :)

I actually just ordered the same rgb led strip to use with my music visualizer as a more dramatic improvement. Any tips or suggestions with using the strip?
Title: Re: Arduino based Ambilight for you computer :)
Post by: roy18 on Feb 11, 2011, 02:59 pm
Thanks guys!  @majordecibel: Sparkfun mentions that the B and G pin might be switched sometimes and that is really true! Mine was like that so do not panic if the colors look horribly wrong: just switch the B and G pin wires. Also, remember that the ULN2003A chip is really nice to drive this strip since our Arduino PWN pinouts are 5VDC and the strip requires 12VDC. Finally, remember to ground the ULN2003A chip because I seems to "work" without the biasing but the light is much weaker and fluctuates a lot (it made me sad initially). Then I realized and grounded the pin, and the led strip was glorious :)
Title: Re: Arduino based Ambilight for you computer :)
Post by: nietgiftig on Feb 13, 2011, 04:46 pm

I used Processing to capture screenshots, get an average screen color, and send it to the Arduino which controls an RGB led strip based on the values. I am glad it works great and am happy to share it with everyone!
.....
Enjoy!! New ideas and comments please!  :)



Awsome looks really great
What kind of processor power is needed on the computer?
Is it possible to devide the screen in 3 parts (left / right / top) for some more effect.

I want to make this on my HTPC.

Thanks for sharing your idea (processing and so)
Title: Re: Arduino based Ambilight for you computer :)
Post by: chnics on Feb 13, 2011, 09:49 pm
Great!
I am actually going to do something like this.

The problem at the moment is that I don't know anything about programming or the arduino things.
For now I am testing out some rgb led strip that I got from ebay to see if it's worth ordering more of. I guess the lifetime of these led strips isn't as good as the ones I have seen at our wholesalers. Also the RGB amplifiers from ebay are a lot cheaper than the quality ones here. I wonder if this will result in a burnt down house :)


Anyway... Iam going to do something similar to this:
http://fibreled.jet.ie/wp-content/uploads/2009/08/IMG_9826.jpg

I will be using this controller(common anode) when not watching tv:
http://www.ledlightsworld.com/24-keys-infrared-rgb-controller-p-120.html
And when watching tv I'll use it as ambilight with Arduino.

All video and all audio is coming from a computer(Mac).
As my lights will be in the ceiling I don't need more than one rgb out channel for the strip.

I noticed that a lot of people are using solutions that only work with specific software like winamp, vlc or xbmc.
Which isn't good for me as plugins usually, IF they come for mac are updated much later than the ones for PC.
Thats why I like yours better as it will work with anything(right?).


Someone on youtube did it like this:
"A very simple hack using lowpass filters on the VGA color signals and an Arduino that generates PWM from the filtered signals driving an IKEA "Dioder" LED strip."
http://youtube.com/watch?v=XY36fLqs4YM
What do you think of this?


You wrote "I skip every alternate pixel making my program 4 times faster".
How about every 4 or 8 pixels?
What happens? A feeling is that you still would get a ok average when watching movies?

You also wrote "delay(10); //delay for safety" and "delay(10); //just to be safe"
Is this milliseconds? Could you explain what the safety is for?

Is it possible to reduce or increase the overall brightness of the leds if they for example are too bright?

Is it possible to make the screenshots smaller than the actual screen to get better performance?
If you cut XXX pixels off every side of the screen and still get a good average reading?

Is it much more complicated and more expensive to use Bluetooth instead of a USB cable?
I would like to have all equipment on the other side of the room.




Thank you
C
Title: Re: Arduino based Ambilight for you computer :)
Post by: roy18 on Feb 14, 2011, 03:53 am
@NietGiftig:
1) What kind of processor power is needed on the computer?
This is what i see in my task-manager:
DWM:          ~34,540K   CPU 02  (desktop window manager is affected due to teh constant screenshots)
javaw.exe:    ~32,880K   CPU 13  (how much the processing code uses up)
My computer specs: 2.4 Ghz  64-bit Windows 7

2) Is it possible to devide the screen in 3 parts (left / right / top) for some more effect.
Yes it is :)
Code: [Select]
//right now part of my code is:
for(i =0;i<1368; i=i+2){
for(j=0; j<928;j=j+2){
pixel = screenshot.getRGB(i,j); //the ARGB integer has the colors of pixel (i,j)
r = r+(int)(255&(pixel>>16)); //add up reds
g = g+(int)(255&(pixel>>8)); //add up greens
b = b+(int)(255&(pixel)); //add up blues
}
}

Change it to:
Code: [Select]
//for left (one-third screen):
for(i =0;i<456; i=i+2){
for(j=0; j<928;j=j+2){
pixel = screenshot.getRGB(i,j); //the ARGB integer has the colors of pixel (i,j)
r = r+(int)(255&(pixel>>16)); //add up reds
g = g+(int)(255&(pixel>>8)); //add up greens
b = b+(int)(255&(pixel)); //add up blues
}
}


//for right (one-third screen):
for(i =912;i<1368; i=i+2){
for(j=0; j<928;j=j+2){
pixel = screenshot.getRGB(i,j); //the ARGB integer has the colors of pixel (i,j)
r = r+(int)(255&(pixel>>16)); //add up reds
g = g+(int)(255&(pixel>>8)); //add up greens
b = b+(int)(255&(pixel)); //add up blues
}
}

//for top (one-third screen):
for(i =0;i<1368; i=i+2){
for(j=0; j<310;j=j+2){
pixel = screenshot.getRGB(i,j); //the ARGB integer has the colors of pixel (i,j)
r = r+(int)(255&(pixel>>16)); //add up reds
g = g+(int)(255&(pixel>>8)); //add up greens
b = b+(int)(255&(pixel)); //add up blues
}
}


@chnics:
1) I noticed that a lot of people are using solutions that only work with specific software like winamp, vlc or xbmc.
Which isn't good for me as plugins usually, IF they come for mac are updated much later than the ones for PC.
Thats why I like yours better as it will work with anything(right?).
Yes it will work with anything. And also on any OS. Just export the application from processing (checklist the Mac option) then enjoy it as a standalone application.

2)Someone on youtube did it like this:
"A very simple hack using lowpass filters on the VGA color signals and an Arduino that generates PWM from the filtered signals driving an IKEA "Dioder" LED strip."
http://youtube.com/watch?v=XY36fLqs4YM
What do you think of this?
It is a genius way to the same thing and I actually pondered about doing it. But I was just too lazy to design a circuit to do so. Its major advantage is that it wont take up processing power of your CPU. Some minor disadvantages are its slightly slow response time, inability to control light based on sections of the screen and using up your VGA port.

3) You wrote "I skip every alternate pixel making my program 4 times faster".
How about every 4 or 8 pixels?
What happens? A feeling is that you still would get a ok average when watching movies?
I have yet to try it. I strongly believe it will still be fine for movies.

4) You also wrote "delay(10); //delay for safety" and "delay(10); //just to be safe"
Is this milliseconds? Could you explain what the safety is for?
Yes, on the arduino's code change:
Code: [Select]
analogWrite (RedPin, red);
analogWrite (GreenPin, green);
analogWrite (BluePin, blue);


to:
Code: [Select]
analogWrite (RedPin, n*red);
analogWrite (GreenPin, n*green);
analogWrite (BluePin, n*blue);


where n is a variable with maximum 1 and minimum 0. you can control n from a potentiometer to adjust the brightness using a dial :p Or you can keep n fixed to lets say 0.5 for half the brightness.

6) Is it possible to make the screenshots smaller than the actual screen to get better performance?
If you cut XXX pixels off every side of the screen and still get a good average reading?
Yes it is :). See the answer to query 2 of NietGiftig. (see above)

7) Is it much more complicated and more expensive to use Bluetooth instead of a USB cable?
It is more complicated. And it might slow down the response time.

Lastly, upload a video when youre done!! its a great interior decor idea.

Title: Re: Arduino based Ambilight for you computer :)
Post by: nietgiftig on Feb 14, 2011, 11:43 pm
Thanks for the info and addition.

I see that it all about this piece of code, the use of Processing makes it a lot easier. :)

for(i =0;i<1368; i=i+2){
for(j=0; j<928;j=j+2){
pixel = screenshot.getRGB(i,j); //the ARGB integer has the colors of pixel (i,j)

I use full HD so for me it would be 1920 x 1080
But iI can speed things up by skipping more pixels.

Do you think that 3 times a (smaller) screenshot is not to much an overhead in processortime?
I don't want the films I'm playing to stutter
I use XBMC as player.

FYI.
I use this kind of LED strip, works good I have made my Aquarium light with these LED's
http://www.dealextreme.com/details.dx/sku.41522 (http://www.dealextreme.com/details.dx/sku.41522)
Title: Re: Arduino based Ambilight for you computer :)
Post by: fkeel on Feb 15, 2011, 01:32 am
this is awesome :-)

great idea and implementation.
Title: Re: Arduino based Ambilight for you computer :)
Post by: nietgiftig on Feb 15, 2011, 05:47 pm
I have installed Processing on my Pc.
And because don't have the drivers at the moment i took one RGB LED witch I had as spare.
And yes it worked.
I changed the capture parameters for my screen. (1920x1080)

I think that it need some tinkering because not all types of RGB Led's are equal.
The LED did not get off with a black screen but stayed on more or less white coloured.
Red on the screen did not made a the LED very red
I think that the treshold for each color is differend and also depends on the type of LED (strip) that you are using.

But the begin is promising and Processing looks very much capable for this task.

Now searching for an "how to" to monitor the serialport while the Processing sends data to the Arduino
I want to see the values.
I think that I have to search in Processing :)


Title: Re: Arduino based Ambilight for you computer :)
Post by: roy18 on Feb 16, 2011, 05:36 am
Thanks fkeel!
@ NietGiftig
If you are using my code, you should be able to see the values in processing itself (the numbers being continuously printed at the black potion in the bottom of processing) Something of that sort (weak colors, not totally dark when black screen) happened when I initially used the non-PWM pins accidentally. Also happened when I didn't ground pin 8 of the ULN2003A chip (should be connected to ground of 12V power supply and also Arduino GND pin). Also, remember that since you changed the resolution you should also change the numbers you divide the total by when averaging.

Code: [Select]
r=r/(684*464); //average red (should change to r=r/(950*540) )
g=g/(684*464); //average green  (should change to g=g/(950*540) )
b=b/(684*464); //average blue   ((should change to b=b/(950*540) )


I hope this will solve the problem because I think LED strips functions more or less the same.
Title: Re: Arduino based Ambilight for you computer :)
Post by: nietgiftig on Feb 16, 2011, 12:56 pm
Thanks for the input royboy,

I had already changed the parameters, but its good to see them from you too.

I don´t see the numbers in the application screen, but I have add some print statements in the Processing code and now they are there. (I´m learning quick, after all its works almost the same as the Arduino code  :) )
The average color in the little screen however did work, that´s why I assume that it has to do with the Arduino - RGB Led combination.

When I bypass the average colour and feed the Arduino with zero's for all colours then the LED stays off, but when I increase the value to 1 then it´s lightning up.
I use the same pins as you did and I don´t use the driver yet, just a plain RGB led with resistors to the Arduino.
But I´m sure that I will find the problem, and more sure that its not in your code.

The part in your code :
      r = r+(int)(255&(pixel>>16)); //add up reds
      g = g+(int)(255&(pixel>>8)); //add up greens
      b = b+(int)(255&(pixel)); //add up blues
is something i have to discover how it works because the are so many shades of red (and blue and green)

Just playing and discovering now, that's also a fun part for me.

Title: Re: Arduino based Ambilight for you computer :)
Post by: chnics on Feb 16, 2011, 07:23 pm
Thanks for the answers Royboy!
I just ordered all the Arduino stuff last night.
Hopefully the wait won't be too long. Bluetooth was quite expensive so  I got a Uno.

Sure I'll do a video!
Though I am between apartments so it will be after a move And after a renovation. So don't wait up.. :)

You got great feedback on your blog!

You guys who are talking about 2,3 and 4 channels for your ambilights. There are videos on youtube with 64.
Search for "64 channel ambilight".

Royboy, are you still interested in this project and looking to improve it or do you see yourself as done and going for the next challenge?
Title: Re: Arduino based Ambilight for you computer :)
Post by: mowcius on Feb 16, 2011, 08:41 pm

You guys who are talking about 2,3 and 4 channels for your ambilights. There are videos on youtube with 64.
Search for "64 channel ambilight".

Yeah that's pretty cool but the fade in/out is much too slow on it for my liking. Presumably that can be adjusted though :)

I wouldn't have thought that doing a lot of channels would be too tricky.

I'm currently wondering about if some kind of VGA passthrough device would be possible, with a chip controlling the ambilight without the computer having to do anything.

Perhaps this is more easily possible for RGB video?

RGB video presumably would be relatively easy as the RGB is split up already.
Title: Re: Arduino based Ambilight for you computer :)
Post by: nietgiftig on Feb 17, 2011, 12:41 pm

I'm currently wondering about if some kind of VGA passthrough device would be possible, with a chip controlling the ambilight without the computer having to do anything.

Perhaps this is more easily possible for RGB video?

RGB video presumably would be relatively easy as the RGB is split up already.


The problem with RGB or VGA is that if you also have to divide the screen
If you want to get good Ambilight you extend the screen with the colours projected on the wall.
So you have to divide the screen where each side gets the colour of that portion of the screen.
Thats not posible with the RGB output from your Tv set without additional hardware to devide the screen as far as I know.

To take it a step further, i have seen a demo with thunder and explosions wich were directed to a seperate very bright LED wich pulsed on the explosions.  :D
Title: Re: Arduino based Ambilight for you computer :)
Post by: mowcius on Feb 17, 2011, 02:02 pm

The problem with RGB or VGA is that if you also have to divide the screen
If you want to get good Ambilight you extend the screen with the colours projected on the wall.
So you have to divide the screen where each side gets the colour of that portion of the screen.
Thats not posible with the RGB output from your Tv set without additional hardware to devide the screen as far as I know.

Yeah, that was the conclusion I came to after a while.
Title: Re: Arduino based Ambilight for you computer :)
Post by: AndAy on Feb 18, 2011, 05:57 pm
Is there anyway for the script to avoid averaging the black bars on widescreen movies?
Title: Re: Arduino based Ambilight for you computer :)
Post by: nietgiftig on Feb 18, 2011, 06:56 pm
Yes, define the screen portion that you need.

I'm curently developing a version with 4 the sides from the screen giving sepate outputs.
In that version I have a pixel offset feature,  with that you can skip the black bars.

By the way most of the work must be done in Processing, not on the Arduino
Title: Re: Arduino based Ambilight for you computer :)
Post by: roy18 on Feb 19, 2011, 01:30 am
@chnics "Royboy, are you still interested in this project and looking to improve it or do you see yourself as done and going for the next challenge?"

My aim was to quickly create a platform which is easy to understand and modify. My job is done here :) I am quite happy with it :) It will at most take a person a day of coding and a week of shipping materials to extend this to screen borders and so on. So good luck to everyone who wants to extend this further! And please post your code somewhere where everyone can find :D

It is another weekend and thus time for me to work on something else :p
Title: Re: Arduino based Ambilight for you computer :)
Post by: fkeel on Feb 19, 2011, 09:57 pm
edit: just tried this myself. 9.6 volts works just fine...

Has anyone tried powering these strips off 9.6 volts? I assume it should work, but the LED's will simply be dimm. Does that make sense? Can anyone who has a working setup and a 9v battey lying around quickly give this a try and tell me the results? I would like to incroporate this into an excisting project and dont have 12v available...

Well, if someone has the equipment to quickly check this out for me, it would be apreceated.

Cheers

P.
Title: Re: Arduino based Ambilight for you computer :)
Post by: roy18 on Feb 20, 2011, 02:20 am
I tried that initially. My 9V battery couldn't drive even one channel.
Title: Re: Arduino based Ambilight for you computer :)
Post by: fkeel on Feb 20, 2011, 04:46 am

Edit: I got this to work

I have the plus end of 9.6Volts (1600 mAh) connected to the power in of the LED strip and the minus connected to the ground of my arduino. I have the Red Green and Blue channels directly connected to digital pins 9,10,11.

In the arduino code, I inverted the values send to PWM (i.e. blue = 255 - Serial.read();)

and tada. It works like a charm.

Hey Royboy, maybe you can help me. I have now officially butchered your project, and it "almost" works. Now I am a completel lay person at this and I am having the trouble finding the error.

I have 9.6Volts (1600 mAh) connected to the power in of the LED strip. I have the Red Green and Blue channels directly connected to digital pins 9,10,11. The ground of the battery is connected to a ground pin in the arduino.

This setup works (and makes me feel like a butcher). I can do RGB as well as CMYK and White using simple on & off. However, when I run your code, it does respond to the screen, but in a slightly random manner. Almost as if it where ouputting inverse values. (gets bright on a white background and dark on a black one etc.) One of the things I have tried is subtrackting 20 from all the values, which, for some mysterious reason improves the result (as in, black will dimm them, white makes it bright, yellows and blues are spot on, rest is random again.)

edit: the values that processing sends out are correct. something is going wrong on the arduino end.

Any Idea why it should have this issue?

I have had problems with this particular arduino board before, I suspect it might simply be bad. On the other hand, what I am doing seems rather unrefined, though I just cant find a reason for it not to work. (not that I actually know what I am doing.)

any ideas would be apreceated.

Thanks

P.
Title: Re: Arduino based Ambilight for you computer :)
Post by: roy18 on Feb 22, 2011, 08:26 am
@fkeel
Alright I looked at the ULN 2003 datasheet: http://focus.ti.com/lit/ds/symlink/uln2003a.pdf and the thing is that the first thing the chip does is that it performs a logical NOT on the input. Thus a +5V signal into the chip creates a 0V output signal and a 0V signal creates a 12V output signal in my setup. Now this is wonderful because the led is bright when the difference between the +12V common anode and the output signal from the chip is 12V. a.k.a. The led is bright when the output signal is zero cause by a 5V signal from the arduino.  In your case this is switched because the if the arduino outputs a 5V, the difference voltage is 9.6-5 = 4.6 and the leds are off. while when your arduino output is a 0V, the difference voltage is 9.6V and your leds are on! So basically the whole ON/OFF of the leds is the opposite in your setup than my setup.

Now by doing (255-Serial.read), you are completely switching the ratio of the ON and OFF time in one duty cycle. So what the ULN2003 chip switches for me, you do the switching in your code instead; ending up with the correct result :)

I hope my explanation is understandable.
Title: Re: Arduino based Ambilight for you computer :)
Post by: fkeel on Feb 22, 2011, 04:31 pm

yep. that explains it. and thanks for posting this - whil I figured out how to solve the problem, it still confused me...

btw. could you explain your reasoning for using the ULN2003 chip?

anyway. thanks for posting this project - as you see, you inspired me to mess around with this myself and I learned some things which have become really handy in another project I am working on right now.

cheers

p.
Title: Re: Arduino based Ambilight for you computer :)
Post by: AndAy on Feb 25, 2011, 05:22 am
What code modifications would be needed to make it so that if processing wasn't sending it any data, the strip would slowly pulsate blue, but when processing started sending data again, it would become an ambilight again?
Title: Re: Arduino based Ambilight for you computer :)
Post by: fkeel on Feb 25, 2011, 05:51 am
check the processing webpage for the exact code. Its a sample matter of doing

if serial is available, do amblight
else, pulsate blue.

you want this implemented on your arduino.

check the arduino reference material on "serial" & "if", and for the pulsating you may want to use a "for2 loop. its trivial, you should be able to figure it out...

If you need more help, tell me, and I'll give you more detailed instructions.
Title: Re: Arduino based Ambilight for you computer :)
Post by: AndAy on Feb 25, 2011, 06:18 am
Would it be a serial.available that would serve this purpose?
Title: Re: Arduino based Ambilight for you computer :)
Post by: bld on Feb 26, 2011, 02:04 am
I just made a new version of this, based on the original code.

(http://peecee.dk/uploads/022011/ambi_new_big_thumb.png) (http://peecee.dk/uploads/022011/ambi_new.png)

This one got four rectangles where it gets the average color within. This makes the processing of the picture a bit faster, because it skips all the pixels outside of the boxes.

The form size is based on the screen (1/5th of the real size) and the rectangles inside are in scale to the settings with border (there are definable borders on all four sides of the screen), and to the size of each box.

http://captain-slow.dk/2011/02/26/ambilight-with-arduino-and-processing/
Title: Re: Arduino based Ambilight for you computer :)
Post by: mowcius on Feb 26, 2011, 10:26 am
Neat bld :)
Title: Re: Arduino based Ambilight for you computer :)
Post by: bld on Feb 26, 2011, 02:45 pm
Already working on an update, want it to have more options, and work even faster than it currently do.
Title: Re: Arduino based Ambilight for you computer :)
Post by: roy18 on Feb 27, 2011, 02:29 am
@AndAy: yep! Logically using an if there makes sense. So if (available) {mode =0}  else{mode =1)}. after that you can write two codes for: if (mode =0){make colors = serial data} else{some code for pulsating blue}. (sorry for the horrible pseudocode)

@bld: NICE.


Title: Re: Arduino based Ambilight for you computer :)
Post by: bld on Feb 27, 2011, 06:58 pm
And here is the new version... Still same speed as before, but now it is possible to deactivate those you don't use.

(http://captain-slow.dk/wp-content/uploads/2011/02/ambi_new2-300x187.png) (http://captain-slow.dk/wp-content/uploads/2011/02/ambi_new2.png)

And also put an FPS counter in.
Title: Re: Arduino based Ambilight for you computer :)
Post by: fkeel on Feb 28, 2011, 03:57 am
why is the bottom left green?

@AndAy

did you manage to do what you planned? if not and you still need help send me a message over the forum and I'll see if I can quickly whip it up.
Title: Re: Arduino based Ambilight for you computer :)
Post by: Targettio on Mar 03, 2011, 02:53 pm
Nice project, I have seen this sort of thing done with Boblight and other programs but I like the idea of using processing as it is much easier to setup and get working.

One question; can you set up processing so it doesn't produce the little draw window? I would like to use it on my main PC as it should reduce eye strain. So I want to export the processing code to an .exe and run that at startup. But it would be a annoying to have a little box sat in the corner of the screen randomly changing colour.
Title: Re: Arduino based Ambilight for you computer :)
Post by: bld on Mar 03, 2011, 02:59 pm

why is the bottom left green?

No idea, feel free to see if you can find the bug.

Code: [Select]
//Developed by Rajarshi Roy heavily modified by bld @ http://captain-slow.dk/
import java.awt.Robot; //java library that lets us take screenshots
import java.awt.AWTException;
import java.awt.event.InputEvent;
import java.awt.image.BufferedImage;
import java.awt.Rectangle;
import java.awt.Dimension;
import processing.serial.*; //library for serial communication


Serial port; //creates object "port" of serial class
Robot robby; //creates object "robby" of robot class

//IMPORTANT!!!
//Put the right screen size in here, if isn't 100% right, the code might give some unexpected results.
int screenW     = 1920;
int screenH     = 1200;

//Define a border on each side of the screen
int borderLeft  = 10;
int borderRight = 10;
int borderTop   = 50;
int borderBot   = 100;

//Size of top left box
boolean tlActiv = false;
int topLeftW    = 350;
int topLeftH    = 450;

//Size of top right box
boolean trActiv = false;
int topRightW   = 350;
int topRightH   = 450;

//Size of top center box
boolean tcActiv = true;
int topCenterW  = 1000;
int topCenterH  = 250;

//Size of bottom left box
boolean blActiv = false;
int botLeftW    = 350;
int botLeftH    = 450;

//Size of bottom right box
boolean brActiv = false;
int botRightW   = 350;
int botRightH   = 450;

//Size of bottom center box
boolean bcActiv = false;
int botCenterW  = 1000;
int botCenterH  = 250;

//Color adjustments, use this to adjust the color values to match your LEDs
int maxRed      = 255;
int maxGreen    = 255;
int maxBlue     = 125;

//How many pixels to skip while reading
int pixelSpread = 2;

void setup()
{
  port = new Serial(this, Serial.list()[0],115200); //set baud rate
  size(screenW/5, screenH/5); //window size

  try //standard Robot class error check
  {
    robby = new Robot();
  }
  catch (AWTException e)
  {
    println("Robot class not supported by your system!");
    exit();
  }
}

void draw()
{
  long start = millis();
  fill(255);
  ellipse((screenW/5)/2, screenH/5/2, 80, 40);
  //rect((screenW/5)/2-50, screenH/5/2-25, 100, 50);

  int pixel; //ARGB variable with 32 int bytes where
  //sets of 8 bytes are: Alpha, Red, Green, Blue
  float r=0;
  float g=0;
  float b=0;

  //get screenshot into object "screenshot" of class BufferedImage
  BufferedImage screenshot = robby.createScreenCapture(new Rectangle(new Dimension(screenW,screenH)));


  //Calculate top left rectangle
  if (tlActiv)
  {
    for(int i = borderLeft; i < (topLeftW + borderLeft); i += pixelSpread)
    {
      for(int j = borderTop; j < (topLeftH + borderTop); j += pixelSpread)
      {
        pixel = screenshot.getRGB(i,j); //the ARGB integer has the colors of pixel (i,j)
        r = r+(int)(maxRed&(pixel>>16)); //add up reds
        g = g+(int)(maxGreen&(pixel>>8)); //add up greens
        b = b+(int)(maxBlue&(pixel)); //add up blues
      }
    }
    r = r / ( (topLeftW / pixelSpread) * (topLeftH / pixelSpread) ); //average red
    g = g / ( (topLeftW / pixelSpread) * (topLeftH / pixelSpread) ); //average green
    b = b / ( (topLeftW / pixelSpread) * (topLeftH / pixelSpread) ); //average blue

    port.write(0xC1); //sync
    port.write((byte)(r)); //red
    port.write((byte)(g)); //green
    port.write((byte)(b)); //blue

    color topL = color(r, g, b);
    fill(topL);
    rect(borderLeft/5, borderTop/5, topLeftW/5, topLeftH/5);

    r = 0;
    g = 0;
    b = 0;
  }

  //Calculate top right rectangle
  if (trActiv)
  {
    for(int i = screenW - (borderRight + topRightW); i < (screenW-borderRight); i += pixelSpread)
    {
      for(int j = borderTop; j < (topRightH + borderBot); j += pixelSpread)
      {
        pixel = screenshot.getRGB(i,j); //the ARGB integer has the colors of pixel (i,j)
        r = r+(int)(maxRed&(pixel>>16)); //add up reds
        g = g+(int)(maxGreen&(pixel>>8)); //add up greens
        b = b+(int)(maxBlue&(pixel)); //add up blues
      }
    }
    r = r / ( (topRightW / pixelSpread) * (topRightH / pixelSpread) ); //average red
    g = g / ( (topRightW / pixelSpread) * (topRightH / pixelSpread) ); //average green
    b = b / ( (topRightW / pixelSpread) * (topRightH / pixelSpread) ); //average blue

    port.write(0xC2); //sync
    port.write((byte)(r)); //red
    port.write((byte)(g)); //green
    port.write((byte)(b)); // blue

    color topR = color(r, g, b);
    fill(topR);
    rect(screenW/5 - ((topRightW/5)+(borderRight/5)), borderTop/5, topRightW/5, topRightH/5);

    r = 0;
    g = 0;
    b = 0;
  }


  //Calculate top center rectangle
  if (tcActiv)
  {
    for(int i = (screenW/2) - (topCenterW/2); i < (screenW/2) + (topCenterW/2); i += pixelSpread)
    {
      for(int j = borderTop; j < (topCenterH + borderTop); j += pixelSpread)
      {
        pixel = screenshot.getRGB(i,j); //the ARGB integer has the colors of pixel (i,j)
        r = r+(int)(maxRed&(pixel>>16)); //add up reds
        g = g+(int)(maxGreen&(pixel>>8)); //add up greens
        b = b+(int)(maxBlue&(pixel)); //add up blues
      }
    }
    r = r / ( (topCenterW / pixelSpread) * (topCenterH / pixelSpread) ); //average red
    g = g / ( (topCenterW / pixelSpread) * (topCenterH / pixelSpread) ); //average green
    b = b / ( (topCenterW / pixelSpread) * (topCenterH / pixelSpread) ); //average blue

    port.write(0xC5); //sync
    port.write((byte)(r)); //red
    port.write((byte)(g)); //green
    port.write((byte)(b)); // blue

    color topR = color(r, g, b);
    fill(topR);
    rect((screenW/5)/2 - (topCenterW/5)/2, borderTop/5, topCenterW/5, topCenterH/5);

    r = 0;
    g = 0;
    b = 0;
  }


  //Calculate bottom left rectangle
  if (blActiv)
  {
    for(int i = borderLeft; i < (botLeftW + borderLeft); i += pixelSpread)
    {
      for(int j = screenH - (botLeftH + borderBot); j < (screenH - borderBot); j += pixelSpread)
      {
        pixel = screenshot.getRGB(i,j); //the ARGB integer has the colors of pixel (i,j)
        r = r+(int)(maxRed&(pixel>>16)); //add up reds
        g = g+(int)(maxGreen&(pixel>>8)); //add up greens
        b = b+(int)(maxBlue&(pixel)); //add up blues
      }
    }
    r = r / ( (botLeftW / pixelSpread) * (botLeftH / pixelSpread) ); //average red
    g = g / ( (botLeftW / pixelSpread) * (botLeftH / pixelSpread) ); //average green
    b = b / ( (botLeftW / pixelSpread) * (botLeftH / pixelSpread) ); //average blue

    port.write(0xC3); //sync
    port.write((byte)(r)); //red
    port.write((byte)(g)); //green
    port.write((byte)(b)); //blue

    color botL = color(r, g, b);
    fill(botL);
    rect(borderLeft/5, screenH/5 - (botLeftH/5 + borderBot/5), botLeftW/5, botLeftH/5);

    r = 0;
    g = 0;
    b = 0;
  }

  //Calculate bottom right rectangle
  if (brActiv)
  {
    for(int i = screenW - (borderRight + botRightW); i < (screenW-borderRight); i += pixelSpread)
    {
      for(int j = screenH - (botRightH + borderBot); j < (screenH - borderBot); j += pixelSpread)
      {
        pixel = screenshot.getRGB(i,j); //the ARGB integer has the colors of pixel (i,j)
        r = r+(int)(maxRed&(pixel>>16)); //add up reds
        g = g+(int)(maxGreen&(pixel>>8)); //add up greens
        b = b+(int)(maxBlue&(pixel)); //add up blues
      }
    }
    r = r / ( (botRightW / pixelSpread) * (botRightH / pixelSpread) ); //average red
    g = g / ( (botRightW / pixelSpread) * (botRightH / pixelSpread) ); //average green
    b = b / ( (botRightW / pixelSpread) * (botRightH / pixelSpread) ); //average blue

    port.write(0xC4); //sync
    port.write((byte)(r)); //red
    port.write((byte)(g)); //green
    port.write((byte)(b)); //blue

    color botR = color(r, g, b);
    fill(botR);
    rect(screenW/5 - ((topRightW/5)+(borderRight/5)), screenH/5 - (botLeftH/5 + borderBot/5), botRightW/5, botRightH/5);

    r = 0;
    g = 0;
    b = 0;
  }

  //Calculate bottom center rectangle
  if (bcActiv)
  {
    for(int i = (screenW/2) - (botCenterW/2); i < (screenW/2) + (botCenterW/2); i += pixelSpread)
    {
      for(int j = screenH - (botCenterH + borderBot); j < (screenH - borderBot); j += pixelSpread)
      {
        pixel = screenshot.getRGB(i,j); //the ARGB integer has the colors of pixel (i,j)
        r = r+(int)(maxRed&(pixel>>16)); //add up reds
        g = g+(int)(maxGreen&(pixel>>8)); //add up greens
        b = b+(int)(maxBlue&(pixel)); //add up blues
      }
    }
    r = r / ( (botCenterW / pixelSpread) * (botCenterH / pixelSpread) ); //average red
    g = g / ( (botCenterW / pixelSpread) * (botCenterH / pixelSpread) ); //average green
    b = b / ( (botCenterW / pixelSpread) * (botCenterH / pixelSpread) ); //average blue

    port.write(0xC6); //sync
    port.write((byte)(r)); //red
    port.write((byte)(g)); //green
    port.write((byte)(b)); // blue

    color topR = color(r, g, b);
    fill(topR);
    rect((screenW/5)/2 - (botCenterW/5)/2, screenH/5 - (botCenterH/5 + borderBot/5), botCenterW/5, botCenterH/5);

    r = 0;
    g = 0;
    b = 0;
  }

  fill(0);
  textAlign(CENTER, CENTER);
  text(round(1000/(millis() - start)) + " fps", (screenW/5)/2, screenH/5/2);
}

Title: Processing based Ambilight (with a little help from Arduino)
Post by: nietgiftig on Mar 03, 2011, 03:44 pm
Topic Title should be : Processing based Ambilight (with a little help from Arduino)

This is the processing side of things.
I think the the processing forum would be happy to see this (too)
Maybe they can help

http://forum.processing.org/ (http://forum.processing.org/)
Title: Re: Arduino based Ambilight for you computer :)
Post by: fabio978 on Sep 10, 2011, 05:42 am

I think that it need some tinkering because not all types of RGB Led's are equal.
The LED did not get off with a black screen but stayed on more or less white coloured.
Red on the screen did not made a the LED very red
I think that the treshold for each color is differend and also depends on the type of LED (strip) that you are using.


Thank you (to the Author) for this great and simple project.
But i have to agree with NietGiftig about "calibrating", i noticed differences between the processing window color and the actual RGB color on my stripe. Adjusting green and blue values using map function in processing helped a lot to obtain a more accurate light  ;)
Title: Re: Arduino based Ambilight for you computer :)
Post by: BogRoll on Feb 16, 2013, 11:59 pm
Hey guys,

First of all @Royboy thanks for sharing your project with us I'm having a lot of fun messing around with it.

I need a little bit of help with setting the arduino code up the way I want to. I basically want to do what @AndAy wanted to do. Which is to have the arduino act as a background light when the processing is off and as an ambilight when processing is on, except I'm not looking for a pulsating light.

My problem is that I have very limited experience with code, while I was doing my degree in electronics engineering we barely touched on C++ and C. So after trying my best to figure it out on my own I'm now stuck :P I'd appreciate if any one could help me out.

Regards,

Lou
Title: Re: Arduino based Ambilight for you computer :)
Post by: dtokez on Feb 18, 2013, 02:02 am
Hi, I have just built a 12 channel (4 strip) controller running the amblone software.

It works great and I am very pleased, but the windows software is quite processor intensive, how intensive is processing at working out the colours?
Title: Re: Arduino based Ambilight for you computer :)
Post by: BogRoll on Feb 18, 2013, 02:53 pm
It's not too bad, I've got an AMD Phenom II X4 955 BL processor running @ 3.2 Jiggahertz and with processing on it idles at about 25% usage, but processing is capturing the full screen. I'm thinking of optimizing to capture just specific areas so that should reduce the CPU usage.