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
msensorValue0 = map (sensorValue0, 0, 1023, 255, 0);
msensorValue1 = map (sensorValue1, 0, 1023, 255, 0);
msensorValue2 = map (sensorValue2, 0, 1023, 255, 0);
msensorValue3 = map (sensorValue3, 0, 1023, 255, 0);
msensorValue4 = map (sensorValue4, 0, 1023, 255, 0);
msensorValue5 = map (sensorValue5, 0, 1023, 255, 0);
Serial.print(msensorValue0);
Serial.print(',');
Serial.print(msensorValue1);
Serial.print(',');
Serial.print(msensorValue2);
Serial.print(',');
Serial.print(msensorValue3);
Serial.print(',');
Serial.print(msensorValue4);
Serial.print(',');
Serial.print(msensorValue5);
Serial.print(',');
digitalWrite (dPin8, 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 (dPin8, LOW); //turn row0 off
msensorValue0 = map (sensorValue0, 0, 1023, 255, 0);
msensorValue1 = map (sensorValue1, 0, 1023, 255, 0);
msensorValue2 = map (sensorValue2, 0, 1023, 255, 0);
msensorValue3 = map (sensorValue3, 0, 1023, 255, 0);
msensorValue4 = map (sensorValue4, 0, 1023, 255, 0);
msensorValue5 = map (sensorValue5, 0, 1023, 255, 0);
Serial.print(msensorValue0);
Serial.print(',');
Serial.print(msensorValue1);
Serial.print(',');
Serial.print(msensorValue2);
Serial.print(',');
Serial.print(msensorValue3);
Serial.print(',');
Serial.print(msensorValue4);
Serial.print(',');
Serial.print(msensorValue5);
Serial.print(',');
digitalWrite (dPin7, 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 (dPin7, LOW); //turn row0 off
msensorValue0 = map (sensorValue0, 0, 1023, 255, 0);
msensorValue1 = map (sensorValue1, 0, 1023, 255, 0);
msensorValue2 = map (sensorValue2, 0, 1023, 255, 0);
msensorValue3 = map (sensorValue3, 0, 1023, 255, 0);
msensorValue4 = map (sensorValue4, 0, 1023, 255, 0);
msensorValue5 = map (sensorValue5, 0, 1023, 255, 0);
Serial.print(msensorValue0);
Serial.print(',');
Serial.print(msensorValue1);
Serial.print(',');
Serial.print(msensorValue2);
Serial.print(',');
Serial.print(msensorValue3);
Serial.print(',');
Serial.print(msensorValue4);
Serial.print(',');
Serial.print(msensorValue5);
Serial.print(',');
digitalWrite (dPin6, 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 (dPin6, LOW); //turn row0 off
msensorValue0 = map (sensorValue0, 0, 1023, 255, 0);
msensorValue1 = map (sensorValue1, 0, 1023, 255, 0);
msensorValue2 = map (sensorValue2, 0, 1023, 255, 0);
msensorValue3 = map (sensorValue3, 0, 1023, 255, 0);
msensorValue4 = map (sensorValue4, 0, 1023, 255, 0);
msensorValue5 = map (sensorValue5, 0, 1023, 255, 0);
Serial.print(msensorValue0);
Serial.print(',');
Serial.print(msensorValue1);
Serial.print(',');
Serial.print(msensorValue2);
Serial.print(',');
Serial.print(msensorValue3);
Serial.print(',');
Serial.print(msensorValue4);
Serial.print(',');
Serial.print(msensorValue5);
Serial.print(',');
digitalWrite (dPin5, 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 (dPin5, LOW); //turn row0 off
msensorValue0 = map (sensorValue0, 0, 1023, 255, 0);
msensorValue1 = map (sensorValue1, 0, 1023, 255, 0);
msensorValue2 = map (sensorValue2, 0, 1023, 255, 0);
msensorValue3 = map (sensorValue3, 0, 1023, 255, 0);
msensorValue4 = map (sensorValue4, 0, 1023, 255, 0);
msensorValue5 = map (sensorValue5, 0, 1023, 255, 0);
Serial.print(msensorValue0);
Serial.print(',');
Serial.print(msensorValue1);
Serial.print(',');
Serial.print(msensorValue2);
Serial.print(',');
Serial.print(msensorValue3);
Serial.print(',');
Serial.print(msensorValue4);
Serial.print(',');
Serial.print(msensorValue5);
Serial.print(',');
digitalWrite (dPin4, 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 (dPin4, LOW); //turn row0 off
msensorValue0 = map (sensorValue0, 0, 1023, 255, 0);
msensorValue1 = map (sensorValue1, 0, 1023, 255, 0);
msensorValue2 = map (sensorValue2, 0, 1023, 255, 0);
msensorValue3 = map (sensorValue3, 0, 1023, 255, 0);
msensorValue4 = map (sensorValue4, 0, 1023, 255, 0);
msensorValue5 = map (sensorValue5, 0, 1023, 255, 0);
Serial.print(msensorValue0);
Serial.print(',');
Serial.print(msensorValue1);
Serial.print(',');
Serial.print(msensorValue2);
Serial.print(',');
Serial.print(msensorValue3);
Serial.print(',');
Serial.print(msensorValue4);
Serial.print(',');
Serial.print(msensorValue5);
Serial.print(',');
digitalWrite (dPin3, 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 (dPin3, LOW); //turn row0 off
msensorValue0 = map (sensorValue0, 0, 1023, 255, 0);
msensorValue1 = map (sensorValue1, 0, 1023, 255, 0);
msensorValue2 = map (sensorValue2, 0, 1023, 255, 0);
msensorValue3 = map (sensorValue3, 0, 1023, 255, 0);
msensorValue4 = map (sensorValue4, 0, 1023, 255, 0);
msensorValue5 = map (sensorValue5, 0, 1023, 255, 0);
Serial.print(msensorValue0);
Serial.print(',');
Serial.print(msensorValue1);
Serial.print(',');
Serial.print(msensorValue2);
Serial.print(',');
Serial.print(msensorValue3);
Serial.print(',');
Serial.print(msensorValue4);
Serial.print(',');
Serial.print(msensorValue5);
Serial.print(',');
digitalWrite (dPin2, 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 (dPin2, LOW); //turn row0 off
msensorValue0 = map (sensorValue0, 0, 1023, 255, 0);
msensorValue1 = map (sensorValue1, 0, 1023, 255, 0);
msensorValue2 = map (sensorValue2, 0, 1023, 255, 0);
msensorValue3 = map (sensorValue3, 0, 1023, 255, 0);
msensorValue4 = map (sensorValue4, 0, 1023, 255, 0);
msensorValue5 = map (sensorValue5, 0, 1023, 255, 0);
Serial.print(msensorValue0);
Serial.print(',');
Serial.print(msensorValue1);
Serial.print(',');
Serial.print(msensorValue2);
Serial.print(',');
Serial.print(msensorValue3);
Serial.print(',');
Serial.print(msensorValue4);
Serial.print(',');
Serial.println(msensorValue5);
}
Processing program
/*
Sensor orientation: corner 0,0 top left
*/
// 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);
}
}
}
}
}