Variable Variables


Closed Thread
Results 1 to 5 of 5

Hybrid View

  1. #1

    Default Variable Variables

    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?

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


    Did you find this post helpful? Yes | No

    Default

    It's called an array (PBP manual section 4.5).

  3. #3


    Did you find this post helpful? Yes | No

    Default

    This is my issue:

    Code:
        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..

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


    Did you find this post helpful? Yes | No

    Default

    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?

  5. #5


    Did you find this post helpful? Yes | No

    Talking Senior Moment

    Melanie,

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

    Code:
    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

Similar Threads

  1. Help with sound command in 2 programs
    By hyperboarder in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th July 2007, 20:36
  2. Changing declared variables names on the fly
    By jessey in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 16th December 2006, 06:34
  3. 32-bit Variables and DIV32, Hourmeter 99999.9
    By Darrel Taylor in forum Code Examples
    Replies: 9
    Last Post: - 23rd November 2006, 07:23
  4. Variables not appearing in Watch Window MPLabv7.3
    By jd76duke in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 16th June 2006, 14:36
  5. WORD vs BYTE variable lengths
    By bartman in forum General
    Replies: 0
    Last Post: - 28th November 2005, 21:16

Members who have read this thread : 0

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