Using this program it is possible to read a analog 6x8 matrix sensor connected to an Arduino and visualize the read out on a matrix using Processing.
Sensor is connected to Arduino so that digital pins 2 to 9 are connected to the 9 rows of the sensor, while the 6 columns are connected to Analog pins 0 to 5.
Arduino program
int Pin0 = A0; // sensor attached to analog Pin0
int Pin1 = A1; // sensor attached to analog Pin1
int Pin2 = A2; // sensor attached to analog Pin2
int Pin3 = A3; // sensor attached to analog Pin3
int Pin4 = A4; // sensor attached to analog Pin4
int Pin5 = A5; // sensor attached to analog Pin5
const int dPin2 = 2; //output attached to digital Pin 2 = sensor row 0
const int dPin3 = 3; //output attached to digital Pin 3 = sensor row 1
const int dPin4 = 4; //output attached to digital Pin 4 = sensor row 2
const int dPin5 = 5; //output attached to digital Pin 5 = sensor row 3
const int dPin6 = 6; //output attached to digital Pin 6 = sensor row 4
const int dPin7 = 7; //output attached to digital Pin 7 = sensor row 5
const int dPin8 = 8; //output attached to digital Pin 8 = sensor row 6
const int dPin9 = 9; //output attached to digital Pin 9 = sensor row 7
int sensorValue0 = 0; //value read from line 0 of the sensor
int sensorValue1 = 1; //value read from line 1 of the sensor
int sensorValue2 = 2; //value read from line 2 of the sensor
int sensorValue3 = 3; //value read from line 3 of the sensor
int sensorValue4 = 4; //value read from line 4 of the sensor
int sensorValue5 = 5; //value read from line 5 of the sensor
int msensorValue0 = 0; // mapped value read from line 0 of the sensor
int msensorValue1 = 1; // mapped value read from line 1 of the sensor
int msensorValue2 = 2; // mapped value read from line 2 of the sensor
int msensorValue3 = 3; // mapped value read from line 3 of the sensor
int msensorValue4 = 4; // mapped value read from line 4 of the sensor
int msensorValue5 = 5; // mapped value read from line 5 of the sensor
void setup() {
pinMode(dPin2, OUTPUT); //pin2 is an OUTPUT
pinMode(dPin3, OUTPUT); //pin3 is an OUTPUT
pinMode(dPin4, OUTPUT); //pin4 is an OUTPUT
pinMode(dPin5, OUTPUT); //pin5 is an OUTPUT
pinMode(dPin6, OUTPUT); //pin6 is an OUTPUT
pinMode(dPin7, OUTPUT); //pin7 is an OUTPUT
pinMode(dPin8, OUTPUT); //pin8 is an OUTPUT
pinMode(dPin9, OUTPUT); //pin9 is an OUTPUT
Serial.begin(9600);
}
void loop()
{
digitalWrite (dPin9, HIGH); //turn row0 on
sensorValue0 = analogRead (Pin5); //read value column 0
// wait 10 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(10);
sensorValue1 = analogRead (Pin4); //read value column 1
delay(10);
sensorValue2 = analogRead (Pin3); //read value column 2
delay(10);
sensorValue3 = analogRead (Pin2); //read value column 3
delay(10);
sensorValue4 = analogRead (Pin1); //read value column 4
delay(10);
sensorValue5 = analogRead (Pin0); //read value column 5
delay(10);
digitalWrite (dPin9, LOW); //turn row0 off
// Number of columns and rows in the grid
int cols = 8;
int rows = 6;
// Declare 2D array
float[] myArray = new float[cols*rows];
import processing.serial.*;
Serial myPort;
void setup() {
size(480, 360);
// 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');
}
void draw() {
}
void serialEvent(Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// split the string on the commas and convert the
// resulting substrings into an integer array:
float[] myArray = float(split(inString, ","));
// if the array has at least three elements, you know
// you got the whole thing. Put the numbers in the
// color variables:
println("Arrivato");
if (myArray.length >=48) {
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
stroke(255);
println("valori i=" + i + "J="+ j + "val=" + int(myArray[(i*rows)+j]));
fill(int(myArray[(i*rows)+j]));
rect(i*60,j*60,60,60);
}
}
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.