PDA

View Full Version : Variable Variables



CocaColaKid
- 29th August 2005, 15:39
Is there a way I can create a variable that increments itself. For example:

gosub read_temp
temp_0 = temp_c
gosub read_temp
temp_1 = temp_c
gosub read_temp
temp_2 = temp_c

It would be nice to have something like

for x = 0 to 4
gosub read_temp
temp_[x] = temp_c
next x

Is this possible?

Melanie
- 29th August 2005, 15:55
It's called an array (PBP manual section 4.5).

CocaColaKid
- 29th August 2005, 16:00
This is my issue:



for a = 0 to 4
gosub read_temp
temp_0[a] = temp_c
temp_0[a+1] = temp_f
temp_0[a+2] = temp_k
next a


I have to read 5 sensors, temp_0 - temp_4. Each of these sensors will have 3 temperature units associated with it. This code loads the array for the 1st sensor but it won't do much for the other four unless I can find a way to increment the temp_0 to temp_1 etc..

Melanie
- 29th August 2005, 17:39
You read 5 Sensors with three readings per sensor, so either have three arrays five elements deep...

for a = 0 to 4
gosub read_temp
temp_A[a] = temp_c
temp_B[a] = temp_f
temp_C[a] = temp_k
next a

or one array 15 elements deep...

for a = 0 to 4
gosub read_temp
temp_0[a] = temp_c
temp_0[a+5] = temp_f
temp_0[a+10] = temp_k
next a

or if you want the readings contiguous next to each other (again with a 15 element array), add another variable like so...

for a = 0 to 4
b=a*3
gosub read_temp
temp_0[b] = temp_c
temp_0[b+1] = temp_f
temp_0[b+2] = temp_k
next a

There's three examples... need more variations?

CocaColaKid
- 29th August 2005, 19:23
Melanie,

Thank you very much. I was having a real seniors moment for a while there. My code now works beautifully.



main:
for a = 0 to 4
gosub read_temp
sign_c_array[a] = sign_c
sign_f_array[a] = sign_f
temp_c_array[a] = temp_c
temp_f_array[a] = temp_f
temp_k_array[a] = temp_k
next a
gosub display
goto main