-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResistor Calculator.bak
496 lines (469 loc) · 19.8 KB
/
Resistor Calculator.bak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
<!DOCTYPE html>
<head>
<p>
<strong>Note:</strong> Resistor Calculator is not supported in Internet Explorer 8 and earlier versions.
</p>
</head>
<body onload="init()">
<canvas id="Resistor Canvas" width="600" height="140" style="border:1px solid #d3d3d3;"> Your browser does not support the HTML5 canvas tag.
</canvas>
<script>
var img = new Image();
var img_x_start = 15;
var img_y_start = 15;
var img_orig_width = 569;
var img_orig_height = 106;
var resistor_URL;
function init() {
Resistor_Calculator();
calculate_resistor_value();
}
function Resistor_Calculator() {
var context = document.getElementById('Resistor Canvas').getContext("2d");
img.src = "resistor base.png";
img.onload = function () {
context.drawImage(img, img_x_start, img_y_start);
create_band(1, getColor(document.getElementById("band1")));
create_band(2, getColor(document.getElementById("band2")));
create_band(4, getColor(document.getElementById("multiplier")));
create_band("tolerance", getColor(document.getElementById("tolerance")));
}
}
function getColor(object) {
return remove_whitespace(object.textContent.split("\n")[parseInt(object.value) + 1]);
}
function remove_whitespace(string) {
var i = 0;
for (i = 0; i < string.length; i++) {
if (string[i] != ' ')
break;
}
return string.substring(i, string.length);
}
function add_band(object) {
if (parseInt(object.value) == 4) {
document.getElementById("band3_label").setAttribute("style", "visibility: hidden");
document.getElementById("band3").setAttribute("style", "visibility: hidden");
remove_band(3);
} else {
document.getElementById("band3_label").setAttribute("style", "visibility: visible");
document.getElementById("band3").setAttribute("style", "visibility: visible");
create_band(3, getColor(document.getElementById("band3")));
}
calculate_resistor_value();
}
function create_band(band_number, color) {
drawBand(band_number, color);
}
function remove_band(band_number) {
drawBand(band_number, "remove");
}
function redraw(object) {
change_band_color(object);
calculate_resistor_value();
}
function change_band_color(object) {
switch (object.id) {
case "band1":
drawBand(1, getColor(object));
break;
case "band2":
drawBand(2, getColor(object));
break;
case "band3":
drawBand(3, getColor(object));
break;
case "multiplier":
drawBand(4, getColor(object));
break;
case "tolerance":
drawBand("tolerance", getColor(object));
break;
}
}
function calculate_resistor_value() {
var resistor_value;
var first_digit = parseInt(document.getElementById("band1").value) + 1;
var second_digit = parseInt(document.getElementById("band2").value);
var third_digit = parseInt(document.getElementById("band3").value);
var multiplier = parseInt(document.getElementById("multiplier").value);
var tolerance = parseInt(document.getElementById("tolerance").value);
var power = document.getElementById("power").value;
resistor_URL = "https://circuitspecialists.com/"
if (parseInt(document.getElementById("number_of_bands").value) == 4) {
switch (multiplier) {
case 0:
if (second_digit > 0) {
resistor_value = "." + first_digit.toString() + second_digit.toString();
} else {
resistor_value = first_digit.toString();
}
break;
case 1:
if (second_digit > 0) {
resistor_value = first_digit.toString() + "." + second_digit.toString();
} else {
resistor_value = first_digit.toString();
}
break;
case 2:
resistor_value = first_digit.toString() + second_digit.toString();
break;
case 3:
resistor_value = first_digit.toString() + second_digit.toString() + "0";
break;
case 4:
if (second_digit > 0) {
resistor_value = first_digit.toString() + "." + second_digit.toString() + "k";
} else {
resistor_value = first_digit.toString() + "k";
}
break;
case 5:
resistor_value = first_digit.toString() + second_digit.toString() + "k";
break;
case 6:
resistor_value = first_digit.toString() + second_digit.toString() + "0k";
break;
case 7:
if (second_digit > 0) {
resistor_value = first_digit.toString() + "." + second_digit.toString() + "M";
} else {
resistor_value = first_digit.toString() + "M";
}
break;
case 8:
resistor_value = first_digit.toString() + second_digit.toString() + "M";
break;
case 9:
resistor_value = first_digit.toString() + second_digit.toString() + "0M";
break;
case 10:
resistor_value = first_digit.toString() + "." + second_digit.toString() + "G";
break;
case 11:
resistor_value = first_digit.toString() + second_digit.toString() + "G";
break;
default:
resistor_value = "undefined";
break;
}
} else {
switch (multiplier) {
case 0:
resistor_value = first_digit.toString() + "." + second_digit.toString() + third_digit.toString();
break;
case 1:
resistor_value = first_digit.toString() + second_digit.toString() + "." + third_digit.toString();
break;
case 2:
resistor_value = first_digit.toString() + second_digit.toString() + third_digit.toString();
break;
case 3:
resistor_value = first_digit.toString() + "." + second_digit.toString() + third_digit.toString() +
"k";
break;
case 4:
resistor_value = first_digit.toString() + second_digit.toString() + "." + third_digit.toString() +
"k";
break;
case 5:
resistor_value = first_digit.toString() + second_digit.toString() + +third_digit.toString() +
"k";
break;
case 6:
resistor_value = first_digit.toString() + "." + second_digit.toString() + third_digit.toString() +
"M";
break;
case 7:
resistor_value = first_digit.toString() + second_digit.toString() + "." + third_digit.toString() +
"M";
break;
case 8:
resistor_value = first_digit.toString() + second_digit.toString() + third_digit.toString() +
"M";
break;
case 9:
resistor_value = first_digit.toString() + "." + second_digit.toString() + third_digit.toString() +
"G";
break;
case 10:
resistor_value = first_digit.toString() + second_digit.toString() + "." + third_digit.toString() +
"G";
break;
case 11:
resistor_value = first_digit.toString() + second_digit.toString() + third_digit.toString() +
"G";
break;
default:
resistor_value = "undefined";
break;
}
}
check_if_valid_resistor(resistor_value, tolerance, power);
switch (power) {
case "1/8 watt":
resistor_URL += "rd" + resistor_value + ".html";
break;
case "1/4 watt":
if (tolerance == 6) {
resistor_URL += "ra" + resistor_value + ".html";
} else {
resistor_URL += "rc" + resistor_value + ".html";
}
break;
case "1/2 watt":
resistor_URL += "rb" + resistor_value + ".html";
break;
case "1 watt":
resistor_URL += "rg" + resistor_value + ".html";
break;
default:
break;
}
resistor_value += " Ohms ";
switch (tolerance) {
case 0:
resistor_value += ".05%";
break;
case 1:
resistor_value += ".1%";
break;
case 2:
resistor_value += ".25%";
break;
case 3:
resistor_value += ".5%";
break;
case 4:
resistor_value += "1%";
break;
case 5:
resistor_value += "2%";
break;
case 6:
resistor_value += "5%";
break;
case 7:
resistor_value += "10%";
break;
default:
break;
}
resistor_value += " " + power;
document.getElementById("resistor_value").innerHTML = resistor_value;
}
function check_if_valid_resistor(rv, tolerance, power) {
var standard_values = ["1", "1.1", "1.2", "1.3", "1.5", "1.6", "1.8", "2", "2.2", "2.4", "2.7", "3", "3.3", "3.6", "3.9", "4.3", "4.7", "5.1", "5.6", "6.2", "6.8", "7.5", "8.2", "9.1",
"10", "11", "12", "13", "15", "16", "18", "20", "22", "24", "27", "30", "33", "36", "39", "43", "47", "51", "56", "62", "68", "75", "82", "91",
"100", "110", "120", "130", "150", "160", "180", "200", "220", "240", "270", "300", "330", "360", "390", "430", "470", "510", "560", "620", "680", "750", "820", "910",
"1k", "1.1k", "1.2k", "1.3k", "1.5k", "1.6k", "1.8k", "2k", "2.2k", "2.4k", "2.7k", "3k", "3.3k", "3.6k", "3.9k", "4.3k", "4.7k", "5.1k", "5.6k", "6.2k", "6.8k", "7.5k", "8.2k", "9.1k",
"10k", "11k", "12k", "13k", "15k", "16k", "18k", "20k", "22k", "24k", "27k", "30k", "33k", "36k", "39k", "43k", "47k", "51k", "56k", "62k", "68k", "75k", "82k", "91k",
"100k", "110k", "120k", "130k", "150k", "160k", "180k", "200k", "220k", "240k", "270k", "300k", "330k", "360k", "390k", "430k", "470k", "510k", "560k", "620k", "680k", "750k", "820k", "910k",
"1M", "1.1M", "1.2M", "1.3M", "1.5M", "1.6M", "1.8M", "2M", "2.2M", "2.4M", "2.7M", "3M", "3.3M", "3.6M", "3.9M", "4.3M", "4.7M", "5.1M", "5.6M", "6.2M", "6.8M", "7.5M", "8.2M", "9.1M"];
var found = false;
var lower_val;
var upper_val;
var rv_int = parseInt(rv);
for (i = 0; i < standard_values.length; i++) {
if (rv == standard_values[i]) {
found = true;
break;
}
}
if (!found) {
alert("Warning: Resistor Value may not exist");
}
}
function drawBand(band_num, color) {
var c = document.getElementById("Resistor Canvas");
var ctx = c.getContext("2d");
switch (color) {
case "Black":
ctx.fillStyle = "#000000";
break;
case "Brown":
ctx.fillStyle = "#8b4513";
break;
case "Red":
ctx.fillStyle = "#ff0000";
break;
case "Orange":
ctx.fillStyle = "#ffa500";
break;
case "Yellow":
ctx.fillStyle = "#ffff00";
break;
case "Green":
ctx.fillStyle = "#00ff00";
break;
case "Blue":
ctx.fillStyle = "#0000ff";
break;
case "Violet":
ctx.fillStyle = "#8a2be2";
break;
case "Grey":
ctx.fillStyle = "#d3d3d3";
break;
case "White":
ctx.fillStyle = "#ffffff";
break;
case "Gold":
ctx.fillStyle = "#ffd700";
break;
case "Silver":
ctx.fillStyle = "#c0c0c0";
break;
default:
ctx.fillStyle = "#edc054";
break;
}
var band_x_start = 0;
var band_y_start = band_y_start = 10 * img.height / img_orig_height + img_y_start;;
var band_width = band_width = 15 * img.width / img_orig_width;
var band_height = band_height = 83 * img.height / img_orig_height;;
switch (band_num) {
case 1:
band_x_start = 104 * img.width / img_orig_width + img_x_start;
band_y_start = 1 * img.height / img_orig_height + img_y_start;
band_height = 104 * img.height / img_orig_height;
break;
case "tolerance":
band_x_start = 454 * img.width / img_orig_width + img_x_start;
band_y_start = 1 * img.height / img_orig_height + img_y_start;
band_height = 104 * img.height / img_orig_height;
break;
default:
band_x_start = (104 + ((band_num - 1) * 60)) * img.height / img_orig_height + img_x_start;
break;
}
ctx.fillRect(band_x_start, band_y_start, band_width, band_height);
}
function goto_site() {
location.href = resistor_URL;
}
function show_table() {
document.getElementById("dimension_table").setAttribute("style", "visibility: visible");
}
</script>
<br>
<label>Number of Bands</label>
<select id="number_of_bands" onchange="add_band(this)">
<option value="4" selected>4</option>
<option value="5">5</option>
</select>
<br>
<label>Band #1</label>
<select id="band1" onchange="redraw(this)">
<option value="0" selected>Brown</option>
<option value="1">Red</option>
<option value="2">Orange</option>
<option value="3">Yellow</option>
<option value="4">Green</option>
<option value="5">Blue</option>
<option value="6">Violet</option>
<option value="7">Grey</option>
<option value="8">White</option>
</select>
<label>Band #2</label>
<select id="band2" onchange="redraw(this)">
<option value="0" selected>Black</option>
<option value="1">Brown</option>
<option value="2">Red</option>
<option value="3">Orange</option>
<option value="4">Yellow</option>
<option value="5">Green</option>
<option value="6">Blue</option>
<option value="7">Violet</option>
<option value="8">Grey</option>
<option value="9">White</option>
</select>
<label id="band3_label" style="visibility: hidden">Band #3</label>
<select id="band3" style="visibility: hidden" onchange="redraw(this)">
<option value="0" selected>Black</option>
<option value="1">Brown</option>
<option value="2">Red</option>
<option value="3">Orange</option>
<option value="4">Yellow</option>
<option value="5">Green</option>
<option value="6">Blue</option>
<option value="7">Violet</option>
<option value="8">Grey</option>
<option value="9">White</option>
</select>
<br>
<label>Multiplier</label>
<select id="multiplier" onchange="redraw(this)">
<option value="0">Silver</option>
<option value="1">Gold</option>
<option value="2" selected>Black</option>
<option value="3">Brown</option>
<option value="4">Red</option>
<option value="5">Orange</option>
<option value="6">Yellow</option>
<option value="7">Green</option>
<option value="8">Blue</option>
<option value="9">Violet</option>
<option value="10">Grey</option>
<option value="11">White</option>
</select>
<label>Tolerance</label>
<select id="tolerance" onchange="redraw(this)">
<option value="0">Grey</option>
<option value="1">Violet</option>
<option value="2">Blue</option>
<option value="3">Green</option>
<option value="4">Brown</option>
<option value="5">Red</option>
<option value="6" selected>Gold</option>
<option value="7">Silver</option>
</select>
<label>Power</label>
<select id="power" onchange="calculate_resistor_value()">
<option value="1/8 watt" selected>1/8 watt</option>
<option value="1/4 watt">1/4 watt</option>
<option value="1/2 watt">1/2 watt</option>
<option value="1 watt">1 watt</option>
</select>
<br>
<label>Resistor Value: </label>
<label id="resistor_value">0</label>
<br>
<button id="buy_button" onclick="goto_site()">Check Availability</button>
<br>
<br>
<button id="power_table" onclick="show_table()">Display Resistor Size Power Table</button>
<table id="dimension_table" style="visibility: hidden">
<tr>
<th>Power Rating</th>
<th>Body Length</th>
<th>Body Diameter</th>
</tr>
<tr>
<th>Wattage</th>
<th>size in mm</th>
<th>size in mm</th>
</tr>
<tr>
<td>1/8</td>
<td>3.0</td>
<td>1.8</td>
</tr>
<tr>
<td>1/4</td>
<td>6.5</td>
<td>2.5</td>
</tr>
<tr>
<td>1/2</td>
<td>8.5</td>
<td>3.2</td>
</tr>
<tr>
<td>1</td>
<td>11</td>
<td>5</td>
</tr>
</table>
</body>