OnewireKeypad
OnewireKeypad copied to clipboard
Better voltage distance by swapping the 4k7 and 1k values.
Hi, I have made a quick and dirty test sketch to find the best voltage distance between the keys. It turns out that if the high-side resistors at the rows are 1k and the low-side resistors at the columns are 4k7, then I get better results. My test-sketch is based on your ShowRange.ino sketch.
A few results of the minimum voltage distance between keys with certain resistors:
4k7 and 1k : 0.01615
1k and 4k7 : 0.05183
1k and 3k9 : 0.05957
2k7 and 10k : 0.06182
4k7 and 18k : 0.06035
Have you already optimized the results with 1k and 4k7 values ? Did you swap them or does my sketch swap them ?
float voltages[16];
void setup()
{
Serial.begin(115200);
Serial.println( "---START------------------");
Serial.println( "Finding the best resistors values,");
Serial.println( "for the maximum voltage distance between keys.");
Serial.println( "A few fixed values:");
float x;
Serial.print( " 4k7 and 1k \t: ");
fillValues( 4, 4, 4700, 1000, 5);
x = findMin();
Serial.println( x, 5);
Serial.print( " 1k and 4k7 \t: ");
fillValues( 4, 4, 1000, 4700, 5);
x = findMin();
Serial.println( x, 5);
Serial.print( " 1k and 3k9 \t: ");
fillValues( 4, 4, 1000, 3900, 5);
x = findMin();
Serial.println( x, 5);
Serial.print( " 2k7 and 10k \t: ");
fillValues( 4, 4, 2700, 10000, 5);
x = findMin();
Serial.println( x, 5);
Serial.print( " 4k7 and 18k \t: ");
fillValues( 4, 4, 4700, 18000, 5);
x = findMin();
Serial.println( x, 5);
Serial.println( "Calculate resistors...");
float maxDistance = 0.0;
for( long r1 = 100; r1<22000; r1+=100)
{
for( long r2 = 100; r2<22000; r2+=100)
{
fillValues( 4, 4, r1, r2, 5);
float minimal = findMin();
// Don't print values that are too close to zero.
// Also print values that are close to the best value.
if( minimal > (maxDistance - 0.0001) && minimal > 0.001)
{
maxDistance = minimal;
Serial.print( " ");
Serial.print( r1);
Serial.print( ",\t");
Serial.print( r2);
Serial.print( "\t : ");
Serial.print( maxDistance, 5);
Serial.println();
}
}
}
Serial.println( "---THE-END----------------");
}
void loop()
{
}
void fillValues(int rows, int cols, long Rrows, long Rcols, int Volt)
{
for( int R = 0; R < rows; R++)
{
for( int C = 0; C<cols; C++)
{
float V = (5.0f * float( Rcols )) / (float(Rcols) + (float(Rrows) * R) + (float(Rcols) * C));
voltages[(R*4) + C] = V;
// Serial.print(V, 3); Serial.print(F("\t"));
}
// Serial.println();
}
}
float findMin()
{
float absdist[16];
// find minimal distance for each one, in positive distance
for( int i=0; i<16; i++)
{
float min = 100.0;
for( int j=0; j<16; j++)
{
if( i != j) // don't compare to own voltage
{
float distance = voltages[j] - voltages[i];
if( distance < 0.0)
distance = -distance;
if( distance < min)
min = distance;
}
}
absdist[i] = min;
}
// printValues( absdist);
// find overal minimal distance
float overallmin = 100.0;
for( int i=0; i<16; i++)
{
if( absdist[i] < overallmin)
overallmin = absdist[i];
}
return( overallmin);
}
void printValues( float data[])
{
for( int i=0; i<4; i++)
{
for( int j=0; j<4; j++)
{
Serial.print(data[(i*4)+j], 3);
Serial.print(F("\t"));
}
Serial.println();
}
}