Shop OBEX P1 Docs P2 Docs Learn Events
Many "var word" — Parallax Forums

Many "var word"

RobbanRobban Posts: 124
edited 2007-03-19 01:13 in BASIC Stamp
Hi!

I have a program where i use many varibles...like

a,b,c,d,e,f,g etc
and everyone i want to be a word
so i wrote

a var word
b var word
c var word
d var word
..
..
..
..
j var word

but i get "out of variable space...

why? what can i do instead?

Comments

  • Larry~Larry~ Posts: 242
    edited 2007-03-17 15:59
    Well you did not say what a-z is used for or any other info like what the program does but here is a way to get more word varables.

    use only the varable a!

    as long as you do something with the results right after reading this varable, like print to lcd you can use the same varable to do the next step then debug or print to lcd. now if you need all the varables to be saved use a eprom and store them one at a time.

    not knowing what your doing makes it hard to give an exact answer.
  • ForrestForrest Posts: 1,341
    edited 2007-03-17 16:01
    The Basic Stamp 2 has 26 bytes reserverd for variables, or 13 words.
  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-17 16:03
    There are only 13 word variables available. You will have to change your program to not use so many unique variables.

    Sometimes we don't need to use all the variables at the same time, but having different names makes a program easier to understand. In that case, you can use an alias which is just a way of giving the same memory location two different names (look up alias in the PBasic Manual).

    Sometimes we don't really need a word. Sometimes a byte or a 4-bit nibble or only a single bit will do. In that case, we can allocate only the space that's needed so that everything fits.

    In some Stamps (everything but the BS1 and BS2), there is what's called the Scratchpad RAM. You can use the GET and PUT statements to access this one byte at a time to store some data (up to 64 or 128 bytes depending on the Stamp model).

    If the data doesn't change often, you can use leftover space in the program EEPROM. The problem with this is that writing is slow and you can write to EEPROM only a limited number of times (about 100,000 times) before a given location "wears out".
  • RobbanRobban Posts: 124
    edited 2007-03-18 10:44
    tried to make it work by ignoring some "var word" but no luck so here is the original program...if anyone has any suggestions

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    HEXSTR VAR Byte(15)
    I VAR Byte
    Result VAR Word
    SPEED VAR Word
    RPM VAR Word
    GAS VAR Word
    COOLTEMP VAR Word
    INTAKE_AIR_TEMP VAR Word
    AIR_FLOW VAR Word
    IGNITION_TIMING VAR Word
    VOLTAGE VAR Word
    disp VAR Word
    light VAR Word


    ' send echo off to ECU
    echo_off:
    SEROUT 3,84,[noparse][[/noparse]"ate0",13]


    Main:

    ' Read the outside light intensity
    ljus:
    HIGH 1
    PAUSE 100
    RCTIME 1,1,light
    IF light>0 AND light<10000 THEN disp=20
    IF light>12000 AND light<20000 THEN disp=30
    IF light>22000 AND light<30000 THEN disp=40
    IF light>32000 AND light<40000 THEN disp=50
    IF light>42000 AND light<50000 THEN disp=60
    IF light>52000 AND light<60000 THEN disp=70
    IF light>60000 THEN disp=80

    ' Dim the H.U.D
    SEROUT 2,84,[noparse][[/noparse]"?b",DEC disp]




    ' check the switch position
    switch:
    HIGH 0
    PAUSE 1
    RCTIME 0,1,Result
    IF result>0 AND result<500 THEN dispoff
    IF result>750 AND result<1500 THEN hast
    IF result>2000 AND result<3500 THEN varv
    IF result>4000 AND result<4700 THEN gasa
    IF result>5000 AND result<6700 THEN kyltemp
    IF result>7000 AND result<8000 THEN inkom_luft_temp
    IF result>8500 AND result<9700 THEN luftflode
    IF result>10000 AND result<11550 THEN tandning
    IF result>12000 AND result<14100 THEN belast
    IF result>15000 AND result<16830 THEN Volt
    IF result>19000 AND result<22000 THEN hast_varv
    IF result>22500 THEN varv_gas

    PAUSE 100
    GOTO Main

    ' Turn off H.U.D
    dispoff:
    SEROUT 2,84,[noparse][[/noparse]"?f","?m"]
    PAUSE 50
    GOTO main

    ' Display Speed
    hast:
    SEROUT 2,84,[noparse][[/noparse]"?y0","SPEED","?m"]
    SEROUT 3,16468,[noparse][[/noparse]"010d",13]
    SERIN 4,16468,[noparse][[/noparse]STR hexstr\10]
    GOSUB hexkonv
    SPEED =(hexstr(6) << 4) + hexstr(7)
    SEROUT 2,84,[noparse][[/noparse]"?y1","Speed: ",DEC SPEED," Km/h","?m" ]
    PAUSE 1000
    GOTO main


    ' Display RPM
    varv:
    SEROUT 2,84,[noparse][[/noparse]"?y0","RPM" ,"?m"]
    SEROUT 3,16468,[noparse][[/noparse]"010c",13]
    SERIN 4,16468,[noparse][[/noparse]STR hexstr\15]
    FOR i=6 TO 13
    GOSUB hexkonv2
    RPM =(hexstr(6) << 12) + (hexstr(7) << 8) + (hexstr(9) << 4) + hexstr(10)
    SEROUT 2,84,[noparse][[/noparse]"?Y1?",DEC RPM/4,"?n"]
    PAUSE 1000
    GOTO main


    ' Display Throotle position
    gasa:
    SEROUT 2,84,[noparse][[/noparse]"?y0","GAS POSITION","?m"]
    SEROUT 3,16468,[noparse][[/noparse]"0111",13]
    SERIN 4,16468,[noparse][[/noparse]STR hexstr\10]
    GOSUB hexkonv
    GAS =(hexstr(6) << 4) + hexstr(7)
    SEROUT 2,84,[noparse][[/noparse]"?y1",DEC GAS*100/255," %","?m" ]
    PAUSE 1000
    GOTO main


    ' Display Cooltemp
    kyltemp:
    SEROUT 2,84,[noparse][[/noparse]"?y0","COOLTEMP","?m"]
    SEROUT 3,16468,[noparse][[/noparse]"0105",13]
    SERIN 4,16468,[noparse][[/noparse]STR hexstr\10]
    GOSUB hexkonv
    COOLTEMP =(hexstr(6) << 4) + hexstr(7)
    SEROUT 2,84,[noparse][[/noparse]"?y1",DEC COOLTEMP-40," Gr.C","?m" ]
    PAUSE 1000
    GOTO main


    ' Display Intake air temp
    inkom_luft_temp:
    SEROUT 2,84,[noparse][[/noparse]"?y0","Intake-air-temp","?m"]
    SEROUT 3,16468,[noparse][[/noparse]"010f",13]
    SERIN 4,16468,[noparse][[/noparse]STR hexstr\10]
    GOSUB hexkonv
    calc_load =(hexstr(6) << 4) + hexstr(7)
    SEROUT 2,84,[noparse][[/noparse]"?y1",DEC intake_air_temp-40," %","?m" ]
    PAUSE 1000
    GOTO main


    'Display Airflow
    luft_flode:
    SEROUT 2,84,[noparse][[/noparse]"?y0","AIRFLOW","?m"]
    SEROUT 3,16468,[noparse][[/noparse]"0110",13]
    SERIN 4,16468,[noparse][[/noparse]STR hexstr\15]
    GOSUB hexkonv2
    AIRFLOW =(hexstr(6) << 12) + (hexstr(7) << 8) + (hexstr(9) << 4) + hexstr(10)
    SEROUT 2,84,[noparse][[/noparse]"?y1",DEC airflow/100," gm/sec","?n "]
    PAUSE 1000
    GOTO main


    'Display Ignition timing
    tandning:
    SEROUT 2,84,[noparse][[/noparse]"?y0","IGNITION TIMING","?m"]
    SEROUT 3,16468,[noparse][[/noparse]"010e",13]
    SERIN 4,16468,[noparse][[/noparse]STR hexstr\10]
    GOSUB hexkonv
    ignition_timing =(hexstr(6) << 4) + hexstr(7)
    SEROUT 2,84,[noparse][[/noparse]"?y1",DEC ignition_timing/2-64,"Deg","?m" ]
    PAUSE 1000
    GOTO main


    'Display calculated load
    belast:
    SEROUT 2,84,[noparse][[/noparse]"?y0","CALC.LOAD ","?m"]
    SEROUT 3,16468,[noparse][[/noparse]"0104",13]
    SERIN 4,16468,[noparse][[/noparse]STR hexstr\10]
    GOSUB hexkonv
    calc_load =(hexstr(6) << 4) + hexstr(7)
    SEROUT 2,84,[noparse][[/noparse]"?y1",DEC calc_load/255*100," %","?m" ]
    PAUSE 1000
    GOTO main

    'Display ECU voltage
    volt:
    SEROUT 2,84,[noparse][[/noparse]"?y0","ECU VOLTAGE","?m"]
    PAUSE 50
    GOTO main


    'Display Speed AND Rpm
    hast_varv:
    SEROUT 2,84,[noparse][[/noparse]"?y0","SPEED & RPM","?m"]
    SEROUT 3,16468,[noparse][[/noparse]"010d",13]
    SERIN 4,16468,[noparse][[/noparse]STR hexstr\10]
    GOSUB hexkonv
    SPEED =(hexstr(6) << 4) + hexstr(7)
    SEROUT 2,84,[noparse][[/noparse]"?y0?x13",DEC SPEED," Km/h","?m" ]
    SEROUT 3,16468,[noparse][[/noparse]"010c",13]
    SERIN 4,16468,[noparse][[/noparse]STR hexstr\15]
    FOR i=6 TO 13
    GOSUB hexkonv2
    RPM =(hexstr(6) << 12) + (hexstr(7) << 8) + (hexstr(9) << 4) + hexstr(10)
    SEROUT 2,84,[noparse][[/noparse]"?Y1?x5",DEC RPM/4,"?m"]
    PAUSE 1000
    GOTO main

    ' Display RPM and throttle position
    varv_gas:
    SEROUT 2,84,[noparse][[/noparse]"?y0","RPM & GAS","?m"]
    PAUSE 50
    GOTO main


    ' Convert 1 byte Hex
    hexkonv:
    FOR i= 6 TO 7
    IF hexstr(i) < "0" OR hexstr(i) > "9" THEN skip1x
    hexstr(i) = hexstr(i) - "0"
    skip1x:
    IF hexstr(i) < "a" OR hexstr(i) > "f" THEN skip2x
    hexstr(i) = hexstr(i) - "a" + 10
    skip2x:
    IF hexstr(i)<"A" OR hexstr(i)>"F" THEN skip3x
    hexstr(i)=hexstr(i)-"A" +10
    SKIP3x:
    NEXT
    RETURN

    ' Convert 2 byte hex
    hexkonv2:
    FOR i= 6 TO 7
    IF hexstr(i) < "0" OR hexstr(i) > "9" THEN skip4x
    hexstr(i) = hexstr(i) - "0"
    skip4x:
    IF hexstr(i) < "a" OR hexstr(i) > "f" THEN skip5x
    hexstr(i) = hexstr(i) - "a" + 10
    skip5x:
    IF hexstr(i)<"A" OR hexstr(i)>"F" THEN skip6x
    hexstr(i)=hexstr(i)-"A" +10
    SKIP6x:
    NEXT

    FOR i = 9 TO 10
    IF hexstr(i) < "0" OR hexstr(i) > "9" THEN skip7x
    hexstr(i) = hexstr(i) - "0"
    skip7x:
    IF hexstr(i) < "a" OR hexstr(i) > "f" THEN skip8x
    hexstr(i) = hexstr(i) - "a" + 10
    skip8x:
    IF hexstr(i)<"A" OR hexstr(i)>"F" THEN skip9x
    hexstr(i)=hexstr(i)-"A" +10
    SKIP9x:
    NEXT
    RETURN
  • Steve JoblinSteve Joblin Posts: 784
    edited 2007-03-18 15:35
    Do each of these really need to be "words"?· You can only have 13 "words", but each word can be broken down into two·"Bytes", so you can store 26 varialbles if they are all defined as bytes.· You can break down each byte into two·"Nibs", so you can store 56 varialbes if they are all defined as nibs.· You can break down each Nib into 4 "bits", so you can store 112 variables if they are all defined as bits.

    bit = value can be 0 or 1
    nib = value in range 0 to 15
    byte = value in range 0 to 255
    word = value in range 0 to 65535

    You should define your variables to the smallest size that will hold the largest value that might ever be stored in it.· For example, if "Air_Flow" is a yes or no type variable, define it as a bit.· If "Voltage" is never going to be above 12 volts, define it as a nib.
  • Martin HebelMartin Hebel Posts: 1,239
    edited 2007-03-18 15:50
    It appears from your code you decide what value you need, then calculate it, then send it, and not really ever use that variable for anything else.

    For example, AIRFLOW appears only once in the program. Why waste a variable for it alone when that data never has to be accessed again.

    For many, I see no reason why the same variable cannot be used. Something generic, such as VALUE, each time you must perfrom a calculation that is not referenced anywhere else.

    -Martin

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    StampPlot - GUI and Plotting, and XBee Wireless Adapters
    Southern Illinois University Carbondale, Electronic Systems Technologies
  • RobbanRobban Posts: 124
    edited 2007-03-18 15:55
    ok. i changed to

    result var word
    speed var byte
    rpm var word
    gas var byte
    cooltemp var byte
    intake_air_temp var byte
    air_flow var word
    ignition timing var byte
    voltage var nib
    disp var word
    lgiht var word


    but i get the ignition timing var byte marked and messege "Out of variable space"...

    why`?

    do i have to many Var?
  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-18 16:07
    Please look in the index of the PBasic manual under "aliases". There's a whole discussion there with examples of how to reuse variables.
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-03-19 01:13
    The big problem you have is that 10 bytes of "HEXSTR". Do you really NEED 10 bytes for that? Can't you recieve the string using the "HEX" modifier to convert it immediately into a word value?
Sign In or Register to comment.