Processing and Arduino

I combined Processing and Arduino in this example by getting an RGB LED to change colors according to the pixels on the screen (using processing).

The codes:

int redPin   = 9;   
int greenPin = 10;
int bluePin  = 11;  

long int inByte;

void setup()
{
  pinMode(redPin,   OUTPUT);  
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin,  OUTPUT);

  Serial.begin(9600);
}


void outputColour(int red, int green, int blue) {
  analogWrite(redPin, red);
  analogWrite(bluePin, blue);
  analogWrite(greenPin, green);
}


int* getColour() {
  int* colour;
  int i;

  i = 0;

  while (i < 4)
  {
    if (Serial.available() > 0) {
      colour[i] = Serial.read();
      i++;
    }
  }

  return colour;
}

void loop()
{
  if (Serial.available() > 0) {

    inByte = Serial.read();

    if (inByte == 'C') {
      int* one;
      one =  getColour();

      outputColour(one[1], one[2], one[3]);

    }
  }

  delay(10);
}
import processing.serial.*;

PImage img;  
Serial myPort;

void setup() {
  //set these to the size of the image

  size(512, 512);
  // printArray(Serial.list());
  // String portname=Serial.list()[1];
  // println(portname);

  img = loadImage("pixels.jpg");
  myPort = new Serial(this, Serial.list()[1], 9600);
}

void draw() {
  background(0);
  image(img, 0, 0);
  img.loadPixels();
}

void mousePressed() 
{
  myPort.write("CL");
  myPort.write(int(red(img.pixels[mouseX+mouseY*img.width])));
  myPort.write(int(green(img.pixels[mouseX+mouseY*img.width]))); 
  myPort.write(int(blue(img.pixels[mouseX+mouseY*img.width])));
}

 

I got help using the following tutorials:

https://www.hackster.io/hardikrathod/control-arduino-using-gui-arduino-processing-2c9c6c

https://www.instructables.com/id/RGBs-with-Arduino-and-Processing/

 

Leave a Reply