Hey, I am currently working on a project for my university. I'm very inexperienced in Arduino and processing and have hit a wall with my knowledge and I've searched everywhere for other similar threads.
I am using an Arduino Nano, and 88 WS8211 Smart RGB LEDs
I am using a Processing screenshot to capture my screen and divide the screen up into a 32x19 grid (the LEDs I have are arranged around my screen follow the other positions of the grid) I need to get the average RGB values from these bordering screen areas to then send through to my Arduino Nano , which can then assign them to their corresponding LEDs around my screen. If anyone has any suggestions on what I could do, or how to improve my code that would be great.
Here's my Processing Code:
import java.awt.*;
import java.awt.image.*;
import processing.serial.*;
Serial serial;
Rectangle screenRect;
PImage screenGrab;
PGraphics screenGrabSmall;
int r, g, b;
color ledColour = color(0, 0, 0);
int ledNumX = 32;
int ledNumY = 19;
int ledPos[][] = new int[][]{
{10,19},{9,19},{8,19},{7,19},{6,19},{5,19},{4,19},{3,19},{2,19},{1,19}, // Bottom Left (left to right)
{0,19},{0,18},{0,17},{0,16},{0,15},{0,14},{0,13},{0,12},{0,11},{0,10},{0,9},{0,8},{0,7},{0,6},{0,5},{0,4},{0,3},{0,2},{0,1}, // Left side UP
{0,0},{1,0},{2,0},{3,0},{4,0},{5,0},{6,0},{7,0},{8,0},{9,0},{10,0},{11,0},{12,0},{13,0},{14,0},{15,0},{16,0},{17,0},{18,0},{19,0},{20,0},{21,0},{22,0},{23,0},{24,0},{25,0},{26,0},{27,0},{28,0},{29,0},{30,0},{31,0},// Top LEDS
{31,1},{31,2},{31,3},{31,4},{31,5},{31,6},{31,7},{31,8},{31,9},{31,10},{31,11},{31,12},{31,13},{31,14},{31,15},{31,16},{31,18},{31,19},// Right side down
{30,19},{29,19},{28,19},{27,19},{26,19},{25,19},{24,19},{23,19},{22,19},{21,19}
};
byte[] serialData = new byte[5 + ledPos.length * 3];
void setup() {
size(800, 600);
serial = new Serial(this, Serial.list()[0], 9600);
// how big is the screen?
screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
screenGrab = createImage((int) screenRect.getWidth(), (int) screenRect.getHeight(), RGB);
screenGrabSmall = createGraphics(ledNumX, ledNumY); // final LED resolution "image"
noSmooth(); // pixelised look
}
void draw() {
takeScreenShot();
image(screenGrabSmall, 0, 0, width, height);
serial.write(serialData);
}
void takeScreenShot() {
try {
// get screenshot, original resolution
BufferedImage screenBuffer = new Robot().createScreenCapture(screenRect);
// convert to PImage
screenBuffer.getRGB( 0, 0, screenGrab.width, screenGrab.height, screenGrab.pixels, 0, screenGrab.width);
screenGrab.updatePixels();
// scale down to LED resolution
screenGrabSmall.beginDraw();
screenGrabSmall.image(screenGrab, 0, 0, screenGrabSmall.width, screenGrabSmall.height);
screenGrabSmall.endDraw();
screenGrabSmall.loadPixels(); // loading pixels? i dont know man
for(int i=0; i<ledPos.length; i++){
int x = ledPos[i][0];
int y = ledPos[i][1]; if(y ==19) y =18;
ledColour = screenGrabSmall.get(x,y);
}
}
catch ( AWTException e ) {
e.printStackTrace();
}
for (int i=0; i<ledPos.length; i++) {
serialData[0] = (byte) 'N';
serialData[1] = (byte) i;
serialData[2] = (byte) (int(red(ledColour)));
serialData[3] = (byte) (int(green(ledColour)));
serialData[4] = (byte) (int(blue(ledColour)));
}
for (int i=0; i<ledPos.length; i++) {
//println(ledPos[i][0],ledPos[i][1]);
println(ledPos[i][0] + " " + ledPos[i][1] + " " + (int(red(ledColour))) + " " + (int(green(ledColour))) + " " + (int(blue(ledColour))));
}
}
And here's my Arduino Code (I'm not very good at this side of things =)
#include<FastLED.h>
#define LED_PIN 6
#define COLOR_ORDER GRB
#define CHIPSET WS2811
#define NUM_LEDS 88
#define BRIGHTNESS 50
#define FPS 60
CRGB leds[NUM_LEDS];
volatile byte colourList[NUM_LEDS][3];
volatile byte r, g, b, index, header;
void setup() {
Serial.begin(115200);
FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
}
void loop()
{
while (Serial.available() >= 5)
{
header = Serial.read();
if (header == 'N')
{
index = Serial.read();
//leds[0].setRGB(index, index, index);
if ( index >= 0 && index < NUM_LEDS )
{
colourList[index][0] = Serial.read();
colourList[index][1] = Serial.read();
colourList[index][2] = Serial.read();
// leds[index].setRGB([index][0],[index][1], [index][2]);
g = colourList[index][1];
b = colourList[index][2];
}
}
}
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB(r,g,b);
FastLED.show();
}
}