use utf8; use 5.022; use strict; use warnings; my $excitation_voltage = 5; # excitation voltage for load cell (volts) my $load_cell_sensitivity = 0.015; # adjusted (down via rheosat) load cell full scale sensitivity (volts/volt) my $pot_taps1 = 128; # number of tap settings allowed by diff amp gain pots my $pot_taps2 = 128; # number of tap settings allowed by non-inverting amp gain pot my $desired_max_output = 4.8; # maximum final output voltage desired at full scale (volts), usually a limitation of the selected op amp or for ADC input headroom my $allowed_output_deviation = 0.01; # how much deviation from $desired_max_output due to the limited number of pot settings is acceptable (e.g. 0.01 would be 1%) my $printf_format = "%8s%15s%12s%15s%15s%14s"; printf "\n$printf_format\n$printf_format\n\n", "Diff Amp", "Non-Inv Amp", "Diff Amp", "Non-Inv Amp", "Total", "Max Output", "Pot Set", "Pot Set", "Gain", "Gain", "Gain", "Voltage"; my $possible = 0; foreach my $gain1 (map { ($pot_taps1-$_)/$_ } (1..$pot_taps1/2)) # for diff amp, only half of pot taps usable since gains less than 1 are not allowed { foreach my $gain2 (map { ($pot_taps2-$_)/$_+1 } (1..$pot_taps2)) # for non-inverting amp { if ( abs($desired_max_output-$gain1*$gain2*$excitation_voltage*$load_cell_sensitivity)/$desired_max_output <= $allowed_output_deviation ) { printf "$printf_format\n", sprintf("%.0f", $pot_taps1/($gain1+1)), sprintf("%.0f", $pot_taps2/$gain2), sprintf("%.3f", $gain1), sprintf("%.3f", $gain2), sprintf("%.3f", $gain1*$gain2), sprintf("%.6f", $excitation_voltage*$load_cell_sensitivity*$gain1*$gain2); $possible = 1; } } } say "\nNo combination of potentiometer settings meets your criteria.\nTry allowing for more \$allowed_output_deviation\nand/or adjusting your \$desired_max_output." if (!$possible);