User Tools

Site Tools


light_experience_prototyping
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.event.*;
import java.awt.event.KeyEvent;
import java.awt.MouseInfo;
import java.awt.Point;
 
PImage shot;
Robot robot;
import processing.serial.*;
 
Serial myPort;  
int val;       
 
void setup(){
  size(1000, 150);
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
  rectMode(CENTER);
  try
  {
    robot = new Robot();
    BufferedImage screenshot =  robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
    shot = new PImage(screenshot);
  }catch (AWTException e){
    throw new RuntimeException("Unable to Initialize", e);
  }
 
}
/**
 * Simple Write. 
 * 
 * Check if the mouse is over a rectangle and writes the status to the serial port. 
 * This example works with the Wiring / Arduino program that follows below.
 */
float r,g,b = 0;
int i=0;
void draw(){
  Point mouse;
  mouse = MouseInfo.getPointerInfo().getLocation();
  BufferedImage screenshot =  robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
  shot = new PImage(screenshot);
  color top;
     top = shot.get( mouse.x, mouse.y);
  myPort.write('S'); 
  r = (float) (red(top)*0.780);//(199/255));
  g = (float) (green(top)*0.99);//;(254/255));
  b = (float) (blue(top)*0.56);//(144/255));
 
  myPort.write((byte) r); 
  myPort.write((byte) g); 
  myPort.write((byte)b); 
  i++;
  stroke(top);
  line(i,0,i+1,150);
}
int j=0;
void keyPressed() {
  j++;
  save("image"+j+".tif");
  background(0);
  i=0;
}
/*
@Title Processing to Arduino RGB LED (Neopixels)
@Author Geert Roumen
@Institute Umeå Institute of Design
@Date 29-01-2018
 
@Hardware
 
ESP8266   RGB Neopixel
 
GND       GND
D2        Input
5V/USB    Power
 
NodeMCU ESP8266:
 
https://en.wikipedia.org/wiki/NodeMCU 
 
Neopixel:
https://www.adafruit.com/category/168
 
In this example sketch processing can send an RGB value to Arduino and Arduino will change the LED color of the NUMPIXELS connected neopixels
*/
 
#include <Adafruit_NeoPixel.h>
 
//In this example I use a nodeMCU, with the LED strip connected to D2, for normal Arduino use a number for digital, A0-A5 for the analog pins 
#define PIN            D2
 
//The amount of pixels connected to the Arduino, make sure they are powered correctly
#define NUMPIXELS      6
 
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
 
void setup() {
  Serial.begin(9600);
  pixels.begin(); // This initializes the NeoPixel library.
}
 
void loop() {
 
  // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.
  int r,r2,r3;
  int g,g2,g3;
  int b,b2,b3;
  if(Serial.available()) {   
    if(Serial.read() == 'S') {
      //When it finds an 'S' char, it will wait untill it finds 3 other bytes
      while(!Serial.available()){}
      r = Serial.read();
      while(!Serial.available()){}
      g = Serial.read();
      while(!Serial.available()){}
      b = Serial.read();
    }
    delay(10); // some recovery time, not sure if nessecary
 
    for(int i=0;i<NUMPIXELS;i++){
 
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(r,g,b)); // Moderately bright green color.
 
  }
      pixels.show(); // This sends the updated pixel color to the hardware
  }
 
}
light_experience_prototyping.txt · Last modified: 2018/01/29 13:07 by geert