Variable within Variable


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2008
    Posts
    22

    Default Variable within Variable

    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

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    You mean an ARRAY variable?

    Section 4.5 Page 23 of your PBP manual.

  3. #3
    Join Date
    Nov 2008
    Posts
    22


    Did you find this post helpful? Yes | No

    Default

    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
    Last edited by munromh; - 7th January 2009 at 18:10.

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    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.

  5. #5
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Dave
    Always wear safety glasses while programming.

  6. #6
    Join Date
    Nov 2008
    Posts
    22


    Did you find this post helpful? Yes | No

    Default

    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

Similar Threads

  1. EEPROM Variables (EE_Vars.pbp)
    By Darrel Taylor in forum Code Examples
    Replies: 79
    Last Post: - 26th October 2012, 00:06
  2. Variable PWM PIC18F2431
    By trr1985 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 12th June 2009, 06:03
  3. Help with sound command in 2 programs
    By hyperboarder in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th July 2007, 20:36
  4. WORD vs BYTE variable lengths
    By bartman in forum General
    Replies: 0
    Last Post: - 28th November 2005, 21:16
  5. Storing a variable in EEPROM
    By Tissy in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 29th October 2005, 13:53

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts