#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Resistance Color Code

edited February 2013 Posts: 78

To celebrate the Codea 1.5, I created a program to read the resistance color code. Hope that it will be useful for electronic hobbyists.

``````-- Resistance Color Code

function setup()
print("Resistance Color Code")
parameter.integer("Band",4,6,4)
parameter.integer("FirstDigit",0,9,0)
parameter.integer("SecondDigit",0,9,0)
parameter.integer("ThirdDigit",0,9,0)
parameter.integer("Multiplier",-2,7,0)
parameter.integer("Tolerance",-2,7,-2)
parameter.integer("TempCoeffient",1,4,1)
end

-- This function gets called once every frame
function draw()
-- This sets a White background color
background(255,255,255,255)

-- Calculate the Resistance Value
resistval = FirstDigit*10+SecondDigit
if Band>4 then
resistval = resistval*10 + ThirdDigit
end
resistval = resistval*math.pow(10,Multiplier)
if resistval>=1000000 then
r = (resistval/1000000).." M"
elseif resistval>=1000 then
r = (resistval/1000).." K"
else
r = resistval
end
r = r.." ohms"
if Tolerance==-2 then
r = r.." +/- 10%"
elseif Tolerance==-1 then
r = r.." +/- 5%"
elseif Tolerance==1 then
r = r.." +/- 1%"
elseif Tolerance==2 then
r = r.." +/- 2%"
elseif Tolerance==5 then
r = r.." +/- 0.5%"
elseif Tolerance==6 then
r = r.." +/- 0.25%"
elseif Tolerance==7 then
r = r.." +/- 0.1%"
end

-- Display Title and Resistance Value
fontSize(30)
fill(0,0,255, 255)
textMode(CENTER)
text("Resistor Color Code",340,730)
text("R = "..r,340,500)

-- Draw Resistance
posX = 120
posY = 600
resistWidth = 450
registHight = 100

-- Set the width of color tag
tagSpace = 40
tagWidth = 20

stroke(0)
strokeWidth(10)
line(50,650,650,650)
strokeWidth(2)
fill(0,255,255, 255)
rect(posX,posY,resistWidth,registHight)

--Draw First Tag
fill(getColor(FirstDigit))
rect(posX+tagSpace,posY,tagWidth,registHight)

--Draw Second Tag
fill(getColor(SecondDigit))
rect(posX+2*tagSpace,posY,tagWidth,registHight)

--Draw Third Tag
if Band > 4 then
fill(getColor(ThirdDigit))
rect(posX+3*tagSpace,posY,tagWidth,registHight)
end

-- Draw Multiplier Tag
fill(getColor(Multiplier))
if Band==4 then
rect(posX+3*tagSpace,posY,tagWidth,registHight)
else
rect(posX+4*tagSpace,posY,tagWidth,registHight)
end

--Draw Tolerance Tag
fill(getColor(Tolerance))
rect(posX+8*tagSpace,posY,tagWidth,registHight)

--Draw Temperature Coefficient Tag
if Band == 6 then
fill(getColor(TempCoeffient))
rect(posX+9*tagSpace,posY,tagWidth,registHight)
end

--Set the parameter related to specific band
if (Tolerance==0) or (Tolerance==3) or (Tolerance==4) then
Tolerance =Tolerance+1
end
if Band==4 then
ThirdDigit = 0
TempCoeffient = 1
elseif Band == 5 then
TempCoeffient = 1
end
end

function getColor(n)
if n==-2 then
c = color(161, 161, 161, 255)
elseif n==-1 then
c = color(255, 197, 0, 255)
elseif n==0 then
c = color(0, 0, 0, 255)
elseif n==1 then
c = color(101, 31, 31, 255)
elseif n==2 then
c = color(255,0,0, 255)
elseif n==3 then
c = color(249, 97, 17, 255)
elseif n==4 then
c = color(255, 250, 0, 255)
elseif n==5 then
c = color(0, 255, 0, 255)
elseif n==6 then
c = color(0, 0, 250, 255)
elseif n==7 then
c = color(255, 0, 255, 255)
elseif n==8 then
c = color(127, 127, 127, 255)
elseif n==9 then
c = color(255, 250, 255, 255)
end
return c
end
``````
Tagged: