Go Down

Topic: Reading from more than one pin in capacitive touch circuit (Read 533 times) previous topic - next topic

joeBwanKenobi

Hi guys, I'm fairly new to coding and I've been playing with a capacitive touch instructable http://www.instructables.com/id/Touche-for-Arduino-Advanced-touch-sensing/ .  I'm looking to modify the code so that I can connect another capacitive sensing circuit to my analog pin1 as the first input is on analog pin0.  The Arduino code I'm currently working with is:

Code: [Select]
 

 
  //****************************************************************************************
// Illutron take on Disney style capacitive touch sensor using only passives and Arduino
// Dzl 2012
//****************************************************************************************


//                              10n
// PIN 9 --[10k]-+-----10mH---+--||-- OBJECT
//               |            |
//              3.3k          |
//               |            V 1N4148 diode
//              GND           |
//                            |
//Analog 0 ---+------+--------+
//            |      |
//          100pf   1MOmhm
//            |      |
//           GND    GND



#define SET(x,y) (x |=(1<<y)) //-Bit set/clear macros
#define CLR(x,y) (x &= (~(1<<y)))       // |
#define CHK(x,y) (x & (1<<y))           // |
#define TOG(x,y) (x^=(1<<y))             //-+



#define N 160  //How many frequencies

float results[N];            //-Filtered result buffer
float freq[N];            //-Filtered result buffer
int sizeOfArray = N;


 
 

void setup()
{
 
 
 TCCR1A=0b10000010;        //-Set up frequency generator
 TCCR1B=0b00011001;        //-+
 ICR1=110;
 OCR1A=55;

 pinMode(9,OUTPUT);        //-Signal generator pin
 pinMode(8,OUTPUT);        //-Sync (test) pin

 Serial.begin(115200);

 for(int i=0;i<N;i++)      //-Preset results
   results[i]=0;         //-+
}

void loop()
{
 unsigned int d;

 int counter = 0;
 for(unsigned int d=0;d<N;d++)
 {
   int v=analogRead(0);    //-Read response signal
   CLR(TCCR1B,0);          //-Stop generator
   TCNT1=0;                //-Reload new frequency
   ICR1=d;                 // |
   OCR1A=d/2;              //-+
   SET(TCCR1B,0);          //-Restart generator

   results[d]=results[d]*0.5+(float)(v)*0.5; //Filter results
 
   freq[d] = d;

//   plot(v,0);              //-Display
//   plot(results[d],1);
 // delayMicroseconds(1);
 }


PlottArray(1,freq,results);


 TOG(PORTB,0);            //-Toggle pin 8 after each sweep (good for scope)
}
 
   byte yMSB=0, yLSB=0, xMSB=0, xLSB=0, zeroByte=128, Checksum=0;  

   void SendData(int Command, unsigned int yValue,unsigned int xValue){
   

     
     /* >=================================================================<
         y = 01010100 11010100    (x & y are 2 Byte integers)
              yMSB      yLSB      send seperately -> reciever joins them
       >=================================================================<  */
     
       yLSB=lowByte(yValue);
       yMSB=highByte(yValue);
       xLSB=lowByte(xValue);
       xMSB=highByte(xValue);        
 
     
    /* >=================================================================<
       Only the very first Byte may be a zero, this way allows the computer
       to know that if a Byte recieved is a zero it must be the start byte.
       If data bytes actually have a value of zero, They are given the value
       one and the bit in the zeroByte that represents that Byte is made
       high.  
       >=================================================================< */  
       
      zeroByte = 128;                                   // 10000000
 
      if(yLSB==0){ yLSB=1; zeroByte=zeroByte+1;}        // Make bit 1 high
      if(yMSB==0){ yMSB=1; zeroByte=zeroByte+2;}        // make bit 2 high
      if(xLSB==0){ xLSB=1; zeroByte=zeroByte+4;}        // make bit 3 high
      if(xMSB==0){ xMSB=1; zeroByte=zeroByte+8;}        // make bit 4 high
     

    /* >=================================================================<
       Calculate the remainder of: sum of all the Bytes divided by 255
       >=================================================================< */
       
      Checksum = (Command + yMSB + yLSB + xMSB + xLSB + zeroByte)%255;
     
      if( Checksum !=0 ){
       Serial.write(byte(0));            // send start bit
       Serial.write(byte(Command));      // command eg: Which Graph is this data for
       
       Serial.write(byte(yMSB));         // Y value's most significant byte  
       Serial.write(byte(yLSB));         // Y value's least significant byte  
       Serial.write(byte(xMSB));         // X value's most significant byte  
       Serial.write(byte(xLSB));         // X value's least significant byte  
       
       Serial.write(byte(zeroByte));     // Which values have a zero value
       Serial.write(byte(Checksum));     // Error Checking Byte
      }
   }  
   


     
void PlottArray(unsigned int Cmd,float Array1[],float Array2[]){
 
     SendData(Cmd+1, 1,1);                        // Tell PC an array is about to be sent                      
     delay(1);
     for(int x=0;  x < sizeOfArray;  x++){     // Send the arrays
       SendData(Cmd, round(Array1[x]),round(Array2[x]));
       //delay(1);
     }
     
     SendData(Cmd+2, 1,1);                        // Confirm arrrays have been sent
   }


From reading the code I can see that digital pin9 is sending a signal which is reduced when you touch the plant and is tested against a solid signal at pin8, and the new data is stored in filtered and unfiltered arrays.  I'm quite new to coding and I get lost when the x and y MSB LSB and byte related things are going on.  If anyone could give me some input or a clue as to what I might need to add the help would be greatly appreciated!

joeBwanKenobi

I'm using processing to draw things based on the average of a Voltage array in a SerialLink as follows:

Code: [Select]
import processing.serial.*;
int SerialPortNumber=2;
int PortSelected=2;

/*   =================================================================================      
Global variables
=================================================================================*/

int xValue, yValue, Command;
boolean Error=true;

boolean UpdateGraph=true;
int lineGraph;
int ErrorCounter=0;
int TotalRecieved=0;

/*   =================================================================================      
Local variables
=================================================================================*/
boolean DataRecieved1=false, DataRecieved2=false, DataRecieved3=false;

float[] DynamicArrayTime1, DynamicArrayTime2, DynamicArrayTime3;
float[] Time1, Time2, Time3;
float[] Voltage1, Voltage2, Voltage3;
float[] current;
float[] DynamicArray1, DynamicArray2, DynamicArray3;

float[] PowerArray= new float[0];            // Dynamic arrays that will use the append()
float[] DynamicArrayPower = new float[0];    // function to add values
float[] DynamicArrayTime= new float[0];

String portName;
String[] ArrayOfPorts=new String[SerialPortNumber];

boolean DataRecieved=false, Data1Recieved=false, Data2Recieved=false;
int incrament=0;

int NumOfSerialBytes=8;                              // The size of the buffer array
int[] serialInArray = new int[NumOfSerialBytes];     // Buffer array
int serialCount = 0;                                 // A count of how many bytes received
int xMSB, xLSB, yMSB, yLSB;                // Bytes of data

Serial myPort;                                        // The serial port object


/*   =================================================================================      
A once off serail port setup function. In this case the selection of the speed,
the serial port and clearing the serial port buffer  
=================================================================================*/

void SerialPortSetup() {

 //  text(Serial.list().length,200,200);

 portName= Serial.list()[PortSelected];
 //  println( Serial.list());
 ArrayOfPorts=Serial.list();
 println(ArrayOfPorts);
 myPort = new Serial(this, portName, 115200);
 delay(50);
 myPort.clear();
 myPort.buffer(20);
}

/* ============================================================    
serialEvent will be called when something is sent to the
serial port being used.
============================================================   */

void serialEvent(Serial myPort) {

 while (myPort.available ()>0)
 {
   /* ============================================================    
    Read the next byte that's waiting in the buffer.
    ============================================================   */

   int inByte = myPort.read();

   if (inByte==0)serialCount=0;

   if (inByte>255) {
     println(" inByte = "+inByte);    
     exit();
   }

   // Add the latest byte from the serial port to array:

   serialInArray[serialCount] = inByte;
   serialCount++;

   Error=true;
   if (serialCount >= NumOfSerialBytes ) {
     serialCount = 0;

     TotalRecieved++;

     int Checksum=0;

     //    Checksum = (Command + yMSB + yLSB + xMSB + xLSB + zeroByte)%255;
     for (int x=0; x<serialInArray.length-1; x++) {
       Checksum=Checksum+serialInArray[x];
     }

     Checksum=Checksum%255;



     if (Checksum==serialInArray[serialInArray.length-1]) {
       Error = false;
       DataRecieved=true;
     }
     else {
       Error = true;
       //  println("Error:  "+ ErrorCounter +" / "+ TotalRecieved+" : "+float(ErrorCounter/TotalRecieved)*100+"%");
       DataRecieved=false;
       ErrorCounter++;
       println("Error:  "+ ErrorCounter +" / "+ TotalRecieved+" : "+float(ErrorCounter/TotalRecieved)*100+"%");
     }
   }

   if (!Error) {


     int zeroByte = serialInArray[6];
     // println (zeroByte & 2);

     xLSB = serialInArray[3];
     if ( (zeroByte & 1) == 1) xLSB=0;
     xMSB = serialInArray[2];      
     if ( (zeroByte & 2) == 2) xMSB=0;

     yLSB = serialInArray[5];
     if ( (zeroByte & 4) == 4) yLSB=0;

     yMSB = serialInArray[4];
     if ( (zeroByte & 8) == 8) yMSB=0;


     //   println( "0\tCommand\tyMSB\tyLSB\txMSB\txLSB\tzeroByte\tsChecksum");
     //  println(serialInArray[0]+"\t"+Command +"\t"+ yMSB +"\t"+ yLSB +"\t"+ xMSB +"\t"+ xLSB+"\t" +zeroByte+"\t"+ serialInArray[7]);

     // >=====< combine bytes to form large integers >==================< //

     Command  = serialInArray[1];

     xValue   = xMSB << 8 | xLSB;                    // Get xValue from yMSB & yLSB  
     yValue   = yMSB << 8 | yLSB;                    // Get yValue from xMSB & xLSB

       // println(Command+ "  "+xValue+"  "+ yValue+" " );

     /*
How that works: if xMSB = 10001001   and xLSB = 0100 0011
      xMSB << 8 = 10001001 00000000    (shift xMSB left by 8 bits)                      
      xLSB =          01000011    
      xLSB | xMSB = 10001001 01000011    combine the 2 bytes using the logic or |
      xValue = 10001001 01000011     now xValue is a 2 byte number 0 -> 65536  
      */







     /*  ==================================================================
      Command, xValue & yValue have now been recieved from the chip
      ==================================================================  */

     switch(Command) {


       /*  ==================================================================
        Recieve array1 and array2 from chip, update oscilloscope      
        ==================================================================  */

     case 1: // Data is added to dynamic arrays
       DynamicArrayTime3=append( DynamicArrayTime3, (xValue) );
       DynamicArray3=append( DynamicArray3, (yValue) );

       break;

     case 2: // An array of unknown size is about to be recieved, empty storage arrays
       DynamicArrayTime3= new float[0];
       DynamicArray3= new float[0];
       break;    

     case 3:  // Array has finnished being recieved, update arrays being drawn
       Time3=DynamicArrayTime3;
       Voltage3=DynamicArray3;
       //   println(Voltage3.length);
       DataRecieved3=true;
       break;  

       /*  ==================================================================
        Recieve array2 and array3 from chip
        ==================================================================  */


     case 4: // Data is added to dynamic arrays
       DynamicArrayTime2=append( DynamicArrayTime2, xValue );
       DynamicArray2=append( DynamicArray2, (yValue-16000.0)/32000.0*20.0  );
       break;

     case 5: // An array of unknown size is about to be recieved, empty storage arrays
       DynamicArrayTime2= new float[0];
       DynamicArray2= new float[0];
       break;    

     case 6:  // Array has finnished being recieved, update arrays being drawn
       Time2=DynamicArrayTime2;
       current=DynamicArray2;
       DataRecieved2=true;
       break;  

       /*  ==================================================================
        Recieve a value of calculated power consumption & add it to the
        PowerArray.
        ==================================================================  */
     case 20:  
       PowerArray=append( PowerArray, yValue );

       break;

     case 21:  
       DynamicArrayTime=append( DynamicArrayTime, xValue );
       DynamicArrayPower=append( DynamicArrayPower, yValue );



       break;
     }
   }
 }
 redraw();  
 //    }
}

Go Up