DIYstompboxes.com

DIY Stompboxes => Building your own stompbox => Topic started by: Joe on July 21, 2009, 09:34:50 PM

Title: "Trimpot Cheater"
Post by: Joe on July 21, 2009, 09:34:50 PM
Wrote this little program to calculate four series resistor values which can be used instead of a trimpot. (Hard to adjust, but you can impress your friends with your knowledge of binary math.) Will try to do a online version sometime.


/*
* "Trimpot Cheater"
* by Joe Davisson
*
* This program calculates four series
* resistors to replace a trimpot.
*
* Resistors marked "X" are left alone,
* and the ones marked "-" are jumpered.
*/

#include <stdio.h>
#include <math.h>

static float list[12] = {
1.0f, 1.2f, 1.5f, 1.8f, 2.2f, 2.7f, 3.3f, 3.9f, 4.7f, 5.6f, 6.8f, 8.2f
};

static float nearest(float val)
{
int i;

int mul = 1;
while(val > 9.999f) {
val /= 10;
mul *= 10;
}

float lowest = 99999.0f;
int use = 0;

for(i = 0; i < 12; i++) {
float d = fabs(list[i] - val);

if(d < lowest) {
lowest = d;
use = i;
}
}

return list[use] * mul;
}

int main(void)
{
int size;

printf("\n\"Trimpot Cheater\"\n");
printf("by Joe Davisson\n\n");
printf("Enter trimpot size: ");
scanf("%d", &size);

float r1 = nearest(size / 2);
float r2 = nearest(size / 4);
float r3 = nearest(size / 8);
float r4 = nearest(size / 16);

printf("\nR1 = %.0f\n", r1);
printf("R2 = %.0f\n", r2);
printf("R3 = %.0f\n", r3);
printf("R4 = %.0f\n\n", r4);

printf("Code\t1 2 3 4\t\tValue\n");
printf("----    -------         -----\n");

int i;
for(i = 0; i < 16; i++) {
float v = 0.0f;
char c1 = '-';
char c2 = '-';
char c3 = '-';
char c4 = '-';

if((i >> 3) & 1) {
v += r1;
c1 = 'X';
}
if((i >> 2) & 1) {
v += r2;
c2 = 'X';
}
if((i >> 1) & 1) {
v += r3;
c3 = 'X';
}
if((i >> 0) & 1) {
v += r4;
c4 = 'X';
}

printf("%d\t%c %c %c %c\t\t%.0f\n", i, c1, c2, c3, c4, v);
}

return 0;
}


Edit:
Sample output below for a 100K pot, only the marked resistors are active for each level. (The rest are shorted with jumpers.)


Enter trimpot size: 100

R1 = 47
R2 = 27
R3 = 12
R4 = 6

Code 1 2 3 4 Value
----    -------         -----
0 - - - - 0
1 - - - X 6
2 - - X - 12
3 - - X X 18
4 - X - - 27
5 - X - X 33
6 - X X - 39
7 - X X X 45
8 X - - - 47
9 X - - X 53
10 X - X - 59
11 X - X X 65
12 X X - - 74
13 X X - X 80
14 X X X - 86
15 X X X X 92

Title: Re: "Trimpot Cheater"
Post by: Taylor on July 22, 2009, 12:31:22 AM
Would this be used with a DIP switch to select them? Seems like that would be costlier than a trimpot.
Title: Re: "Trimpot Cheater"
Post by: bbmonster on July 22, 2009, 06:36:41 AM
You just gave me an idea unfortunately, too busy with a newborn around. A web app that can store your list of resistor or capacitor values and use that list to figure the best/closest possible combination, series or parallel, of some value that you do not have in your list. I'm an MS dude, so Silverlight would be my choice since I know it has personal storage space on the user's computer, aside from that I'm not familiar with Flash or Flex.

But in my quest for building the next great application, I'm usually late to the party so I'm guessing there is an app like this already floating around.
Title: Re: "Trimpot Cheater"
Post by: Joe on July 22, 2009, 08:54:33 AM
Was thinking more along the lines of testing stuff out on a breadboard, using little jumper wires to short the resistors.
Title: Re: "Trimpot Cheater"
Post by: Joe on July 27, 2009, 09:24:39 PM
Ok, there is an online version up.  (There is a bug in the C version, but it doesn't matter at this point.)

http://www.diystompboxes.com/analogalchemy/emh/emh.html
Title: Re: "Trimpot Cheater"
Post by: Thomeeque on July 28, 2009, 04:55:30 AM
Quote from: Joe on July 27, 2009, 09:24:39 PM
Ok, there is an online version up.  (There is a bug in the C version, but it doesn't matter at this point.)

http://www.diystompboxes.com/analogalchemy/emh/emh.html

Interesting! Maybe you could render exact value for each code (as a 3rd column of the code table) in Calculate action.. T.

Edit: As you have it in the 1st post :icon_mrgreen: