chipKIT® Development Platform

Inspired by Arduino™

Can't get 4 Wire Touch to work

Created Fri, 25 May 2012 02:53:36 +0000 by BoH_Havoc


BoH_Havoc

Fri, 25 May 2012 02:53:36 +0000

Hi, i somehow can't get a simple 4 wire touch panel to work. I either get nothing or garbage. When i was still using an Arduino i wrote myself a library for touch handling, but the library does not work anymore. I also tried a number of online touchpanel tutorials which were written for the arduino (some have 10k pulldown resistors, some have not. When using an Arduino i never needed pulldown resistors by the way) but they also didn't work.

Any help? :)

The library i wrote in the past: Touchpanel.h

/*

Touchpanel Library by Wolfgang "BoH_Havoc" Reichardt
based on http://kalshagar.wikispaces.com/Arduino+and+a+Nintendo+DS+touch+screen

Create a Touchpanel instance by calling
Touchpanel tpanel(int inXLow, int inXHigh, int inYLow, int inYHigh, int inSensitivity)
	inXLow, inXHigh, inYLow, inYHigh = analog input pins for 4-wire touchpad
	inSensitivity = minimum xy-value at which to return "true" for checkTouch (100 is a good starting point)
	
Then call tpanel.checkTouch() 3 times (with a delay of min. 10 in between). If tpanel.checkTouch returns true, there was a touch.
You can then access the touchposition by tpanel.x and tpanel.y

example:

#include <Touchpanel.h>

//create touchpanel instance
Touchpanel tpanel(14,15,16,17, 100);

void setup()
{
  Serial.begin(9600);
}

void loop(){

  
  if( tpanel.checkTouch() )
  {
    Serial.print(tpanel.x,DEC);   
    Serial.print(",");     
    Serial.println(tpanel.y,DEC); 
  }
  
  delay (10);
  
  
}

*/

#ifndef Touchpanel_h
#define Touchpanel_h

#include "WProgram.h"

class Touchpanel
{
  public:
	Touchpanel(int inXLow, int inXHigh, int inYLow, int inYHigh, int inSensitivity);
	boolean checkTouch();
	int x;
	int y;
  private:
	int touchPhase;
	int sensitivity;
	int xLow;
	int xHigh;
	int yLow;
	int yHigh;
	
};

#endif

Touchpanel.c

#include "Touchpanel.h"

Touchpanel::Touchpanel(int inXLow, int inXHigh, int inYLow, int inYHigh, int inSensitivity)
{
	x = 0;
	y = 0;
	touchPhase = 0;
	sensitivity = inSensitivity;
	xLow = inXLow;
	xHigh = inXHigh;
	yLow = inYLow;
	yHigh = inYHigh;
	
}

boolean Touchpanel::checkTouch()
{
	if(touchPhase == 1)
	{
		x = 0;
		y = 0;
		pinMode(xLow,OUTPUT);
		pinMode(xHigh,OUTPUT);
		digitalWrite(xLow,LOW);
		digitalWrite(xHigh,HIGH);
		 
		digitalWrite(yLow,LOW);
		digitalWrite(yHigh,LOW);
		 
		pinMode(yLow,INPUT);
		pinMode(yHigh,INPUT);
		
		//touch phase not possible as we are prepraing for touch, so return false
		touchPhase = 2;
		return false;
	}
	//delay(10);
 
	if(touchPhase == 2) 
	{
		//xLow has analog port -14 !!
	   x=analogRead(yLow -14);
	 
	   pinMode(yLow,OUTPUT);
	   pinMode(yHigh,OUTPUT);
	   digitalWrite(yLow,LOW);
	   digitalWrite(yHigh,HIGH);
	 
	   digitalWrite(xLow,LOW);
	   digitalWrite(xHigh,LOW);
	 
	   pinMode(xLow,INPUT);
	   pinMode(xHigh,INPUT);
		
		//touch phase not possible as we are STILL prepraing for touch, so return false
		touchPhase = 3;
		return false;
	}
	
	
	//check for touch
		touchPhase = 1;
		//xLow has analog port -14 !!
		y = analogRead(xLow - 14);
		if(x > sensitivity && y > sensitivity)
		{
			//touch detected
			return true;
		}
	//
	
	return false;
}

BoH_Havoc

Sun, 27 May 2012 15:54:20 +0000

Do i need something like this http://www.mikroe.com/eng/downloads/get/1557/ to get it working on the max32 ?

Still can't get it to work. I even tried 2 different touchpanels to make sure the one i'm testing with isn't faulty. But it get the same garbage output, so it's definately not the panels. If i directly connect the panel to A0-A3 i get a neverending list of values between 900 and 1000. If i use 4.7k pulldown resistors i get 0, no matter if i touch or don't touch the panel. If i use 100Ohm resistors in between the max32 and the panel i get 900-1000 values, but get 0 if i touch the panel.

This is starting to get frustrating ;)


BoH_Havoc

Mon, 04 Jun 2012 21:38:57 +0000

I have it working now. Here's the new library for uno32/max32. It's not as fast as it could be due to heavy use of analogRead() and pinMode(), but at least it's working and customizable.

Also needs a little more work on the touch detection (it's working, but a little too hacky for my taste ;) ).

TouchpanelPic32.h

/*

Touchpanel Library by Wolfgang "BoH_Havoc" Reichardt

for chipKIT uno32/max32


Touchpanel with touchsensitiv side facing the viewer

          [T]OP
      ------------
      |				|
[L]EFT|				|[R]IGHT
      |				|
      |				|
      ------------
        ||||[B]OTTOM
        ||||
        ||||
        TRBL    
  



Create a Touchpanel instance by calling
TouchpanelPic32(int inPinLeft, int inPinRight, int inPinBottom, int inPinTop, int inMinValue, int inMaxValue)
	inPinLeft, inPinRight, inPinBottom, inPinTop = analog input pins for 4-wire touchpad
	inMinValue, inMaxValue = minimum/maximum xy-value at which to return "true" for a touch (150 & 800 is a good starting point)
	
Call touchpanel.GetTouch() 3 times (with a delay of min. 5 in between). If touchpanel.GetTouch() returns true, there was a valid touch.
You can then access the touchposition by touchpanel.x and touchpanel.y

Example:

#include <TouchpanelPic32.h>
TouchpanelPic32 touchpanel(A0,A2,A1,A3, 150, 800);

void setup()
{
  Serial.begin(9600);
}

void loop(){

  
  if( touchpanel.GetTouch() )
  {
    Serial.print(touchpanel.x,DEC);   
    Serial.print(",");     
    Serial.println(touchpanel.y,DEC); 
  }
  
  delay (5);
  
  
}

*/

#ifndef TouchpanelPic32_h
#define TouchpanelPic32_h

#include "WProgram.h"

class TouchpanelPic32
{
  public:
	TouchpanelPic32(int inPinLeft, int inPinRight, int inPinBottom, int inPinTop, int inMinValue, int inMaxValue);
	boolean GetTouch();
	int x;
	int y;
  private:
	int touchPhase;
	int minValue, maxValue;
	int pinLeft, pinRight, pinBottom, pinTop;
};

#endif

TouchpanelPic32.cpp

#include "TouchpanelPic32.h"

TouchpanelPic32::TouchpanelPic32(int inPinLeft, int inPinRight, int inPinBottom, int inPinTop, int inMinValue, int inMaxValue)
{
	x = 0;
	y = 0;
	touchPhase = 1;
	minValue = inMinValue;
	maxValue = inMaxValue;
	
	pinLeft = inPinLeft;
	pinRight = inPinRight;
	pinBottom = inPinBottom;
	pinTop = inPinTop;
}

boolean TouchpanelPic32::GetTouch()
{
	
	if(touchPhase == 1)
	{
		//read X
	  pinMode(pinRight, INPUT);
	  pinMode(pinLeft, INPUT);
	  x = analogRead(pinLeft);
	  //prepare Y for next read
	  pinMode(pinRight, OUTPUT);
	  pinMode(pinLeft, OUTPUT);
	  analogWrite(pinLeft, 0);
	  analogWrite(pinRight, 255);
		
		touchPhase++;
		return false;
	}
	if(touchPhase == 2) 
	{
		//read Y
	  pinMode(pinBottom, INPUT);
	  pinMode(pinTop, INPUT);
	  y = analogRead(pinTop);
	  //prepare X for next read
	  pinMode(pinBottom, OUTPUT);
	  pinMode(pinTop, OUTPUT);
	  analogWrite(pinBottom, 0);
	  analogWrite(pinTop, 255);

  		touchPhase++;
		return false;
	}
	
	
	touchPhase = 1;
	if(
		(x > minValue && y > minValue)
		&& (x < maxValue && y < maxValue) 
		)
	{
		//touch detected
		return true;
	}
	
	return false;
	
}