Shop OBEX P1 Docs P2 Docs Learn Events
Optimization — Parallax Forums

Optimization

ArchiverArchiver Posts: 46,084
edited 2001-04-06 04:47 in General Discussion
Ok guys, this is my first stamp project and I was wondering if there would
be any way to optimize this code. It takes in data from sensors and sends
out midi. I'd appreciate any help.

sensorA var word
sensorB var word
sensorC var word
sensorD var word
sensorE var word
sensorF var word
sensorG var word
sensorH var word
channel var byte
x var byte

MAINLOOP:

Sample:

high 9
pause 1
RCTIME 9,1,x

if x = 0 then channel1
if x = 32 then channel2
if x = 64 then channel3
if x = 128 then channel4

Sensor1:

high 1
pause 1
RCTIME 1,1,sensorA

debug ? sensorA

if sensorA > 100 then midiout1

Sensor2:

high 2
pause 1
RCTIME 2,1,sensorB

if sensorB > 100 then midiout2

Sensor3:

high 3
pause 1
RCTIME 3,1,sensorC

if sensorC > 100 then midiout3

Sensor4:

high 4
pause 1
RCTIME 4,1,sensorD

if sensorD > 100 then midiout4

Sensor5:

high 5
pause 1
RCTIME 5,1,sensorE

if sensorE > 100 then midiout5

Sensor6:

high 6
pause 1
RCTIME 6,1,sensorF

if sensorF > 100 then midiout6

Sensor7:

high 7
pause 1
RCTIME 7,1,sensorG

if sensorG > 100 then midiout7

Sensor8:

high 8
pause 1
RCTIME 8,1,sensorH

if sensorH > 100 then midiout8

goto MAINLOOP



channel1:

channel = 0

goto Sensor1

channel2:

channel = 1

goto Sensor1

channel3:

channel = 2

goto Sensor1

channel4:

channel = 3

goto Sensor1



midiout1:

serout 15,$8000+12,0,[noparse][[/noparse]144+channel,60,(sensorA*127)/5000]

goto Sensor2

midiout2:

serout 15,$8000+12,0,[noparse][[/noparse]144+channel,62,(sensorB*127)/5000]

goto Sensor3

midiout3:

serout 15,$8000+12,0,[noparse][[/noparse]144+channel,63,(sensorC*127)/5000]

goto Sensor4

midiout4:

serout 15,$8000+12,0,[noparse][[/noparse]144+channel,65,(sensorD*127)/5000]

goto Sensor5

midiout5:

serout 15,$8000+12,0,[noparse][[/noparse]144+channel,67,(sensorE*127)/5000]

goto Sensor6

midiout6:

serout 15,$8000+12,0,[noparse][[/noparse]144+channel,68,(sensorF*127)/5000]

goto Sensor7

midiout7:

serout 15,$8000+12,0,[noparse][[/noparse]144+channel,70,(sensorG*127)/5000]

goto Sensor8

midiout8:

serout 15,$8000+12,0,[noparse][[/noparse]144+channel,72,(sensorH*127)/5000]

goto MAINLOOP

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2001-04-04 18:21
    Hi Payam,

    I'll make some suggestions in line with your code...

    >Ok guys, this is my first stamp project and I was wondering if there would
    >be any way to optimize this code. It takes in data from sensors and sends
    >out midi. I'd appreciate any help.
    >
    >sensorA var word
    >sensorB var word
    >sensorC var word
    >sensorD var word
    >sensorE var word
    >sensorF var word
    >sensorG var word
    >sensorH var word
    >channel var byte
    >x var byte
    >
    >MAINLOOP:
    >
    >Sample:
    >
    >high 9
    >pause 1
    >RCTIME 9,1,x
    >
    >if x = 0 then channel1
    >if x = 32 then channel2
    >if x = 64 then channel3
    >if x = 128 then channel4

    I am guessing you want inequalities there, because RCtime is very
    unlikely to return exactly 32, 64 or 128, or 0 (1 is the minimum, 0
    only occurs if there is a overrun).

    These gotos just substitute a value [noparse][[/noparse]channel=0,1,2,3] and come back
    to the label sensor1 just below. Instead, you might be able to use
    simply

    channel=ncd (x/32)

    This returns
    channel:=0 for 0=x<=31
    channel:=1 for 32=x<=63
    channel:=2 for 64=<=127
    channel:=3 for 128=x<=255
    Is a byte variable for x always going to work? If RCtime gets up to
    x=256, it is going to rollover back to zero.

    >
    >Sensor1:
    >high 1
    >pause 1
    >RCTIME 1,1,sensorA
    >debug ? sensorA
    >if sensorA > 100 then midiout1
    >
    >Sensor2:
    >high 2
    >pause 1
    >RCTIME 2,1,sensorB
    >if sensorB > 100 then midiout2

    ...snip etc. for 8 sensors.

    This is fine, but if you want to be tricky you could combine them all
    into one for-next loop. There do not have to be separate variables
    for each sensor.

    SensorX var word
    i var nib
    Sensors:
    for i=1 to 8
    high i
    pause 1
    RCtime i,1,sensorX
    if sensorX<=100 then nextsensor ' skip sending data
    serout 15,$8000+12,0,[noparse][[/noparse]144+channel,60,(sensorX*127)/5000]
    nextSensor:
    next


    >goto MAINLOOP
    >
    >

    the following were your channel assignments, maybe not needed, see above

    >channel1:
    >channel = 0
    >goto Sensor1
    >
    >channel2:
    >channel = 1
    >goto Sensor1
    >
    >channel3:
    >channel = 2
    >goto Sensor1
    >
    >channel4:
    >channel = 3
    >goto Sensor1
    >
    >

    The following were the serouts, maybe can be part of a short for-next
    loop, see above.

    >midiout1:
    >serout 15,$8000+12,0,[noparse][[/noparse]144+channel,60,(sensorA*127)/5000]
    >goto Sensor2
    >
    >midiout2:
    >serout 15,$8000+12,0,[noparse][[/noparse]144+channel,62,(sensorB*127)/5000]
    >goto Sensor3
    >
    >... snip..and so on for 8 sensors.


    I hope that is instructive. I'd say you did really well for a first
    Stamp project!!

    -- regards,
    Tracy Allen
    electronically monitored ecosystems
    http://www.emesystems.com
  • ArchiverArchiver Posts: 46,084
    edited 2001-04-06 04:47
    As a neophyte myself, I must say that watching Tracy Allen Optimize the code
    in this thread is fascinating and might be one of the most useful learning
    tools for me. I appreciate the time and effort that a lot of you pro's
    dedicate to answering our questions, I know it must get mundane at times.
    Three cheers for our altruistic friends.

    Thanks

    Jeff
Sign In or Register to comment.