PDA

View Full Version : How to use Arrays Using Pic Basic Pro(need help)



MrSafe
- 30th June 2007, 01:47
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

Darrel Taylor
- 30th June 2007, 02:20
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,

MrSafe
- 30th June 2007, 02:25
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.

Darrel Taylor
- 30th June 2007, 03:23
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 ...
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>

MrSafe
- 30th June 2007, 03:33
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

Darrel Taylor
- 30th June 2007, 12:58
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 ....
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...

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,

MrSafe
- 30th June 2007, 13:23
The backspacing and what not are very easy for me to figure out in fact I have several methods :D 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!

Darrel Taylor
- 30th June 2007, 15:23
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...
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>

SteveB
- 30th June 2007, 18:34
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 (http://apple2history.org/dl/a2refmanorig.pdf)

mister_e
- 30th June 2007, 18:52
I agree, in general it's a good idea, but like beer, when used with moderation :D

Not bad...

PORTB=0 : LATB=0 : TRISB=0
' OR
SELECT CASE Pouet
CASE 1 : Gosub MarryHadALittleBoy
CASE 2 : Gosub DoSomethingElse
'
END SELECT

But erm...

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}
http://www.mainbyte.com/ti99/computers/system_994a.jpg

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

http://www.mainbyte.com/ti99/ti994a_system.jpg

MrSafe
- 1st July 2007, 00:32
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...
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.

Darrel Taylor
- 1st July 2007, 05:46
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 ...

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. :p
<br>

T.Jackson
- 1st July 2007, 06:15
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"

skimask
- 1st July 2007, 08:08
Ummm, we'll call that the "skimask initialization syntax".
HEY...I resemble that remark! :D

Archangel
- 2nd July 2007, 18:51
I agree, in general it's a good idea, but like beer, when used with moderation :D

[/img]
I never could get one of those to work . . . could't find the ANY Key :)
EDIT: DOH TI90 either

MrSafe
- 2nd July 2007, 23:29
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.

MrSafe
- 3rd July 2007, 00:03
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

skimask
- 3rd July 2007, 02:06
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?

MrSafe
- 3rd July 2007, 02:11
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.

skimask
- 3rd July 2007, 02:12
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...

MrSafe
- 3rd July 2007, 02:14
thank you it didn't appear for me

MrSafe
- 3rd July 2007, 04:00
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"

skimask
- 3rd July 2007, 05:10
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...


alpha2:
gosub getkey
LOOKUP key,[" KLMNOPQRST"],tempx
htxt[x] = tempx
htxt[x] = htxt[x] + 1
RETURN

MrSafe
- 3rd July 2007, 05:57
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.

MrSafe
- 3rd July 2007, 06:00
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...


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.

skimask
- 3rd July 2007, 06:13
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...

MrSafe
- 17th July 2007, 21:56
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.