Summary
Hi all!
I am trying to plot and visualise data from pressure sensor array on processing. What I am using currently is two 3x3 pressure sensor arrays. I am using two multiplexers as shown in the circuit diagram. The code that I am using is from instructable (link: https://www.instructables.com/O-mat/).
Processing: CircuitDiagram.jpg...
The arduino code is:
//Mux control pins for analog signal (SIG_pin) default for arduino mini pro
const byte s0 = A4;
const byte s1 = A3;
const byte s2 = A2;
const byte s3 = A1;
//Mux control pins for Output signal (OUT_pin) default for arduino mini pro
const byte w0 = 6;
const byte w1 = 5;
const byte w2 = 4;
const byte w3 = 3;
//Mux in "SIG" pin default for arduino mini pro
const byte SIG_pin = 0;
//Mux out "SIG" pin default for arduino mini pro
const byte OUT_pin = 2;
//Status and Column pins default for arduino mini pro
const byte STATUS_pin = 8;
const byte COL_pin = 9;
const boolean muxChannel[16][4]={
{0,0,0,0}, //channel 0
{1,0,0,0}, //channel 1
{0,1,0,0}, //channel 2
{1,1,0,0}, //channel 3
{0,0,1,0}, //channel 4
{1,0,1,0}, //channel 5
{0,1,1,0}, //channel 6
{1,1,1,0}, //channel 7
{0,0,0,1}, //channel 8
{1,0,0,1}, //channel 9
{0,1,0,1}, //channel 10
{1,1,0,1}, //channel 11
{0,0,1,1}, //channel 12
{1,0,1,1}, //channel 13
{0,1,1,1}, //channel 14
{1,1,1,1} //channel 15
};
//incoming serial byte
int inByte = 0;
int valor = 0; //variable for sending bytes to processing
int calibra[15][15]; //Calibration array for the min values of each od the 225 sensors.
int minsensor=254; //Variable for staring the min array
int multiplier = 254;
int pastmatrix[15][15];
void setup(){
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
pinMode(w0, OUTPUT);
pinMode(w1, OUTPUT);
pinMode(w2, OUTPUT);
pinMode(w3, OUTPUT);
pinMode(OUT_pin, OUTPUT);
pinMode(STATUS_pin, OUTPUT);
pinMode(COL_pin, OUTPUT);
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
digitalWrite(w0, LOW);
digitalWrite(w1, LOW);
digitalWrite(w2, LOW);
digitalWrite(w3, LOW);
digitalWrite(OUT_pin, HIGH);
digitalWrite(STATUS_pin, HIGH);
digitalWrite(COL_pin, HIGH);
Serial.begin(115200);
Serial.println("\n\Calibrating...\n");
// Full of 0's of initial matrix
for(byte j = 0; j < 15; j ++){
writeMux(j);
for(byte i = 0; i < 15; i ++)
calibra[j][i] = 0;
}
// Calibration
for(byte k = 0; k < 50; k++){
for(byte j = 0; j < 15; j ++){
writeMux(j);
for(byte i = 0; i < 15; i ++)
calibra[j][i] = calibra[j][i] + readMux(i);
}
}
//Print averages
for(byte j = 0; j < 15; j ++){
writeMux(j);
for(byte i = 0; i < 15; i ++){
calibra[j][i] = calibra[j][i]/50;
if(calibra[j][i] < minsensor)
minsensor = calibra[j][i];
Serial.print(calibra[j][i]);
Serial.print("\t");
}
Serial.println();
}
Serial.println();
Serial.print("Minimum Value: ");
Serial.println(minsensor);
Serial.println();
establishContact();
digitalWrite(COL_pin, LOW);
}
void loop(){
//Loop through and read all 16 values
//Reports back Value at channel 6 is: 346
if (Serial.available() > 0){
inByte = Serial.read();
if(inByte == 'A'){
for(int j = 14; j >= 0; j--){
writeMux(j);
for(int i = 0; i < 15; i++){
valor = readMux(i);
//Saturation sensors
int limsup = 450;
if(valor > limsup)
valor = limsup;
if(valor < calibra[j][i])
valor = calibra[j][i];
valor = map(valor,minsensor, limsup,1,254);
if(valor < 150)
valor = 0;
if(valor > 254)
valor = 254;
Serial.write(valor);
digitalWrite(COL_pin,!digitalRead(COL_pin));
}
}
}
}
}
int readMux(byte channel){
byte controlPin[] = {s0, s1, s2, s3};
//loop through the 4 sig
for(int i = 0; i < 4; i ++){
digitalWrite(controlPin[i], muxChannel[channel][i]);
}
//read the value at the SIG pin
int val = analogRead(SIG_pin);
//return the value
return val;
}
void writeMux(byte channel){
byte controlPin[] = {w0, w1, w
Summary
This text will be hidden
2, w3};
//loop through the 4 sig
for(byte i = 0; i < 4; i ++){
digitalWrite(controlPin[i], muxChannel[channel][i]);
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.print('A'); // send a capital A
delay(300);
}
}
and the processing code is:
// This example code is in the public domain.
import processing.serial.;
import processing.opengl.;
int bgcolor; // Background color
int fgcolor; // Fill color
Serial myPort; // The serial port
int[] serialInArray = new int[225]; // Where we'll put what we receive
int[] pastInArray = new int [225];
float[][] colorTarget = new float[3][255];
float[][] currentColor = new float[3][255];
PVector[][] vertices = new PVector[15][15];
float[] verticesTZ = new float[15];
float w = 30;
float ease = 0.75;
int serialCount = 0; // A count of how many bytes we receive
int xpos, ypos; // Starting position of the ball
boolean firstContact = false; // Whether we've heard from the microcontroller
int tiempoant;
int render=0;
int dif=0;
void setup() {
size(1000, 1000, OPENGL); // Stage size
noStroke(); // No border on the next thing draw
// Print a list of the serial ports, for debugging purposes:
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[0], 115200);
for (int j = 0; j < 15; j++) {
for (int i = 0; i < 15; i++) {
vertices[i][j] = new PVector( iw, jw, 0);
}
}
}
void draw() {
if (render==1) {
translate(width/4, 100);
rotateX(0.5);
//rotateX(PI/10);
background(0);
for (int j=0; j<14; j++) {
beginShape(QUAD_STRIP);
for (int i=0; i<15; i++) {
stroke(255);
fill(serialInArray[j*15+i], 0, 0);
float x = i*width/15;
float y = j*height/15;
verticesTZ[i] = serialInArray[j*15+i];
vertices[i][j].z += (verticesTZ[i]-vertices[i][j].z)*ease;
vertex( vertices[i][j].x, vertices[i][j].y, vertices[i][j].z);
vertex( vertices[i][j+1].x, vertices[i][j+1].y, vertices[i][j+1].z);
}
endShape(CLOSE);
// println();
}
render=0;
}
}
void serialEvent(Serial myPort) {
// read a byte from the serial port:
int inByte = myPort.read();
// if this is the first byte received, and it's an A,
// clear the serial buffer and note that you've
// h
// ad first contact from the microcontroller.
// Otherwise, add the incoming byte to the array:
if (firstContact == false) {
if (inByte == 'A') {
myPort.clear(); // clear the serial port buffer
firstContact = true; // you've had first contact from the microcontroller
myPort.write('A'); // ask for more
}
} else {
// Add the latest byte from the serial port to array:
serialInArray[serialCount] = inByte;
serialCount++;
// If we have 3 bytes:
if (serialCount > 224 ) {
println(millis()-tiempoant);
tiempoant = millis();
render = 1;
// Send a capital A to request new sensor readings:
myPort.write('A');
// Reset serialCount:
serialCount = 0;
}
}
}
Everything is fine and I'm able to spikes as shown on instructable. But the problem I have now is I want see the data values and map them into kPa. I don't know which line is the arduino code or processing code is taking the data values. First, I want to see what data values I'm getting from my pressure sensor matrix. Then I want to map and see the data values in kPa (the pressure values range from: 49 kPa to 12000 kPa))