Shop OBEX P1 Docs P2 Docs Learn Events
name changes in a do loop — Parallax Forums

name changes in a do loop

ftechftech Posts: 8
edited 2009-10-03 03:33 in BASIC Stamp
I am having a·problem with space!
I therfore would like to change·a "name"·an make it·"variable name"·in a do loop
ie·as in·below I am using·in Chris data logger program for the data logger and I would like to change "header" in the below code to a number of different names. so i could do something like·where button one being prees in other parts of program would GOTO button1

CHRIS CODE
READ·"VAR NAME" + counter, ioByte······ ' Get Next Byte From EEPROM·

I WOULD LIKE TO DO SOMTHING LIKE THIS

button1:
·"VAR NAME"= header1.
GOSUB·· dataread

button2:
"VAR NAME"= header2.......·· etc NEED 4 NAME CHANGES
GOSUB·· dataread

dataread:
DO
TOGGLE LEDA························ ' Blink Red LED
READ·"VAR NAME" + counter, ioByte······ ' Get Next Byte From EEPROM·
....all othere lines code as in data logger program
LOOP

question is i guess
How can i make header be a variable and therfore changed to Header1 or Header2 Header3 etc
Reason being I need to same similar do loops in getting data from EEPROM and writen to logger and not enough room to double / tripple·up on similar actions

I tried searching fourm do loops variables but didnt find answer confused.gif
Thanks for any ideas or what to type to search for answers
idea.gifcool.gif Cheers

Comments

  • sylvie369sylvie369 Posts: 1,622
    edited 2009-09-26 11:32
    What you want, I think, is an array. This from the PBASIC Syntax Guide (help file), under Memory Organization and Variables:
    PBASIC documentation guy said...

    On the BS2-family, you can also define multipart variables called arrays. An array is a group of variables of the same size, and sharing a single name, but broken up into numbered cells, called elements. You can define an array using the following syntax:

    name VAR Size(n)

    where name and Size are the same as described earlier. The new argument, (n), tells PBASIC how many elements you want the array to have. For example:

    myList VAR Byte(10) ' Create a 10-byte array

    Once an array is defined, you can access its elements by number. Numbering starts at 0 and ends at n–1. For example:

    myList(3) = 57 DEBUG ? myList(3)

    This code will display "myList(3) = 57" on the PC screen. The real power of arrays is that the index value can be a variable itself. For example:

    myBytes VAR Byte(10) ' Define 10-byte arrayindex VAR Nib ' Define normal nibble variableMain: FOR index = 0 TO 9 ' Repeat with index= 0, 1, 2...9 myBytes(index) = index * 13 ' Write index*13 to each cell of array NEXT FOR index = 0 TO 9 ' Repeat with index= 0, 1, 2...9 DEBUG ? myBytes(index) ' Show contents of each cell NEXT STOP

    If you run this program, DEBUG will display each of the 10 values stored in the elements of the array: myBytes(0) = 0 * 13 = 0, myBytes(0) = 1 * 13 = 13, myBytes(2) = 2 * 13 = 26 ... myBytes(9) = 9 * 13 = 117.

  • vaclav_salvaclav_sal Posts: 451
    edited 2009-09-26 15:26
    Array will not save you program "space" and it will take more registers space.
    You be better off using "alias" in naming your variables. Of course you will need to keep track of their usage carefully. Program with aliases is harder to debug.
    Isn’t frustrating when you are short just few locations?
    Maybe you should post you program to get the forum to take a look if there is a room for improvement.
    Sometimes you do not see the obvious and someone else will.
  • ftechftech Posts: 8
    edited 2009-09-27 00:35
    Hi all
    Yeah program is not at a stage to post.. its a work in progress and in idea form at the moment... think I maybe asking the wrong question or not understanding answer!
    The variable is NOT to be a number it is a NAME!

    The RESULT from Header = Robert cant result in Header = 4... ie be a number as it is using a counter to count along the bits of data ie

    Header + counter , ioByte

    I want to change Header to header2 or Robert, Paul etc…even just a G or H so in each loop the program will go to next bit of data at Robert, or G or Paul for each loop till counter makes ioByte 255

    If header becomes a number then ioByte will jump to that spot missing data! ie

    instead of
    robert + counter, ioByte.....= robert 3,3
    if
    Robert = 4 it will be
    4+counter,iobyte ... = 4+3,3

    Or am I missing something and having the 4 will not create a math problem? I not great at this stuff

    cheers

    cheers



    I am not past this problem as would like to simplify but now having problem with renaming file which worked before and I have not change it… well hadn’t at that time now I changing it but its right …rrr



    There might be a UFO sighting shortly as the unit fly’s over air space when thrown out my window
  • vaclav_salvaclav_sal Posts: 451
    edited 2009-09-27 04:25
    The parameter "Location" is a number, or you could envision it as an index to the RAM. It can be represented by variable/constant/expression as the READ command description states. What you need to come up with symbolic representation of your names and after you process the READ "assign" the result to your name symbol.
    Header VAR Byte
    Peter CON 120
    Paul CON 100
    Mary CON 130
    ....

    Header = Peter
    GOSUB XX
    .....
    Header = Paul
    GOSUB XX
    ...
    .XX:
    READ Header + x , Result
    RETURN



    Location Location is a variable/constant/expression* (0 - 255 on BS1, 0 - 2047 on all other BASIC Stamps) that specifies the EEPROM address to read from.

    Post Edited (vaclav_sal) : 9/27/2009 4:33:57 AM GMT
  • ftechftech Posts: 8
    edited 2009-09-30 04:49
    Great thankyou all for your help!
    Hopefully I can now sort at least that part out and get it running

    cheers hop.gif
  • ftechftech Posts: 8
    edited 2009-10-02 01:59
    Well sadly it seems "I am tooo stupid for the stamp programing"... as i didnt keep the boxes I cant return them with the afore mention quote. ;o)
    I have searched, I read, I asked my brother but i cant do it... could someone draw me a idiot map please to solve this:

    headers data "xxxxxxxxx", 34, xxxxxxx, 0
    data "xxxxx,34,xxxxxxxx,0
    data "xxxxx", 0, 255
    headera data "bbbbbbbb", 34, "bbbbbbbbbb"
    data "bbbbbbbbb", 0
    data "bbbbbb", 0, 255
    headert data "ccccccccccccccccccccccccccccccccccccccccc"
    data "ccccccccccccccccccccccccccccccccccccccccc', 34, "ccccccccccccccccccccccccccccccccccccccccc"0, 255
    headerf data "ddddddddddddddddddddddddd',0
    data "ddddddddddddddddddddddddd, 34, "dddddddddddddddddddddddddddddddddddddddddddd",0
    data "ddddddd" 0, 255

    Write_Headers:
    poss = 0............................ I need this 0 to change to something that the data in EEPROM can be found
    GOSUB headerwrite
    RETURN

    Write_Headera:
    poss = 341..........................I need this 341/or headera location to change to something that the data in EEPROM can be found in same do loop below
    GOSUB headerwrite
    RETURN

    Write_Headerc:
    RETURN
    poss = 384.......................... again this changes in do loop below so do loop write data at headerc

    GOSUB headerwrite
    RETURN

    Write_Headert:
    poss = 84
    GOSUB headerwrite
    RETURN

    Write_Headerf:
    poss = 433
    GOSUB headerwrite
    RETURN

    headerwrite:
    counter = 0
    DO
    TOGGLE LEDA ' Blink Red LED
    READ poss + counter, ioByte ' Get Next Byte From EEPROM
    IF ioByte = 0 THEN ' Replace 0 Bytes w/CRLF
    SEROUT TX\CTS, Baud, [noparse][[/noparse]$08, $20, $00, $00, $00, $02, $0D,
    $0D, $0A, $0D]
    ELSEIF ioByte = 255 THEN ' End Of Data Block
    EXIT
    ELSE
    SEROUT TX\CTS, Baud, [noparse][[/noparse]$08, $20, $00, $00, $00, $01, $0D,
    ioByte, $0D]
    ENDIF
    counter = counter + 1 ' Increment Pointer
    GOSUB Get_Data ' Purge Receive Buffer
    LOOP
    RETURN

    In above in desperation I tried counting the bytes and using them as a number location to the data as I think has been pointed out in above answers?... I used NO header pointers in that attempt, just data no pointers as in headers headera etc... it writes first lot of data but thats it... the data should I get it working will be words and address etc not just number strings. That part I understand how to do as used Chris's Parallax logger program to understand it. If I place MY header pointer in the loop it will write that data!
    My problem is I have the need to go to many Data pointer locations and it takes up to much space to do each pointer do loop seperate and doesnt seem right to do it that way either.
    I am sorry I cant explain it better! I just would like to know how to make the above "poss" a variable pointer location to data so writes data at differen start points! (note I am same file different order of data written and or file open and closed ... ie open file xxx bbb cccc ddddd xxx cccc ddddd xxxx ccccc bbbbbbb cccccc dddddd xxxxxxxx xxxxxxx ddddddd ccccccc close file open new file new order of data close file.

    thank you for you time effor and understanding as I am sure its as frustrating for you to explain as it is for me to try and understand.. also sorry for any spelling or grammer I not any good at it...

    cheers
  • Mike GreenMike Green Posts: 23,101
    edited 2009-10-02 02:07
    I think you need to back up a step. As often happens with beginners, you've jumped too quickly into the details of writing code rather than staying with trying to decide what you're trying to actually accomplish and you're stuck trying to figure out how to do what you want before you've clearly stated what you want to accomplish.

    Start off by describing why you want to do what you're doing, what data is coming into the Stamp and what data is going out and why. You've talked about creating a datalogger. What kind of data are you logging? What's its format? How often is it coming in? Where will it go eventually? What's connected to the Stamp?
  • ftechftech Posts: 8
    edited 2009-10-02 03:05
    Hi Mike yes your right... though i am sure its not that hard... well in my head it isnt... thank you for your time I try and step you through

    Data is going out to logger if one of 4 pins on stamp go high from a external trigger/switch, maybe more if posible.
    These are input Pins on stamp BS2, lets call these pins 1, 2, 3, 4, that are triggerd by a switch logic hi or low

    start:
    (1) program will open a file on data logger
    (2) run a constant loop checking these pins.
    (3) If no pins high it will log a data stream xxxxxxx and keep looping and logging input data xxxxxxx. This a chaging input of numbers generated and time basically but other numbers too... this part works fine from external source and if done in one section works on stamp no probs.
    (4) while looping if one of the pins are triggered it will stop looping write that that has happened from eeprom data, ie pin 1 triggered then go write some other fixed data from eeprom in an order and close file from eeprom data. Does this by going to the told eeprom poiner loctations... ie headera headerc footer
    (5) new file will open and repeate but may or may not be same pin same data order ie pin 4 pin 2 pin 4 pin 1 close file

    sounds simple enough ;o) modifying the parallax logger file for gps I can cut and shut program to make it work, but not enough room to double up on do loops etc.
    So to go to more eeprom data loctation points as in say header A B C D E F G and Footer
    "A" being pin 1
    "B" being pin 2 etc
    "E" data point related to action ie pin one triggered its data written followed by some common data to both thats stored at point "E"
    footer written close file

    If I could just swap in below Name for headera headerc footer the program would work... ie

    pin 1 = 1 then
    name = headera
    gosub write_data

    pin 2 = 1 then
    name = headerc................. note that headera, footer or headerc are data location names/points/loctation whatever the correct term is
    gosub write_data

    Write_data
    READ name + counter, ioByte ' Get Next Byte From EEPROM

    if I have to use numbers and count the Bytes to use as start point for data strings who do i do this1 i have seen and tried to use the map on the complier but couldnt work out chart and if you use 1D0 as a loctaion it thinks its a name and want it identified.

    yes programming is over my head though this seems feasable and i am using a Stamp as Machine code or PIC code I cant write so this is only chance I have of maybe getting some logging of data from my R&D development project going.

    cheers
    and thank you
  • Mike GreenMike Green Posts: 23,101
    edited 2009-10-02 05:58
    First of all, you can produce a named constant as a label for a DATA statement. The value of the label is the starting address of the data stored. Typically, you would use some kind of terminator character and a zero byte is often used. This would look like:

    headera DATA "This is text",0
    headerb DATA "Other text",0
    headerc DATA "Still more",0

    You'd output this as follows:

    name = headera
    GOSUB write_data
    name = headerb
    GOSUB write_data
    name = headerc
    GOSUB write_data

    Your write routine would look like:

    write_data:
    READ name,ioByte
    name = name + 1
    IF ioByte = 0 THEN RETURN
    SEROUT <pin>,<Baud>,[noparse][[/noparse]ioByte]
    GOTO write_data
  • allanlane5allanlane5 Posts: 3,815
    edited 2009-10-02 13:59
    headera DATA "This is text",0
    headerb DATA "Other text",0
    headerc DATA "Still more",0

    You'd output this as follows:

    name = headera
    GOSUB write_data
    name = headerb
    GOSUB write_data
    name = headerc
    GOSUB write_data

    Your write routine would look like:

    write_data:
    READ name,ioByte
    name = name + 1
    IF ioByte = 0 THEN RETURN
    SEROUT <pin>,<Baud>,[noparse][[/noparse]ioByte]
    GOTO write_data

    *** I believe you could have a 'write' routine that looked like this:

    write_data:
    · SEROUT <pin>, <baudmode>, [noparse][[/noparse]STR Name]
    · return
    ·
  • Mike GreenMike Green Posts: 23,101
    edited 2009-10-03 03:33
    @allanlane5
    Read the manual's description of STR in the chapter on SEROUT. That only works if the string is stored in a byte array in the (very limited) variable space.
Sign In or Register to comment.