.
.
.
TOUCH SENSOR MINI MATRIX 3×3
ARDUINO CODE
/* Touch sensor mini Matrix 3×3 : A0 A1 A2
* 1 2 3 A3
* 4 5 6 A4
* 7 8 9 A5
*
*/
#define numRows 3
#define numCols 3
int rows[] = {
A3, A4, A5};
int cols[] = {
A0, A1, A2};
int incomingValues[9] = {
};
void setup() {
set all rows and columns to INPUT (high impedance):
for(int i=0; i<numRows; i++){
pinMode(rows[i], INPUT);
}
for(int i=0; i<numCols; i++){
pinMode(cols[i], INPUT);
}
Serial.begin(9600);
}
void loop() {
for(int colCount=0; colCount<numCols; colCount++){
pinMode(cols[colCount], OUTPUT); set as OUTPUT
digitalWrite(cols[colCount], LOW); set LOW
for(int rowCount=0; rowCount<numRows; rowCount++){
pinMode(rows[rowCount], INPUT_PULLUP); set as INPUT with PULLUP RESISTOR
delay(1);
incomingValues[ ( (colCount)*numRows) + (rowCount)] = analogRead(rows[rowCount]); read INPUT
set pin back to INPUT
pinMode(rows[rowCount], INPUT);
} end rowCount pinMode(cols[colCount], INPUT); set back to INPUT!
} end colCount
Print the incoming values of the grid:
for(int i=0; i<9; i++){
Serial.print(incomingValues[i]);
if(i<8) Serial.print(“,”);
}
Serial.println();
}
.
.
.
TOUCH SENSOR MINI MATRIX 3×3
PROCESSING CODE
import processing.serial.*;
Serial myPort; The serial port
int maxNumberOfSensors = 9;
float[] sensorValue = new float[maxNumberOfSensors]; global variable for storing mapped sensor values
float[] previousValue = new float[maxNumberOfSensors]; array of previous values
int rows = 3;
int cols = 3;
int rectSize = 800/6;
int averageMIN = 0; level of grey
int averageMAX = 200; level of grey
void setup () {
size(400, 400); set up the window to whatever size you want
println(Serial.list()); List all the available serial ports
String portName = Serial.list()[6];
myPort = new Serial(this, portName, 9600);
myPort.clear();
myPort.bufferUntil('\n'); don't generate a serialEvent() until you get a newline (\n) byte
background(255); set inital background
smooth(); turn on antialiasing
rectMode(CORNER);
}
void draw () {
for (int r=0; r<rows; r++) {
for (int c=0; c<cols; c++) {
fill(sensorValue[(c*rows) + r]);
rect(r*rectSize, c*rectSize, rectSize, rectSize);
} end for cols
} end for rows
}
void serialEvent (Serial myPort) {
String inString = myPort.readStringUntil('\n'); get the ASCII string
if (inString != null) { if it's not empty
inString = trim(inString); trim off any whitespace
int incomingValues[] = int(split(inString, “,”)); convert to an array of ints
if (incomingValues.length ⇐ maxNumberOfSensors && incomingValues.length > 0) {
for (int i = 0; i < incomingValues.length; i++) {
map the incoming values (0 to 1023) to an appropriate gray-scale range (0-255):
sensorValue[i] = map(incomingValues[i], averageMIN, averageMAX, 0, 255);
println(sensorValue[i]);
}
}
}
}