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 2.2.1 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.5;    //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 >= 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

  // 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 = 0;

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

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

// Declare 2D array
//float[] myArray = new float[cols*rows];
int[] myArray = new int[cols*rows];
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;

import processing.serial.*;


Serial myPort;

void setup() {
  if (Orient == 0 || Orient == 2 || Orient == 5 || Orient == 7){
    size(ScrDimX, ScrDimY);
    }
  else{
    size(ScrDimY, ScrDimX);
  } 
  PixDimX=ScrDimX/cols;
  PixDimY=ScrDimY/rows;
  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;
  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;
        //show data on screen
        for (int i = 0; i < rows; i++) {
          for (int j = 0; j < cols; j++) {
            stroke(255);
//              println("valori i=" + i + " j="+ j + "val=" + int(myArray[(i*rows)+j]));
            fill(int(myArray[(i*cols)+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;
      }
    }  
  }
}

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.