HomeArticlesProjectsBlogContact
Articles
Resistance Calculator
Colin Mitchell
Colin Mitchell
December 21, 2004
Make sure to subscribe to our newsletter and be the first to know the news.
<!-- TODO FIXME: resistance calculator -->
   ohmStr = String.fromCharCode(937)
base10 = Math.log(10)

function preLoad () {
  // preload color-band images
  arrCols = new Array()
  srcBase = document.getElementById('sel1')
  for (i = 0; i < srcBase.length; i++) {
    arrCols[i] = srcBase.options[i].value
  }
  arrImg = new Array()
  for (i = 0; i < arrCols.length; i++) {
    for (j = 0; j < 3; j++) {
      arrImg[i * 3 + j]
        = new Image()
      arrImg[i * 3 + j].src = 'images/band_' + arrCols[i]
        + (j + 1).toString() + '.jpg'
    }
  }
  pre1 = new Image()
  pre1.src = 'images/band_d3.jpg'
  pre2 = new Image()
  pre2.src = 'images/band_s3.jpg'
  pre3 = new Image()
  pre3.src = 'images/band_mult_s.jpg'
}

function selCol (what, band) {
  // display color-band image
  eval(`document.getElementById('band' + band + '').src = 'images / band_' + what.value + band + '.jpg';`)
}

function selTol () {
  // display tolerance band, set tolerance numeric dropdown
  document.getElementById('mult').src = 'images/band_mult_' + document.getElementById('tolerance').value + '.jpg'
  document.getElementById('tolNum').selectedIndex = document.getElementById('tolerance').selectedIndex
  calc()
}

function tolRev () {
  // display tolerance band, set tolerance color dropdown
  document.getElementById('tolerance').selectedIndex = document.getElementById('tolNum').selectedIndex
  document.getElementById('mult').src = 'images/band_mult_' + document.getElementById('tolerance').value + '.jpg'
  calc()
}

function calc () {
  // calculate values from color selections
  pow = document.getElementById('sel3').selectedIndex
  if (pow == 10) pow = -1 //gold
  if (pow == 11) pow = -2 //silver
// note: Multiplied resistance value by 100 (pow+2), rounded to nearest integer, then divided
// result by 100 to compensate for Javascript exponentiation errors (only need 2 significant digits)
  res = Math.round((document.getElementById('sel1').selectedIndex *
    10 + document.getElementById('sel2').selectedIndex) * Math.pow(10, pow + 2)) / 100
  if (res > 990000) {
    document.getElementById('multSel').selectedIndex = 2
    res /= 1000000
  } else if (res > 990) {
    document.getElementById('multSel').selectedIndex = 1
    res /= 1000
  } else { document.getElementById('multSel').selectedIndex = 0 }
  if (res > 999) {
    res = res.toString()
    res = res.substr(0, res.length - 3) + ',' + res.substr(res.length - 3, 3)
  }
  tolOut = 5 *
    Math.pow(2, document.getElementById('tolerance').selectedIndex)
  document.getElementById('txtNum').value = res
}

function calcRev () {
  // calculates color selections from numeric input
// convert input to ohms
  document.getElementById('errSpan').innerHTML = ''
  base = document.getElementById('txtNum').value
  if (parseFloat(base).toString() == 'NaN') return false
  if (base == 0) return false
  multBy = Math.pow(1000, document.getElementById('multSel').selectedIndex)
  res = base * multBy
  if ((res > 99000000000) || (res < .1)) {
    inputError()
    return false
  }
  raisedTo = Math.floor(Math.log(res) / base10)
  res = Math.round(res / Math.pow(10, raisedTo - 1)) / 10
  d1 = Math.floor(res)
  d2 = Math.round((res - d1) * 10)

  if (d1 == 10) {
    d1 = 1
    d2 = 0
    raisedTo += 1
  }
  mIndex = raisedTo - 1
  if (mIndex == -1) mIndex = 10
  if (mIndex == -2) mIndex = 11
  document.getElementById('sel1').selectedIndex = d1
  selCol(document.getElementById('sel1'), 1)
  document.getElementById('sel2').selectedIndex = d2
  selCol(document.getElementById('sel2'), 2)
  document.getElementById('sel3').selectedIndex = mIndex
  selCol(document.getElementById('sel3'), 3)
}

function inputError () {
  for (i = 1; i < 4; i++) {
    eval(`document.getElementById('sel' + i + '').selectedIndex = 0;`)
    eval(`document.getElementById('band' + i + '').src = 'images / band_k' + i + '.jpg';`)
  }
  document.getElementById('errSpan').innerHTML = 'out of range'
}

Resistance Calculator

black brown red orange yellow green blue violet gray white black brown red orange yellow green blue violet gray white black brown red orange yellow green blue violet gray white gold silver gold silver brown red

lead l

band k1

fill 1

band k2

fill 2

band k3

fill 3

band mult g

lead r

 **document.write(ohmStr) kdocument.write(ohmStr) Mdocument.write(ohmStr)�5% �10% �1% �2%**

Wait for colours to download


Colin Mitchell

Colin Mitchell

Expertise

electronics
writing
PIC-Chips

Social Media

instagramtwitterwebsite

Related Posts

TODO
Transistor Test
© 2021, All Rights Reserved.

Quick Links

Advertise with usAbout UsContact Us

Social Media