How to use Arrays Using Pic Basic Pro(need help)


Closed Thread
Results 1 to 27 of 27
  1. #1
    MrSafe's Avatar
    MrSafe Guest

    Default How to use Arrays Using Pic Basic Pro(need help)

    Hi, everybody New to the forum just wanted to give a shout out to everyone before I proceed.



    Problem: I Can not figure out how to use arrays with Pic Basic Pro

    Need: my design revolves around witlessly transferring data over the air on to a receiving unit which will then display the incoming message. Currently my program simply displays the message as it is being typed in. However I would like to store the message in an array of lets say of 48 elements in a single dimension just a single row. I would then like to send the data that is being pressed on the keypad and have it stored in an element of the array after which it will then increment to the next position or next element for the next input.

    What I have Tried: I tried declaring the array by doing the fallowing

    htxt var word[48]

    and when I try to use it after declaring it

    htxt[0] = htxt[x]
    temp[x] = htxt[x]

    and when a keypad key is pressed

    temp[x] = #key
    serout.1200.PORTD.0, [temp[x]]
    pause 100
    temp[x] = htxt[x] + 1

    when i use the fallowing method my receiving unit simply displays a blank message when ever the data is transmitted. The way I send data is after the message has been inputed and stored I then send each element by itself to the receiving unit so that I can loop the message since theres more than one receiving unit and this way if one unit is turned on after the first unit it will receive the data one the next cycle of the loop.


    *****UPDATE******

    I have uploaded my code but please understand that it does not contain the portion above simply because I could not get it to work so I used my old method to get the software working. I uploaded it so people will understand that I am using PIC Basic Pro
    Attached Files Attached Files
    Last edited by MrSafe; - 30th June 2007 at 02:35.

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Hello,

    Are you using PicBasic Pro?
    Doesn't appear to be the case. (wrong syntax)

    In which case, you're on the wrong forum.

    Regards,
    DT

  3. #3
    MrSafe's Avatar
    MrSafe Guest


    Did you find this post helpful? Yes | No

    Default

    Yes I am 100% sure I am using PIC Basic Pro.

    Im not sure why arrays wont work for me everything else I can manage but I would love to use arrays, can you point me in the direction or give me a example of how to use an array where if the user pushs a button on the keypad and it stores it in the element and then it increments to next element?

    I know of other methods of storing the values inputed by the user but it would require a VERY lengthy code.

    I will try and upload my current code but but please be nice :P I am still working on it as soon as i figure out the array problem it will cut my program in by 2/3.
    Last edited by MrSafe; - 30th June 2007 at 02:31. Reason: More Info

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Ok, let's see what we can do.

    First off, the brackets.
    Use square brackets when declaring the Array, and round brackets when using it.

    That's not always a requirement, but if you are using statements like SEROUT that uses square brackets, PBP gets confused with the square brackets inside other square brackets.

    And, the SEROUT syntax is different than shown ...
    Code:
    Include modedefs.bas
    
    temp  VAR BYTE[48]
    
    SEROUT PORTD.0, T1200,[temp(x)]
    Also note that you are recieving/sending BYTEs, not WORDs. So the array should be BYTE[48].
    will try and upload my current code
    Great, that'll help, cause I'm still trying to figure out what you were trying to do with the temp array.
    but please be nice :P
    I promise that even if I'm mean, I'll do it in a Nice way.
    <br>
    DT

  5. #5
    MrSafe's Avatar
    MrSafe Guest


    Did you find this post helpful? Yes | No

    Default

    What im trying to attempt is when the user decides to input a message I would like to store every keystroke in the array until he or she is satisfied with the message then I would like to send the message by using arrays I can easily loop the message so for example the user inputs "Accident near exit 101" . I would like to store that in an array and since I will be storing every keystroke the program will take "A" and store it in temp[0] then store "c" in the next array element which would be temp[1] and so on.

    Also I went and uploaded my current program to clarify that I am using PBP
    Last edited by MrSafe; - 30th June 2007 at 03:36. Reason: grammer

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Yup, that's PicBasic Pro.

    First thing to do is to change the keypad routines so that it only "gets the key's".
    This will also reduce the program size quite a bit. Sending to the LCD and SEROUT can be done later, once you know what the character is.

    There are other ways to do it, but I'm trying to keep it in the same context as your program.

    For instance, with the alpha1: routine.
    It can be reduced to ....
    Code:
    alpha1:
        gosub getkey
        LOOKUP key,[" ABCDEFGHIJ"],Char
    RETURN
    Same for the alpha2 and main getkey routines.

    The idea is that the actual character you want will be stored in the "Char" (byte sized) variable. Then you can do what you want with it later, instead of 36 different versions of what to do with it if a certain key is pressed.

    Then, once you have the kaypad routine only returning the desired characters, you can do something like this...
    Code:
    Finished VAR BIT  : Finished = 0
    StrLen   VAR BYTE : StrLen = 0
    
    while not Finished
        gosub getkey
        if Char != EOM then ' End of message key
            htxt(StrLen) = Char
            LCDOUT Char
            StrLen = StrLen + 1
        else   ; originaly post as an endif (DOH!)
            Finished = 1
        endif
    wend
    SEROUT2  PORTD.0, 813, [STR htxt\StrLen,13,10]
    Of course, this doesn't allow backspaces and the like while editing. But I gotta leave something for you to do.

    HTH,
    DT

  7. #7
    MrSafe's Avatar
    MrSafe Guest


    Did you find this post helpful? Yes | No

    Default

    The backspacing and what not are very easy for me to figure out in fact I have several methods my main concern is arrays and to understand them I have been reading on what you said earlier about using byte size and honestly when I read that after you told me I almost punched myself in the gut because my brain immediately was like "oh yea...."


    The Keypad routine currently just gets the keys it does nothing else I myself display it on the lcd.

    ******
    gosub getkey
    LOOKUP key,["ABCDEFGHIJ"],Char

    how does this work I have seen this before but I could never wrap my hands around it and get the feel for it.

    *******
    Finished VAR BIT : Finished = 0 ;;I'm a little confused here are you commenting it or is it part of the declaration?( : Finished = 0 )


    StrLen VAR BYTE : StrLen = 0 ;; Same thing is it just being commented on?( : StrLen = 0 )

    while not Finished ;; is not part of PBP? in my book it does not even suggest anything of this nature


    gosub getkey ;;Understand this portion

    if Char != EOM then ' End of message key ;;is char the key that is received from the keypad?

    htxt(StrLen) = Char ;;I understand this portion just need to know what Char is

    LCDOUT Char

    StrLen = StrLen + 1 ;;Does this increment to next position?
    endif
    Finished = 1
    endif
    wend
    SEROUT2 PORTD.0, 813, [STR htxt\StrLen,13,10] ;; May I still use SEROUT or Must I use SEROUT2. ( [STR htxt\StrLen,13,10] I do not full Understand this portion could you explain to me what this is doing?) Also why do you have 2 endif statements?

    Forgive me for my ignorance I have been writing assembly for about 2 years now and I heard PBP was the next step and its been fun just a couple of walls I have to climb over frankly biggest wall is this array problem! I never had to deal with arrays before.

    Thank You Soo Much for taking the time and effort to help me I understand you have much better things to attend to and I appreciate all that your willing to do to help me!
    Last edited by MrSafe; - 30th June 2007 at 13:25. Reason: forgot something

  8. #8
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    while not Finished ;; is not part of PBP? in my book it does not even suggest anything of this nature
    Really?
    You must have a very old version.
    While:Wend has been there for as long as I can remember.

    In case it's just an old manual mixup ...
    Try looking here.
    http://www.melabs.com/resources/index.htm#Manuals

    LOOKUP key,[" ABCDEFGHIJ"],Char

    how does this work I have seen this before but I could never wrap my hands around it and get the feel for it.
    There needs to be a space before the "A", just because your keypad routine only returns values starting at 1, and Lookup starts at 0.

    LOOKUP is pretty straight forward.
    It returns the value from the "List" of items that corresponds to the "key" value.

    If key = 1 then after the statement, Char will be "A". And ...
    key = 2, char = "B"
    key = 3, char = "C"
    etc. etc.

    Finished VAR BIT : Finished = 0 ;;I'm a little confused here are you commenting it or is it part of the declaration?( : Finished = 0 )
    Ummm, we'll call that the "skimask initialization syntax".
    You might get that later. Inside Joke.

    But no, it's not commented. The colon ":", separates two statements, as if they were on separate lines. I.E...
    Code:
    Finished VAR BIT : Finished = 0
    
    ; is the same as
    
    Finished VAR BIT
    Finished = 0
    StrLen = StrLen + 1 ;;Does this increment to next position?
    Yes it does.

    I know there's more questions, but let's see where this get's you?
    <br>
    DT

  9. #9
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor View Post
    Ummm, we'll call that the "skimask initialization syntax".
    You might get that later. Inside Joke.

    But no, it's not commented. The colon ":", separates two statements, as if they were on separate lines.
    Standard BASIC syntax from "In the beginning...."

    Anybody remember Beagle Bros. Two-Liners? What could you do with 2 lines of code, packed as tightly as possible? I think it was 64 characters max per line, but can't remember.

    I have actually picked up the technique again, for the same reason Skimask mentioned: Less scrolling, more compact code listings, keeps related code elements together. Once I got used to it again, I found it easier to read (as long as it wasn't used excessively). Thanks Skimask!

    SteveB

    Edit: A little research yields "less than approximately 150 characters" per line of Apple II code. Page 23 (26th pdf page) of this reproduction 1978 Apple II reference Manual
    Last edited by SteveB; - 30th June 2007 at 18:49.

  10. #10
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    I agree, in general it's a good idea, but like beer, when used with moderation

    Not bad...
    Code:
    PORTB=0 : LATB=0 : TRISB=0
        '    OR
    SELECT CASE Pouet
        CASE 1 : Gosub MarryHadALittleBoy
        CASE 2 : Gosub DoSomethingElse
        '
        END SELECT
    But erm...
    Code:
    CounterA=0 : CounterB = 1234 : TRISB = 0 : OPTION_REG=%10101010 : LCDOUT $FE,1,"Hello dear!" : Gosub KindaLongLine : IF A=0 then : V=1234 : ELSE : q=908 : ENDIF : GOSUB ItsReallyReallyLongNow : PORTA=128 : PORTC=144 : PAUSE 150 : I2CREAD SDA, SCL, ControlByte, Address, [$FF,$EA, 1,2,3,4,5,6,7,8,9,CounterA, CounterB,0,0,0] : LCDOUT $FE,$C0,"Ok i think you see the picture now" : Gosub OKImDone ' Startup thingy :o)
    that's a bit too much, not sure if it compile anyway

    I don't remind if that syntax was available on that one... ;o}


    That's the whole kit with the speech synthesizer module, but without the tape deck... nostalgic now :-(

    Last edited by mister_e; - 30th June 2007 at 19:09.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  11. #11
    MrSafe's Avatar
    MrSafe Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor View Post
    Really?
    You must have a very old version.
    While:Wend has been there for as long as I can remember.

    In case it's just an old manual mixup ...
    Try looking here.
    http://www.melabs.com/resources/index.htm#Manuals

    There needs to be a space before the "A", just because your keypad routine only returns values starting at 1, and Lookup starts at 0.

    LOOKUP is pretty straight forward.
    It returns the value from the "List" of items that corresponds to the "key" value.

    If key = 1 then after the statement, Char will be "A". And ...
    key = 2, char = "B"
    key = 3, char = "C"
    etc. etc.


    Ummm, we'll call that the "skimask initialization syntax".
    You might get that later. Inside Joke.

    But no, it's not commented. The colon ":", separates two statements, as if they were on separate lines. I.E...
    Code:
    Finished VAR BIT : Finished = 0
    
    ; is the same as
    
    Finished VAR BIT
    Finished = 0


    Yes it does.

    I know there's more questions, but let's see where this get's you?
    <br>
    Thank You I will go and test this out I will be gone for a few days but I will return with the result. Thank You again

    I know understand a little bit im still confused on how to use arrays specifically but this portion I understand to a great degree.

    I understand how to declare my array but thats about it however I will tr first with this knowledge and see if that works. I hope this thread isnt closed :-) If you dont mind I will beback with a few more questions thank you again.

  12. #12
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Was getting pretty tired during that last post, so cut it off a little short. But, now that I've had a good Day's sleep, let's see if I can tackle the rest.

    You're probably out of town by now, but that's ok. It'll still be here when you get back.

    Also why do you have 2 endif statements?
    Good catch, the first one was supposed to be an ELSE.(DOH!) I modified the original post.

    SEROUT2 PORTD.0, 813, [STR htxt\StrLen,13,10] ;; May I still use SEROUT or Must I use SEROUT2. ( [STR htxt\StrLen,13,10] I do not full Understand this portion could you explain to me what this is doing?)
    No, you don't have to use SEROUT2. It just makes it a little easier, so I tend to use it more often.

    The STR-ing function can send the entire contents of an array with one easy statement. In the case of STR htxt\StrLen, it will send the number of bytes specified in the StrLen variable from the htxt array.

    It's pretty much the same as ...
    Code:
    For X = 0 to StrLen
        SEROUT PORTD.0, T1200,[htxt(X)]
    NEXT X
    It's up to you which one you prefer. But STR isn't avaialable with SEROUT.

    And ..., oh I guess that's all there was. Must have been really tired.
    <br>
    DT

  13. #13
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Default Coding styles

    There's big upsides in producing clean, readable and well commented code. Namely for mainly for maintainability. When you type out large programs without comments and go back to them 6 months down the track to revise them, you'll find that you'll spend some considerable time in recalling what's what. Comments also make your program much more legible to anyone else who reads them. Some languages are case sensitive, Java's one of them, I think this in someways encourages the programmer to adhere to acceptable standards. Indenting your code also improves readability, in my opinion, a fine example of well formated code would be Melanie's, Darrel's & Mister_e's. In the long run, you'll save time and probably end up producing much better algorithms if you go the extra mile to properly format & document your work. Most often, programs written in BASIC that make use of many colons are quite quickly dismissed as "spaghetti code"

  14. #14
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor View Post
    Ummm, we'll call that the "skimask initialization syntax".
    HEY...I resemble that remark!

  15. #15
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: TRS80

    Quote Originally Posted by mister_e View Post
    I agree, in general it's a good idea, but like beer, when used with moderation

    [/img]
    I never could get one of those to work . . . could't find the ANY Key
    EDIT: DOH TI90 either
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  16. #16
    MrSafe's Avatar
    MrSafe Guest


    Did you find this post helpful? Yes | No

    Default

    I still can not get the arrays to work. Look at my program to see what I am doing wrong I haven't tried the other suggestions yet but I will get started on those as soon as i figure out the arrays.
    Attached Files Attached Files

  17. #17
    MrSafe's Avatar
    MrSafe Guest


    Did you find this post helpful? Yes | No

    Default

    In the program when i do

    lcdout #key it shoots out the key correctly on the lcd however

    when i use

    htxt(x) = #key

    it says bad expression when i try to compile

    also when i use

    htxt(x) = key

    it works fine however it displays a blank spot on the lcd

  18. #18
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by MrSafe View Post
    In the program when i do
    lcdout #key it shoots out the key correctly on the lcd however
    when i use
    htxt(x) = #key
    it says bad expression when i try to compile
    also when i use
    htxt(x) = key
    it works fine however it displays a blank spot on the lcd
    htxt(x) = key ------>>>>> lookup key , [ "0123456789ABCDEF" ] , htxt[x]
    ^get rid of^---------------^^^^^^ replace with ^^^^^^^^^^^

    Your turn to figure out why assigning the variable 'key' to htxt(x) and trying to get the LCD to display what is effectively variable 'key' doesn't work.
    What is the ASCII character equivalent to 'key', if 'key' = 1? if 'key' = 2? if 'key' = 65?
    Last edited by skimask; - 3rd July 2007 at 02:08.

  19. #19
    MrSafe's Avatar
    MrSafe Guest


    Did you find this post helpful? Yes | No

    Default

    i see ill try thank you for the reply. If you don't mind could you give me an example of that a very simple version of it that I could run with. Im sorry for being so ignorant but arrays is brand spanking new to me.


    Your turn to figure out why assigning the variable 'key' to htxt(x) and trying to get the LCD to display what is effectively variable 'key' doesn't work.
    What is the ASCII character equivalent to 'key', if 'key' = 1? if 'key' = 2? if 'key' = 65?
    I can do that quite effectively ahaha its this damn blasted arrays thats slowing me down I have entire codes already done for that portion its just that I want to be able to loop the message over and over but I need arrays to do that and the message is not predetermined it will be different depending on the user.
    Last edited by MrSafe; - 3rd July 2007 at 02:14.

  20. #20
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by MrSafe View Post
    i see ill try thank you for the reply. If you don't mind could you give me an example of that a very simple version of it that I could run with. Im sorry for being so ignorant but arrays is brand spanking new to me.
    I just did...

  21. #21
    MrSafe's Avatar
    MrSafe Guest


    Did you find this post helpful? Yes | No

    Default

    thank you it didn't appear for me

  22. #22
    MrSafe's Avatar
    MrSafe Guest


    Did you find this post helpful? Yes | No

    Default

    The code below will compile all I did different from the code on the bottom is change the htxt[x] to x
    alpha1:
    gosub getkey
    LOOKUP key,[" ABCDEFGHIJ"],x
    htxt[x] = htxt[x] + 1
    RETURN

    alpha2:
    gosub getkey
    LOOKUP key,[" KLMNOPQRST"],htxt[X]
    htxt[x] = htxt[x] + 1
    RETURN
    The code above will not compile the compiler gives me the fallowing error

    "output parameter must be a variable"
    Attached Files Attached Files

  23. #23
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by MrSafe View Post
    The code above will not compile the compiler gives me the fallowing error
    "output parameter must be a variable"
    The answer is in the manual under the specifications for the LOOKUP...
    Array variables with a variable index (ex. htxt[x] ) may not be used in LOOKUP although array variables with a constant index (ex. htxt[1] ) are allowed

    So, don't use what doesn't work, and use what does work and change it around a bit...
    Code:
    alpha2:
    gosub getkey
    LOOKUP key,[" KLMNOPQRST"],tempx
    htxt[x] = tempx
    htxt[x] = htxt[x] + 1
    RETURN
    Last edited by skimask; - 3rd July 2007 at 05:13.

  24. #24
    MrSafe's Avatar
    MrSafe Guest


    Did you find this post helpful? Yes | No

    Default

    I Got the code to work with each part. By that I specifically mean



    alpha1:
    gosub getkey
    LOOKUP key,[" ABCDEFGHIJ"],Char
    if (key = 1) Then char = "A"
    if (key = 2) Then char = "B"
    if (key = 3) Then char = "C"
    if (key = 4) Then char = "D"
    if (key = 5) Then char = "E"
    if (key = 6) Then char = "F"
    if (key = 7) Then char = "G"
    if (key = 8) Then char = "H"
    if (key = 9) Then char = "I"
    if (key = 0) Then char = "J"
    RETURN
    They work by them selfs however when I combine all of them like in the code provided they do not work I have tried everything I could think of any suggestions?


    **UPDATE**

    What I want my program to do after he or she has chosen to type their message in is I want it to store every character and number that they type in and display it on the lcd. What I thought would be the best method to do this is to use an array to store the message keystroke by keystroke and display it as he or she is typing the message after which to send the stored data out one byte at a time to the destination I am transferring the data wireless over the air.
    Last edited by MrSafe; - 3rd July 2007 at 06:10.

  25. #25
    MrSafe's Avatar
    MrSafe Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    The answer is in the manual under the specifications for the LOOKUP...
    Array variables with a variable index (ex. htxt[x] ) may not be used in LOOKUP although array variables with a constant index (ex. htxt[1] ) are allowed

    So, don't use what doesn't work, and use what does work and change it around a bit...
    Code:
    alpha2:
    gosub getkey
    LOOKUP key,[" KLMNOPQRST"],tempx
    htxt[x] = tempx
    htxt[x] = htxt[x] + 1
    RETURN
    Thank you ahah I figured it out my manual does not have many of these commands for some reason either i got ripped off or I have a REALLY OLD one. I ordered a fresh copy from the link given above they sent the software which I don't need but its nice to have a backup.

  26. #26
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by MrSafe View Post
    They work by them selfs however when I combine all of them like in the code provided they do not work I have tried everything I could think of any suggestions?
    I don't follow what you mean...

  27. #27
    MrSafe's Avatar
    MrSafe Guest


    Did you find this post helpful? Yes | No

    Default

    I finally got the arrays to work correctly thank you very much.
    However now I am in a new hole. When I try to transmit the array using the command

    SEROUT2 PORTD.0, T1200, [STR htxt\StrLen,13,10]

    it will not send any data out. I found out the 13,10 in the program command is related to the hyper terminal in the pc from what I understand. so I modified the program command to

    SEROUT2 PORTD.0, T1200, [STR htxt\StrLen]

    I am using a TX module that transmits any data that is transmitted to it serially it sends out wirelessly and the RX unit then serially transmit data to pic as you can see from the command I am using PORT D pin Zero at baud rate 1200 however I am confused as to why it is not receiving any of the data do I have to change anything on the receiving end to make it work? I have included both my transmitting and my receiving codes.
    Attached Files Attached Files

Similar Threads

  1. Sending Commands from Visual Basic about IR to Pic
    By tne_de in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 29th April 2009, 06:09
  2. Loop with two motor and 2 sensors
    By MrRoboto in forum mel PIC BASIC
    Replies: 4
    Last Post: - 8th December 2008, 23:40
  3. using AND as an IF statement
    By dw_pic in forum mel PIC BASIC
    Replies: 27
    Last Post: - 8th June 2006, 18:05
  4. vb6 to pic basic
    By Darrenmac in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 19th December 2005, 01:56
  5. The Ultimate PIC Basic
    By picnaut in forum PBP Wish List
    Replies: 4
    Last Post: - 9th November 2004, 22:10

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