Login Shopping cart Checkout Distributors Tutorials  


Search



Join our mail list

Email:

 Subscribe
 Unsubscribe

Sketch

In order to visualize the results on a PC a sketch for Arduino and a sketch for Processing are available.


PROCEDURE

Download Arduino IDE and Processing IDE. Copy and paste the Arduino sketch to the Arduino IDE and the Processing sketch to the Processing IDE.

Upload the Arduino sketch to Arduino.

Run the Processing sketch once, on the bottom window a list of serial ports will show up. On the same window find the number associated at the serial port where Arduino is transmitting. You can see this at the bottom right corner of the Arduino 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 COM21

If you run Processing in the bottom window you will read:

[0] "COM3"
[1] "COM5"
[2] "COM6"
[3] "COM21"
[4] "COM22"
[5] "COM23" 

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

Run the processing sketch again and everything should work fine.


The following sketch is for Arduino Mega 2560 and it is for a 12 x 12 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.



/*
  This example reads the output of a 12x12 matrix resistive sensor
 and sends its values serially. The Processing program at the bottom
 take those values and use them to change the background grey level of a
 matrix on the screen.
 
The circuit:
Arduino digital outputs attached to 12 rows of sensor.
Arduino analog inputs attached to 12 columns of sensor with 4.7k pull down resistors.

Sensor orientation: row 0, column 0 are the ones near the connectors.
    
 */
const int Cols = 12; //Define cols number ( 1 >= Cols >= 16 )
const int Rows = 12; //Define rows number
const int factor = 1;    //signal amplifying factor

//declaration of the INPUT pins we will use; i is the position within the array;
int Pin[]= {A0, A2, A4, A6, A8, A10, A11, A9, A7, A5, A3, A1};

//declaration of the OUTPUT pins we will use; i is the position within the array;
int dPin[] = {23, 25, 27, 29, 31, 33, 32, 30, 28, 26, 24, 22};     // Pins 22-37 used to supply current to sensor

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 >= Cols-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] = factor * analogRead (Pin[m]); //read value column m
    // delay(1);
    if (sensorValue[m] < 1) {   // 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.
}




 





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 = 3;

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

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

int CicliInterpolazione = 3; // cycles of 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()[2], 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;
    }
}

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.


 

Currency

 


Shopping cart

There are no products in your shopping cart.