3D part design with OpenSCAD # 155: Automatic resistor generator and calculator version 3.

3D part design with OpenSCAD # 155: Automatic resistor generator and calculator version 3.

In the last post I wrote some OpenSCAD code that would allow you to select the resistor colors based on the value input, while this is handy usually you have a pile of resistors of unknown value that need to be sorted so I made another calculator that allows you to select the colors and will generate the resistor and give it's value.

Full disclosure > I got a brain block and was stumped on how to do this so 90% of this code example was written by me from scratch but 10% was code that I got assistance from by Google AI. I try to keep the posts I make simple enough that anyone can follow the code but sometimes it's interesting to explore the more advanced options available in OpenSCAD and what would have normally taken me days of trial and error , digging through examples online and pounding on the keyboard was finished in literally 15 minutes. Of course the AI didn't crank out perfect code on the first few tries and I spent quite a bit of time getting rid of about 60 lines of code that were completely unnecessary and cleaning it up so it was readable, at least I had a starting point so I could head in the right direction and gave me some ideas for other code I want to write and it gave me a different perspective on how to do some more advanced things with OpenSCAD.

Here is the updated code:

//
/*[Resize for Kicad]*/
Resize=5.30;//[2.65:2.65:26.5]
Show_value_text=true;

/*[Color Band Select]*/
band_1="Black";//[Black,Brown,Red,Orange,Yellow, Green,Blue,Violet,Grey,White,Gold,Silver] 

band_2 = "Orange";//[Black,Brown,Red,Orange, Yellow, Green,Blue,Violet,Grey,White,Gold,Silver]
  
band_3 = "Red";//[Black,Brown,Red,Orange,Yellow, Green,Blue,Violet,Grey,White,Gold,Silver]
   
band_4 = "Gold";//[Silver] 

/*[leads]*/
lead_length=4;//[2:.01:6]
lead_diameter=.40;//[.1:.01:.6]

/*[Hidden]*/
$fn=$preview?60:150;;

color_names=["Black","Brown","Red","Orange", "Yellow","Green","Blue","Violet","Grey","White","Gold","Silver"];

color_rgb=[[0.2,0.2,0.2],[0.4,0.25,0.13],[0.9,0.1,0.1],[1,0.5,0],[0.9,0.9,0],[0.1,0.7,0.1],[0.1,0.1,0.9],[0.6,0.1,0.7],[0.5,0.5,0.5],[0.95,0.95,0.95],[0.8,0.6,0.2],[0.75,0.75,0.75]];

function get_index(name)=search([name],color_names)[0];

function get_color(name)=color_rgb[get_index(name)];

function get_multiplier(index)= 
    (index==10)?0.1: 
    (index==11)?0.01: 
    pow(10,index);

d1=get_index(band_1);
d2=get_index(band_2);
mult=get_multiplier(get_index(band_3));
ohms=(d1*10+d2)*mult;

label_text= 
    (ohms>=1000000)?str(ohms/1000000,"M Ohm") :
    (ohms>=1000)?str(ohms/1000,"k Ohm") : 
    str(ohms," Ohm");
    
module Text(){
if(Show_value_text)
translate([0,1+(Resize/10),0]){
  color("grey"){
  linear_extrude(.1)
  text(label_text, size=1,halign="center",valign="center",spacing=1.1);
}}}

module band_ring(z_pos){
  translate([0,0,z_pos]) {
  cylinder(h=1,r=1.03,center=false);
}}

module resistor_3d(b1,b2,b3,b4){
 color(get_color(b1))band_ring(z_pos=-2.3);
 color(get_color(b2))band_ring(z_pos=-1.0);
 color(get_color(b3))band_ring(z_pos=0.3);
 color(get_color(b4))band_ring(z_pos=2.2);
}

module main_body(){
 color("khaki"){
 minkowski(){
 translate([1.9,0,0])
 rotate([0,90,0])
 cylinder(5,.01,.01);
 sphere(1);
}
 translate([1.6,0,0])
 sphere(1.05);
 translate([7.5,0,0])
 sphere(1.05);
}}

module leads(){
rotate([90,90,90])
color("lightgrey"){
  translate([0,0,2]){
  rotate([90,180,0])
  translate([-2,5,0])
  rotate_extrude(90)
  translate([2,0,0])
  circle(r=lead_diameter);
  rotate([90,180,90])
  translate([0,7,2])
  cylinder(lead_length,lead_diameter,lead_diameter);
}
  translate([0,0,-1]){   
  rotate([270,180,0])
  translate([-2,5,0])
  rotate_extrude(90)
  translate([2,0,0])
  circle(r =lead_diameter);
  rotate([90,180,90])
  translate([0,-7,2])  
  cylinder(lead_length,lead_diameter,lead_diameter);
}}}

resize([Resize,0,0],auto=true){
translate([-.5,0,0]){
rotate([0,90,0])
resistor_3d(band_1,band_2,band_3,band_4);
color("khaki")
translate([-4,0,0])
main_body();
leads();
}}

Text();

And now I can select the color bands and it will automatically generate a resistor and give me the value: