I am attempting to filter out some noise i am getting on an RA8875 touchscreen. The screen works, but there are a few "dots" that show up, and so i think if i can filter them the screen will work better.
briefly discusses how to filter a touchscreen, but the first step is to "sort the array of samples". I can't get my head around that because on a touchscreen, you have two arrays, x and y (or a 2d array of both x and y)
How do you sort them so that the pairs or x and y remain the paired?
You don't need to sort anything, you do however still need a large enough variable (an array works too) and with simple addition and division, you can smooth out your touch points.
Ex.
unsigned int X =0, Y =0;
unsigned int PrecisionX, PrecisionY;
const int samples = 5;
for(byte i = 0; i < samples; i++)
{
X += get_touchpointX;
Y += get_touchpointY;
}
//use these for your smoothed touch points
PrecisionX = X/samples;
PrecisionY = Y/samples;
These problems typically arise due to touch bounce which affects the measurement performed by the ADC because the touch sensor is being sampled when the sensor output has not settled to the final value.
Filtering works but reduces the responsiveness of the touch.
A simple approach I use in the library here is to take three x,y readings spaced 1ms apart and check they are within a defined error range. This means the library rejects spurious readings and the sketch only receives touch position values that have a high level of confidence that they are not spurious.
An extract from the library code follows:
// Get the raw contact coordinates
_xraw = _ReadAxis(_axis);
_yraw = _ReadAxis(!_axis);
// Now double check the touch is still near the initial contact point
// This helps to debounce the touch contact
// We are working with signed integers intentionally
delay(1);
if (abs(_xraw - _ReadAxis(_axis)) > _RAWERR) return false;
if (abs(_yraw - _ReadAxis(!_axis)) > _RAWERR) return false;
delay(1);
if (abs(_xraw - _ReadAxis(_axis)) > _RAWERR) return false;
if (abs(_yraw - _ReadAxis(!_axis)) > _RAWERR) return false;
// Good contact.....
You can think of this as there being a small box around the first contact point and the next two readings must be in that box. The delay between readings is important to ensure the touch signal has stabilised.
The other approach is to use a "median" rather than a simple averaging (sum and divide) approach. A median filter does not sum the values that are far from the average, whereas a simple averaging technique adds in all values.
P.S there is a "running median" library referenced on the Arduino website if you decide to go down that route.