Serial input - matrix output
in
Integration and Hardware
•
1 year ago
Hello, very new to Processing.
Using an Arduino I'm reading an 6x8 analog matrix sensor. I'm trying to visualize the reading in a matrix with a grey level changes.
What I'm sending via serial is this:
9999
41
99
90
13
138
0
0
0
..
..
..
for a total of 48 lines (6x8). The 9999 at the beginning is to sync Processing matrix to Arduin serial communication. What I want to do is that Processing waits for the 9999 line to start building the matrix with the readings. Obviously this format can be changed. In this case Processing detects the new line.
The Processing program I wrote is:
import processing.serial.*;
float colorValue = 0;
int cols = 8; // number of columns
int rows = 6; // number of rows
int[][] myArray = new int[cols][rows];
Serial myPort; // The serial port
void setup() {
size(480,360); //window size
// List all the available serial ports
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Arduino, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[5], 9600);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
myArray[i][j] = 0;
}
}
}
void draw() {
}
void serialEvent(Serial myPort) {
int start = myPort.read();
if (start == 9999); {
// get the ASCII string:
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
int inByte = myPort.read(); //read from serial
float greyLevel = (inByte);
colorValue = map (greyLevel, 0, 1023, 255, 0); //convert into 255-0 range
fill (colorValue);
rect(i*60,j*60, 60 ,60);
}
}
}
}
I can see the squares, but something is not working because I can't see the grey levels changing for each square.
Thank you in advance for your help.
Using an Arduino I'm reading an 6x8 analog matrix sensor. I'm trying to visualize the reading in a matrix with a grey level changes.
What I'm sending via serial is this:
9999
41
99
90
13
138
0
0
0
..
..
..
for a total of 48 lines (6x8). The 9999 at the beginning is to sync Processing matrix to Arduin serial communication. What I want to do is that Processing waits for the 9999 line to start building the matrix with the readings. Obviously this format can be changed. In this case Processing detects the new line.
The Processing program I wrote is:
import processing.serial.*;
float colorValue = 0;
int cols = 8; // number of columns
int rows = 6; // number of rows
int[][] myArray = new int[cols][rows];
Serial myPort; // The serial port
void setup() {
size(480,360); //window size
// List all the available serial ports
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Arduino, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[5], 9600);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
myArray[i][j] = 0;
}
}
}
void draw() {
}
void serialEvent(Serial myPort) {
int start = myPort.read();
if (start == 9999); {
// get the ASCII string:
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
int inByte = myPort.read(); //read from serial
float greyLevel = (inByte);
colorValue = map (greyLevel, 0, 1023, 255, 0); //convert into 255-0 range
fill (colorValue);
rect(i*60,j*60, 60 ,60);
}
}
}
}
I can see the squares, but something is not working because I can't see the grey levels changing for each square.
Thank you in advance for your help.
1