Hello;
I’m so new to processing and I need help with my code.
Basically i have a sensor connected to Arduino that sends out data of an array of data. I’m able to send the data to processing and gets them converted to int successfully.
My plan is to use these sensor data ( an array [36]), to change the color of a 2D grid array ([6][6]).
I saw a perfect example of using 2D array from this link. https://processing.org/tutorials/2darray/
My pan is to see how i can map the sensor values to the 2D array grid since they are of the same size (36). So that according to the change of values of the sensor data, the corresponding grid will change color.
Now i do not know how to use the sensor values to change the properties of the class object.
attached is my code.
// 2D Array of objects
Cell[][] grid;
import processing.serial.*;
Serial myPort; // Create object from Serial class
//String myString = null; // initialize the string to which the message will be assigned
int end = 10; // the number 10 is ASCII for linefeed (end of serial.println), later we will look for this to break up individual messages
// Number of columns and rows in the grid
int cols = 6;
int rows = 6;
int[] serialInArray = new int[36]; // Where we'll put what we receive
int count = 36;
void setup() {
size(600,600);
myPort = new Serial(this, "COM7", 9600);
grid = new Cell[cols][rows];
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
// Initialize each object
grid[i][j] = new Cell(i*100,j*100,100,100,i+j);
}
}
}
void draw()
{
background(0);
// The counter variables i and j are also the column and row numbers and
// are used as arguments to the constructor for each object in the grid.
for (int i = 0; i < cols; i++)
{
for (int j = 0; j < rows; j++)
{
// Oscillate and display each object
grid[i][j].oscillate();
grid[i][j].display();
}
}
}
// read the serial buffer for the sensor data
void serialEvent(Serial myPort) {
String myString = myPort.readStringUntil(end);
// if we get any bytes other than the linefeed:
if (myString != null)
{
// remove the linefeed
myString = trim(myString);
// split the string at the tabs and convert the sections into integers:
int mysensors[] = int(split(myString, '\t'));
count = mysensors.length;
for (int k=0; k<count; k++)
{
serialInArray[k] = mysensors[k];
println (serialInArray[k]);
}
//println();
}
}
class code
// A Cell object
class Cell {
// A cell object knows about its location in the grid
// as well as its size with the variables x,y,w,h
float x,y; // x,y location
float w,h; // width and height
float angle; // angle for oscillating brightness
int[] serialInArray = new int[36]; // Where we'll put what we receive
// Cell Constructor
Cell(float tempX, float tempY, float tempW, float tempH, float tempAngle) {
x = tempX;
y = tempY;
w = tempW;
h = tempH;
angle = tempAngle;
}
// Oscillation means increase angle
void oscillate() {
angle += 0.02;
}
void display() {
stroke(255);
// Color calculated using sine wave
fill(127+127*sin(angle));
rect(x,y,w,h);
}
}