Many "var word"
Robban
Posts: 124
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?
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
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.
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".
' {$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
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.
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
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?