Login Shopping cart Checkout Distributors Tutorials  


Search



Join our mail list

Email:

 Subscribe
 Unsubscribe

Sketch

In order to visualize a 16 x 16 pressure map on a PC a sketch for Arduino and a sketch for Processing are available.


PROCEDURE

Download Arduino IDE from http://arduino.cc and Processing IDE from https://processing.org/. Install the drivers for the Arduino.

Copy and paste the Arduino sketch into the Arduino IDE.  

Upload the Arduino sketch to Arduino.

On the Arduino IDE go on 'Tools' and 'Port'. Check which serial port has been assigned to Arduino Mega 2560.  If you read the following:

[0]"COM3"
[1]"COM5"
[2]"COM6"
[3]"COM21"
[4]"COM22"
[5] "COM23"  (Arduino Mega or Mega 2560)

The number you are looking for is 5.

Copy and paste the Processing sketch into the Processing IDE.

 In the Processing sketch find this line:  myPort = new Serial(this, Serial.list()[0], 38400);

Enter, between the square brackets,  the same number associated to the serial port where Arduino is transmitting.

Example:

On the Arduino IDE you read: Arduino Mega 2560 connected on COM23 and COM23 is the fifth from the top (start counting from 0), then enter 5 in the square brackets.

Replace 5 with 3 in this line of the Processing sketch: myPort = new Serial(this, Serial.list()[5], 38400);

Run the processing sketch again and everything should work fine.


The following sketch is for Arduino Mega 2560 and it is for a 16 x 16 sensor.  

IMPORTANT NOTE:
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY expressed or implied, including the implied warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

//BEGINS ARDUINO SKETCH

const int Cols = 16; //Define cols number ( 1 >= Cols >= 16 )
const int Rows = 16; //Define rows number

const int Level = 1.2; //define amplitude of signal mapping

//declaration of the INPUT pins we will use; i is the position within the array;
//i = 0 corresponds to output 2 (questo commento mi sembra errato ??!!??)
//si deve dichiarare tanti canali analogici quante sono le colonne dichiarate sopra.
int Pin[]= {A15, A14, A13, A12, A11, A10, A9, A8, A7, A6, A5, A4, A3, A2, A1, A0};

//si deve dichiarare tante line di IO digitali quante righe sono dichiarate sopra
//int dPin[] = {22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 18, 19, 20, 21}; //declaration of the OUTPUT pins we will use; i is the position within the array; i = 0 corresponds to output 2
//int dPin[] = {23, 25, 27, 29, 31, 33, 32, 30, 28, 26, 24, 22}; // Pins 22-37 used to supply current to sensor
int dPin[] = {23, 22, 25, 24, 27, 26, 29, 28, 31, 30, 33, 32, 35, 34, 37, 36}; //declaration of the OUTPUT pins we will use;

int i = 0;
int j = 0;
int k = 0;
int n = 0;
int m = 0;

int sensorValue[Cols];
int msensorValue[Cols];


// Define various ADC prescaler
const unsigned char PS_16 = (1 << ADPS2);
const unsigned char PS_32 = (1 << ADPS2) | (1 << ADPS0);
const unsigned char PS_64 = (1 << ADPS2) | (1 << ADPS1);
const unsigned char PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);


void setup() {

for (int k = Rows-1; k >= 0 ; k--) {
pinMode(dPin[k], OUTPUT); //declaration of pin[i] as an OUTPUT
}

for (j = 0; j >= Rows-1 ; j++) {
pinMode (Pin[i], INPUT);
}
// Serial.begin(57600); //turn serial on
// Serial.begin(115200); //turn serial on
Serial.begin(38400); //turn serial on
//Serial.begin(9600); //turn serial on

// set up the ADC
ADCSRA &= ~PS_128; // remove bits set by Arduino library

// you can choose a prescaler from above.
// PS_16, PS_32, PS_64 or PS_128
ADCSRA |= PS_32; // set our own prescaler to 32

}

void loop(){

//Send start pattern
Serial.write( B10101010); //AA
Serial.write( B01010101); //55

for (int i = Rows-1; i >= 0 ; i--) {

digitalWrite (dPin[i], HIGH); //turn row i on

for (int m = Cols-1; m >= 0 ; m--) {
sensorValue[m] = analogRead (Pin[m]) * Level; //read value column m, multiply by Level
// delay(1);
if (sensorValue[m] < 120) { // this is to eliminate noise
sensorValue[m] = 0;
}

msensorValue[m] = map (sensorValue[m], 0, 1024, 255, 0); //map all values read to a new range from 255 to 0

// Serial.print(msensorValue[m]); //print first value on the serial port
Serial.write(msensorValue[m]);
}
digitalWrite (dPin[i], LOW); //turn row i off
}
//Send end frame pattern
Serial.write( B11110000); //F0
Serial.write( B00001111); //0F

//Serial.println (); //print a blank line - this line is important because Processing will start a serial event only after reading this blank line.
}

//ENDS ARDUINO SKETCH

****************************************************************************************************

//BEGINS PROCESSING SKETCH


/*
Sensor orientation: corner 0,0
 face up
 0=top right
 1=bottom right
 2=bottom left
 3=top left

 face down
 4=top right
 5=bottom right
 6=bottom left
 7=top left

 */
int Orient = 7;

// Number of columns and rows in the grid
int cols = 16;
int rows = 16;
// Window dimension
int ScrDimX = 740;
int ScrDimY = 740;

int PixDimX;
int PixDimY;
int ScrDimMUX;
int ScrDimMUY;

int CicliInterpolazione = 2; //change this number for interpolation cycles 0=no interpolation, 3 = max interpolation.
// Declare 2D array
//float[] myArray = new float[cols*rows];
int[] myArray = new int[cols*rows*int(pow(4,CicliInterpolazione))];
byte[] BuffArray = new byte[cols*rows];

// binary receive
int State; //-1 = waiting for 1 start pattern hAA
//-2 = waiting for 2 start pattern h55
//-3 = waiting for 1 end pattern hF0
//-4 = waiting fot 2 end pattern h0F
//>0 caracter to be received to fill the frame

int hAA = unhex("AA");
int h55 = unhex("55");
int hF0 = unhex("F0");
int h0F = unhex("0F");

int ArrIndex;
boolean exitloop;
int lastTime, now;

int vCols; //Visual Cols
int vRows; //Visual Rows

import processing.serial.*;


Serial myPort;

void setup() {
  vRows=rows;
  vCols=cols;
  for (int i = 1; i <= CicliInterpolazione; i++) {
    vRows=vRows*2-1;
    vCols=vCols*2-1;
  }
  if (Orient == 0 || Orient == 2 || Orient == 5 || Orient == 7){
    //size(ScrDimX, ScrDimY);
    surface.setSize(ScrDimX,ScrDimY);
    }
  else{
    //size(ScrDimY, ScrDimX);
    surface.setSize(ScrDimY,ScrDimX);
  }
  PixDimX=ScrDimX/vCols;
  PixDimY=ScrDimY/vRows;
  ScrDimMUX=ScrDimX-PixDimX;
  ScrDimMUY=ScrDimY-PixDimY;
  // 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()[0], 38400);
//  myPort = new Serial(this, Serial.list()[0], 115200);
//  myPort = new Serial(this, Serial.list()[0], 9600);
//  myPort = new Serial(this, Serial.list()[1], 57600);
  // don't generate a serialEvent() unless you get a newline character:
  // myPort.bufferUntil('\n');
  State = -1; //waiting for 1 start pattern
  ArrIndex = 0;
  exitloop = false;
}

void draw() {
  int inByte=0;
  int valore;

  exitloop = false;
  while ((myPort.available() > 0) && !exitloop) {
    if (State < 0) {
      inByte = myPort.read();
      println(inByte, State, myPort.available());
    }
    if (State == -1) {
      if (inByte == hAA ) {
        State = -2; //first pattern OK - Wait for the second
      }
    }
    else if (State == -2) {
      if (inByte == h55 ) {
        State = cols * rows; //second pattern OK - byte to get
      }
    }
    else if (State == -3) {
      if (inByte == hF0 ) {
        State = -4; //first pattern OK - Wait for the second
      }
      else
      {
        print("-");
      }
    }
    else if (State == -4) {
      if (inByte == h0F ) {
        exitloop = true;
        now = millis();
        println(now- lastTime);
        lastTime = now;
        State = -1; //wait for next frame
        ArrIndex = 0;
        //esegue interpolazione
        for (int CI=0;CI<CicliInterpolazione; CI++) {
          Interpola(CI);
        }
        //show data on screen
        colorMode(HSB,350,300,255);
        strokeWeight(0);
        for (int i = 0; i < vRows; i++) {
          for (int j = 0; j < vCols; j++) {
            valore=myArray[(i*vCols)+j];
            if (valore<210) {
              stroke(valore,300,255);
              fill(valore,300,255);
            }
            else {
              stroke(valore,300+((210-valore)*8),255);
              fill(valore,300+((210-valore)*8),255);
            }
//             println("valori i=" + i + " j="+ j + "val=" + int(myArray[(i*vCols)+j]));
            switch(Orient) {
            case 0:
              rect(j*PixDimX, ScrDimMUY-(i*PixDimY), PixDimX, PixDimY);
              break;
            case 1:
              rect(i*PixDimY, j*PixDimX, PixDimY, PixDimX);
              break;
            case 2:
              rect(ScrDimMUX-(j*PixDimX), i*PixDimY, PixDimX, PixDimY);
              break;
            case 3:
              rect(ScrDimMUY-(i*PixDimY), ScrDimMUX-(j*PixDimX), PixDimY, PixDimX);
              break;
            case 4:
              rect(i*PixDimY, ScrDimMUX-(j*PixDimX), PixDimY, PixDimX);
              break;
            case 5:
              rect(j*PixDimX, i*PixDimY, PixDimX, PixDimY);
              break;
            case 6:
              rect(ScrDimMUY-(i*PixDimY), j*PixDimX, PixDimY, PixDimX);
              break;
            case 7:
              rect(ScrDimMUX-(j*PixDimX), ScrDimMUY-(i*PixDimY), PixDimX, PixDimY);
              break;
            }
          }
        }
      }
    }
    if (State > 0) {
      if (myPort.available() >= State)    //wait for a complete frame
      {
        myPort.readBytes(BuffArray);
        for (int i = 0; i < State; i++){
          myArray[i]=int(BuffArray[i]);
        }
        State = -3;
      }
      else {
        print(".", myPort.available());
        exitloop = true;
      }
    }
  }
}

void Interpola(int Ciclo) {
  int cCols; //contatore colonne
  int cRows; //contatore righe
  int bCols; //numero colonne array grande
  int bRows; //numero righe array grande
  int OldCols; //numero colonne array piccolo
  int OldRows; //numero righe array piccolo

  OldRows=rows;
  OldCols=cols;
  for (int i = 1; i <= Ciclo; i++) {
    OldRows=OldRows*2-1;
    OldCols=OldCols*2-1;
  }
  bRows=OldRows*2-1;
  bCols=OldCols*2-1;

  int pArr;
  int pArrBig;
  int a,b,c,d;

  //allarga i punti intervallandoli partendo dalla fine dell'array
   pArr=OldRows*OldCols-1;
   pArrBig=bRows*bCols-1;


   while (pArrBig>=0 ){
     //copia una riga con valori
     cCols=OldCols;
     while (cCols>0 ){
       myArray[pArrBig]=myArray[pArr];
       if (pArr!=0){
         myArray[pArr]=0;
       }
       pArr--;
       pArrBig=pArrBig-2;
       cCols--;
     }
     //salta una riga vuota
     pArrBig=pArrBig - bCols + 1;
   }

   //first interpolation step (X interpolation)
    pArrBig=bCols+1;
    cRows=1;
    while (cRows<bRows-1){
      cCols=1;
      while (cCols<bCols-1){
        a=myArray[pArrBig-bCols-1];
        b=myArray[pArrBig-bCols+1];
        c=myArray[pArrBig+bCols-1];
        d=myArray[pArrBig+bCols+1];
        myArray[pArrBig]=(a+b+c+d)/4;
        pArrBig=pArrBig+2;
        cCols=cCols+2;
      }
      cRows=cRows+2;
      pArrBig=pArrBig+bCols+1;
    }

   //border cell interpolation (| interpolation)
    //prima riga
    pArrBig=0;
    cCols=0;
    while (cCols<bCols-2){
      a=myArray[pArrBig];
      b=myArray[pArrBig+2];
      myArray[pArrBig+1]=(a+b)/2;
      pArrBig=pArrBig+2;
      cCols=cCols+2;
    }

  //ultima riga
    pArrBig=(bRows-1)*(bCols);
    cCols=0;
    while (cCols<bCols-2){
      a=myArray[pArrBig];
      b=myArray[pArrBig+2];
      myArray[pArrBig+1]=(a+b)/2;
      pArrBig=pArrBig+2;
      cCols=cCols+2;
    }

     //prima colonna
    pArrBig=bCols;
    cRows=1;
    while (cRows<bRows-1){
      a=myArray[pArrBig-bCols];
      b=myArray[pArrBig+bCols];
      myArray[pArrBig]=(a+b)/2;
      pArrBig=pArrBig+bCols*2;
      cRows=cRows+2;
    }
     //ultima colonna
    pArrBig=bCols*2-1;
    cRows=1;
    while (cRows<bRows-1){
      a=myArray[pArrBig-bCols];
      b=myArray[pArrBig+bCols];
      myArray[pArrBig]=(a+b)/2;
      pArrBig=pArrBig+bCols*2;
      cRows=cRows+2;
    }

    //second interpolation step (+ interpolation) even rows
    pArrBig=bCols+2;
    cRows=1;
    while (cRows<bRows-1){
      cCols=2;
      while (cCols<bCols-2){
        a=myArray[pArrBig-bCols];
        b=myArray[pArrBig-1];
        c=myArray[pArrBig+1];
        d=myArray[pArrBig+bCols];
        myArray[pArrBig]=(a+b+c+d)/4;
        pArrBig=pArrBig+2;
        cCols=cCols+2;
      }
      cRows=cRows+2;
      pArrBig=pArrBig+bCols+3;
    }

  //second interpolation step (+ interpolation) odd rows
    pArrBig=bCols*2+1;
    cRows=2;
    while (cRows<bRows-1){
      cCols=1;
      while (cCols<bCols-1){
        a=myArray[pArrBig-bCols];
        b=myArray[pArrBig-1];
        c=myArray[pArrBig+1];
        d=myArray[pArrBig+bCols];
        myArray[pArrBig]=(a+b+c+d)/4;
        pArrBig=pArrBig+2;
        cCols=cCols+2;
      }
      cRows=cRows+2;
      pArrBig=pArrBig+bCols+1;
    }
}
//ENDS PROCESSING SKETCH







 

Currency

 


Shopping cart

There are no products in your shopping cart.