Pages: 1 2 [3]   Go Down
Author Topic: Arduino based Ambilight for you computer :)  (Read 17645 times)
0 Members and 1 Guest are viewing this topic.
Urbana, USA
Offline Offline
Newbie
*
Karma: 2
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@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.


Logged

Vejen, Denmark
Offline Offline
Edison Member
*
Karma: 9
Posts: 1178
RAWR! ^,..,^
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

And here is the new version... Still same speed as before, but now it is possible to deactivate those you don't use.



And also put an FPS counter in.
Logged

captain-slow.dk | non contagious!

Offline Offline
God Member
*****
Karma: 2
Posts: 711
a, b = b, a+b
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Offline Offline
Jr. Member
**
Karma: 1
Posts: 95
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Vejen, Denmark
Offline Offline
Edison Member
*
Karma: 9
Posts: 1178
RAWR! ^,..,^
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

why is the bottom left green?
No idea, feel free to see if you can find the bug.

Code:
//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);
}
Logged

captain-slow.dk | non contagious!

Netherlands
Offline Offline
Newbie
*
Karma: 1
Posts: 27
C+ N00b
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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/
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 36
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-wink
Logged

Ireland
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-razz I'd appreciate if any one could help me out.

Regards,

Lou
Logged

South UK
Offline Offline
God Member
*****
Karma: 5
Posts: 583
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Ireland
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: 1 2 [3]   Go Up
Jump to: