Skip to content

Instantly share code, notes, and snippets.

@dicamarques14
Created January 24, 2016 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dicamarques14/0d79d9a12f2dfd7443ab to your computer and use it in GitHub Desktop.
Save dicamarques14/0d79d9a12f2dfd7443ab to your computer and use it in GitHub Desktop.
Processing code to use with arduino to control RGB LED
//Developed by Rajarshi Roy
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
void setup()
{
port = new Serial(this, Serial.list()[0],9600); //set baud rate, if it doesnt work change Serial.list[0] to the number of your com port
size(200, 200); //window size (doesn't matter)
try //standard Robot class error check
{
robby = new Robot();
}
catch (AWTException e)
{
println("Robot class not supported by your system!");
exit();
}
}
void draw()
{
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;
int w=1920;
int h=1080;
//get screenshot into object "screenshot" of class BufferedImage
BufferedImage screenshot = robby.createScreenCapture(new Rectangle(new Dimension(w,h)));
//1368*928 is the screen resolution
int i=0;
int j=0;
//1368*928
//I skip every alternate pixel making my program 4 times faster
for(i=40;i<(w-40); i=i+4){
for(j=40;j<(h-40);j=j+4){
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
}
}
r=r/(((w-180)/4)*((h-180)/4)); //average red (remember that I skipped ever alternate pixel)
g=g/(((w-180)/4)*((h-180)/4)); //average green
b=b/(((w-180)/4)*((h-180)/4)); //average blue
if(r<=15)
r=0;
if(g<=15)
g=0;
if(b<=15)
b=0;
if(r>=255)
r=255;
if(g>=255)
g=255;
if(b>=255)
b=255;
port.write("cL");
port.write(int(r));
port.write(int(g));
port.write(int(b));
println(int(r),int(g),int(b));
delay(10); //delay for safety
background(r,g,b); //make window background average color
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment