PDA

View Full Version : Variable within Variable



munromh
- 7th January 2009, 14:20
I would like to use a variable within a variable ie instead of:


If Data_Sen1 = 255 then

I would like to use:


If Data_Sen(Current_Sensor) = 255 then

Where Current_Sensor is a variable from 1 to 3

Can't seem to find anything on this, but thought I had at one time ....

Thanks,
Mark

Melanie
- 7th January 2009, 14:47
You mean an ARRAY variable?

Section 4.5 Page 23 of your PBP manual.

munromh
- 7th January 2009, 17:45
I think an array may work, I'm playing with it now.

To answer your question mark at the end of the message, essentially I have three sensors which need calibration and one button used to do the calibration. The three sensors calibration values are stored in variables (Data_Sen1, Data_Sen2, Data_Sen3). I wanted to write the code so it "loops" and which sensor you're calibrating is changed with a certian button push. I didn't want to write extra code for each sensor when I could do it in a loop.

The calibration flow is as follows:

-Enter the calibration loop

system ready to cal sensor 1 (Data_Sen1)
cal direction set to up (increase value)
-if you push the button once, cal value increases by one.
-if you push the button twice, cal direction switches to down (decrease value)
-subsequent single button pushes decrease the cal value.
-if you push the button three times, the program moves onto the next sensor (Data_Sen2).
-push the button four times and we exit the calibration loop.

It looks like I could use sensor variables in an array such as Data_Sen[i] and alter the value of i as I move through the program.

I'm pretty sure I've used this before and just forgot, thanks for the reminder. I also recall that setting up an array such as:

Data_Sen var byte[3] would give Data_Sen[0] thru Data_Sen[2]. I'd need to setup Data_Sen var byte[4] if I wanted to use the numbers [1] -> [3]

Mark

Melanie
- 7th January 2009, 18:45
Remember counting starts at ZERO... so...

Data_Sen var BYTE [3]

...gives you three elements...

Data_Sen(0)
Data_Sen(1)
Data_Sen(2)

Oh... Note the use of round ( ) brackets above? Although it doesn't say it in the manual, you can use them as easilly as you can square ones [ ] too.

mackrackit
- 7th January 2009, 21:20
http://www.picbasic.co.uk/forum/showthread.php?t=544
More from Melanie.

munromh
- 8th January 2009, 14:44
Thanks all, works like a champ. I knew I'd done this before, but apparently I'd forgotten. It all came back once I got back on the bike. I just needed a push start.

Thanks again,
Mark