User Tools

Site Tools


light_experience_prototyping

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
light_experience_prototyping [2018/01/29 13:07]
geert
light_experience_prototyping [2018/10/01 14:52] (current)
geert
Line 1: Line 1:
 +<code java>/*
 +@Title Processing to Arduino RGB LED (Neopixels)
 +@Author Geert Roumen
 +@Institute Umeå Institute of Design
 +@Date 29-01-2018
 + 
 +@Software
 +IDE:  Processing 3.3.6
 +OS:   ​Windows 10.0.17134 Build 17134
 +*/
  
 +import java.awt.*;
 +Robot robot;
 +import processing.serial.*;​
 + 
 +Serial myPort;  ​
 +int val;       
 +
 +//Setup the communication and Robot instance
 +//See https://​docs.oracle.com/​javase/​7/​docs/​api/​java/​awt/​Robot.html#​getPixelColor(int,​%20int)
 +
 +void setup(){
 +  size(1000, 150);
 +  //Connect to the first Serial port of the PC
 +  String portName = Serial.list()[0];​
 +  print("​Serial ports: ");
 +  println(Serial.list());​
 +  print("​Chose ");
 +  print(portName);​
 +  //Try to open this port
 +  myPort = new Serial(this,​ portName, 9600);
 +  //If you want this window to always stay on top
 +  //​surface.setAlwaysOnTop(true);​
 +  ​
 +  //Try to create a AWT Robot
 +  try
 +  {
 +    robot = new Robot();
 +
 +  }catch (AWTException e){
 +    throw new RuntimeException("​Unable to Initialize",​ e);
 +  }
 + 
 +}
 +//Make a counter variable
 +int i=0;
 +
 +void draw(){
 +  //Get the location of the pointer (mouse/​cursor)
 +  Point mouse;
 +  mouse = MouseInfo.getPointerInfo().getLocation();​
 +  ​
 +  //Get the color at the location of the pointer, pointer icon is ignored
 +  Color c = robot.getPixelColor(mouse.x,​mouse.y);​
 +  ​
 +  //Get the integer RGB value at this point
 +  int top = c.getRGB();
 +  ​
 +  //Write an S to the Serial port
 +  myPort.write('​S'​); ​
 +  ​
 +  //Do some conversion to make it look white when all colors are 255
 +  //These values are based on neopixels.
 +  float r = (float) (red(top)*0.780);//​(199/​255));​
 +  float g = (float) (green(top)*0.99);//;​(254/​255));​
 +  float b = (float) (blue(top)*0.56);//​(144/​255));​
 +  ​
 +  //Start writing the [RGB] as three bytes 
 +  myPort.write((byte) r); 
 +  myPort.write((byte) g); 
 +  myPort.write((byte)b);  ​
 +  i++;
 +  stroke(top);​
 +  line(i,​0,​i+1,​150);​
 +}
 +int j=0;
 +//if a key is pressed an image of the current '​flow'​ will be saved.
 +void keyPressed() {
 +  j++;
 +  save("​image"​+j+"​.tif"​);​
 +  background(0);​
 +  i=0;
 +}</​code>​
 +
 +<code cpp>
 +
 +/*
 +@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
 +  }
 +  ​
 +}</​code>​
light_experience_prototyping.txt · Last modified: 2018/10/01 14:52 by geert