'===========================================================================
' Subject: GEARWHEEL RATIO CALCULATOR         Date: 02-02-98 (18:42)       
'  Author: Bert vam Dam                       Code: QB, QBasic, PDS        
'  Origin: bd1rsd@usa.net                   Packet: ALGOR.ABC
'===========================================================================
'GEAR.BAS Gearwheel ratio calculator for LEGO users
'Bert van Dam (29/11/94)

'This program will help you determine which gearwheels to use when you're
'building a LEGO model and need a certain acceleration/deceleration
'transmission. Start the program and enter an acceleration (for example
'125. The program will now calculate the best possible combination to get
'as close to the target value as possible. As an alternative a second
'acceleration might be suggested with fewer gearwheels. This is very
'helpfull in case the desired transmission takes 15 gearwheels while a
'close alternative may require only two.

'This program is freeware and may be use in whatever way you see fit. If
'you have any questions you can contact me in the QUIK_BAS Fido conference,
'at Fido 2:285/750.16 of the inet BD1RSD@USA.NET


AantalRatios = 13
DIM Ratio(AantalRatios), Beste(AantalRatios), Aantal(AantalRatios), Naam$(AantalRatios)
LaatsteFout = 100000000
LaatsteTotaalAantal = LaatsteFout


'Available ratio's

FOR t = 1 TO AantalRatios
        READ Ratio(t), Naam$(t)
NEXT t
DATA 1.55, from 2.2 cm string-wheel to 3.4 cm string-wheel
DATA 1.67, from gear-wheel with 24 teeth to gear-wheel with 40 teeth
DATA 1.71, from gear-wheel with 14 teeth to gear-wheel with 24 teeth
DATA 1.75, from gear-wheel with 8 teeth to gear-wheel with 14 teeth
DATA 2.86, from gear-wheel with 14 teeth to gear-wheel with 40 teeth
DATA 3, from gear-wheel with 8 teeth to gear-wheel with 24 teeth
DATA 3.67, from 0.6 cm string-wheel to 2.2 cm string-wheel
DATA 5, from gear-wheel with 8 teeth to gear-wheel with 40 teeth
DATA 5.67, from 0.6 cm string-wheel to 3.4 cm string-wheel
DATA 8, from crown-wheel to gear-wheel with 8 teeth
DATA 14, from crown-wheel to gear-wheel with 14 teeth
DATA 24, from crown-wheel to gear-wheel with 24 teeth
DATA 40, from crown-wheel to gear-wheel with 40 teeth

'Setup screen

CLS
PRINT
PRINT "                Ratio calculation for Lego Technic"
PRINT "                ---------------------------------- "
PRINT
PRINT " This program will calculate based on the desired acceleration/deceleration"
PRINT " ratio the gear-wheel and/or string-wheel compositions you need to use from"
PRINT " your LEGO box. Please bear in mind that for the protection of your motor you"
PRINT " need at least one string-wheel combo, for example a 1:1 transmission."
PRINT
INPUT " What is the desired accelleration "; Target
Target = ABS(Target)
PRINT
PRINT TAB(5); "Ratio"; TAB(12); "Wheels"; TAB(20); "Description"
PRINT

'Ratio calculation

StartRatio = AantalRatios + 1

DO
StartRatio = StartRatio - 1
FOR t = 1 TO AantalRatios
        Aantal(t) = 0
NEXT t
TotaalAantal = 0
Result = 1
WerkTarget = Target
FOR t = StartRatio TO 1 STEP -1
        Aantal = WerkTarget / Ratio(t)
        IF Aantal > .95 THEN
                Aantal(t) = Aantal(t) + 1
                TotaalAantal = TotaalAantal + 1
                WerkTarget = WerkTarget / Ratio(t)
                Result = Result * Ratio(t)
                t = t + 1
        END IF
NEXT t
IF ABS(Target - Result) < LaatsteFout THEN
        FOR t = t TO AantalRatios
                Beste(t) = Aantal(t)
        NEXT t
        LaatsteFout = ABS(Target - Result)
        BesteResult = Result
        Goed = 1
END IF
IF TotaalAantal < LaatsteTotaalAantal AND TotaalAantal > 0 THEN
        MinTand = Result
        TotAant = TotaalAantal
        LaatsteTotaalAantal = TotaalAantal
END IF
IF TotaalAantal = LaatsteTotaalAantal AND Goed = 1 THEN
        MinTand = Result
        TotAant = TotaalAantal
        LaatsteTotaalAantal = TotaalAantal
END IF
Goed = 0
LOOP UNTIL ABS(Target - Result) < 1 OR StartRatio = 1

IF ABS(Target - Result) > LaatsteFout THEN
        FOR t = 1 TO AantalRatios
                Aantal(t) = Beste(t)
        NEXT t
        Result = BesteResult
END IF
FOR t = 1 TO AantalRatios
        IF Aantal(t) <> 0 THEN PRINT TAB(5); Ratio(t); TAB(13); Aantal(t); TAB(20); Naam$(t)
        TotaalOplossing = TotaalOplossing + Aantal(t)
NEXT t
PRINT
PRINT " Target ratio    "; Target
PRINT " Calculated ratio "; Result
PRINT " Error in ratio   "; ABS(Target - Result)
PRINT
IF TotAant < TotaalOplossing THEN PRINT " Alternative ratio: "; MinTand; " with "; TotAant; "teeth/string-wheels."

END
