Shop OBEX P1 Docs P2 Docs Learn Events
Need help, Again. This time with arrays — Parallax Forums

Need help, Again. This time with arrays

RavenkallenRavenkallen Posts: 1,057
edited 2010-04-23 17:20 in Propeller 1
Hey, i can't figure out how to get a blasted array to work. I need read in keyboard data and store it in a array or string or something. Here is the code i used and i can't get the desired results.....

repeat
·Message[noparse][[/noparse]counter] := chardata2
·waitcnt(clkfreq/4 + cnt)
·counter++

i want to put single bytes into a array, but i do not want to have to declare every byte in the array....E.G

Message[noparse][[/noparse]0]
message[noparse][[/noparse]1]
message[noparse][[/noparse]2]
ect....

Can variables be used as a pointer to a array. This has been so annoying to get it work. IF you guys have any code snippets or ideas, please share with a newbie. I know you can do the lookup thing, but i don't know if you can write to it..... I am at my wits end. Thank you all, for allready helping me with previous posts. It is good to know that there are people who care about more than themselves.

Comments

  • MagIO2MagIO2 Posts: 2,243
    edited 2010-04-22 16:50
    Of course you can access each bayte in a byte array by using a variable as index-variable.

    But you have to take care that the index is not bigger than the last possible index of the array. Nothing will check that for you - neither the propeller during runtime (like it's done in Java), nor the compiler during compile-time ( so you could have a message[noparse][[/noparse] 100 ] instruction somewhere even if the message array only has 40 bytes). When you leave the boundary of an array anything can happen ... reset ... hang ... destroy pins ....

    What's the difference in an array and a string? Easy ... the string is terminated by a 0 (zero). If you read a ascii string using a interface (for example a serial interface) there is usually no string end sent. So, you should be sure not to call a string function without adding the string terminator by yourself.

    A variable can also be used as a pointer to an array. Say you defined your Message variable and want to pass that to an object. Then you have to give the address to the object, as the object won't see the variable named Message.

    VAR
    byte Message[noparse][[/noparse]50]
    PUB
    obj1.fillMessage( @Message )

    the function definition in the object would look like:

    PUB fillMessage( buffer_addr )

    and you can access the content of the array by:

    byte[noparse][[/noparse] buffer_addr ]
    or
    byte[noparse][[/noparse] buffer_addr ][noparse][[/noparse] counter ]

    in the first case you have to increase buffer_addr if you want to loop through the array. In the second case you can increase counter instead.
  • RavenkallenRavenkallen Posts: 1,057
    edited 2010-04-22 17:03
    I still don't really get it. I don't understand why you just can't use the raw variable as a pointer. Maybe it would be easier if i uploaded the whole code and see if you could find the problem. Thanks for the quick reply.
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-04-22 17:50
    Your original example will work if you define Message like MagIO2 did.· If you want to use a pointer then you need to put an "@" in front of it.· So either of the following would work

    Message[noparse][[/noparse]counter] := chardata2
    or
    byte[noparse][[/noparse]@Message + counter] := chardata2
    or
    byte[noparse][[/noparse]@Message][noparse][[/noparse]counter] := chardata2

    Dave
  • RavenkallenRavenkallen Posts: 1,057
    edited 2010-04-22 18:37
    Thanks, dave. I STILL don't get the define thing you are talking about. I don't want to pass it to another object. I Just want to read inputs and place them to a array. Sorry, I am just a little frustrated. If you guys have a full demo that would be fantastic.
  • MagIO2MagIO2 Posts: 2,243
    edited 2010-04-22 20:07
    Why don't you post your code?
  • RavenkallenRavenkallen Posts: 1,057
    edited 2010-04-23 01:56
    Okay this is it. The attachment manager is not working so i will have to copy and paste the whole thing here.



    con
    ·_xinfreq = 5_000_000
    ·_clkmode = xtal1 + pll16X

    obj

    Debug: "fullduplexserial"

    var

    long erase[noparse][[/noparse]3], adcval, adcvaltwo, adcvalthree
    byte chardata, message[noparse][[/noparse]22], shift, othervar, i1, j2, k3, l4, m5, n6, o7, p8, q9, r0, backspace[noparse][[/noparse]2], ss, tt, uu, vv, ww, xx, yy
    byte counter, nextline,space, goback[noparse][[/noparse]2], counter2, chardata2, txbyte


    pub main
    ·debug.start(3, 4, 0, 2400)
    ·erase := string(254, 81)
    ·debug.str(erase)
    ·backspace := string(254, 78)
    ·nextline := string(254, 69, 64)
    ·goback := string(254, 73)
    repeat
    ·chardata := 0
    ·readadc
    ·if chardata > 1
    · debug.tx(chardata)
    · Message[noparse][[/noparse]counter] := chardata
    · waitcnt(clkfreq/4 + cnt)
    · counter++


    ·if counter == 20
    · debug.str(nextline)

    ·if counter == 40
    · debug.str(erase)
    · counter2 := 0
    · repeat 20
    ·· byte[noparse][[/noparse]@message + counter] := txbyte
    ·· debug.tx(txbyte)
    ·· counter++
    · waitcnt(clkfreq * 2 + cnt)
    · debug.str(erase)
    · counter := 0
    ·


    pub readadc






    ·

    ···
    ···
    · ctra[noparse][[/noparse]30..26] := %01000
    · ctra[noparse][[/noparse]5..0] := 13
    · frqa := 1
    · dira[noparse][[/noparse]13] := outa[noparse][[/noparse]13] := 1
    · waitcnt(clkfreq/1_000 + cnt)
    · phsa~
    · dira[noparse][[/noparse]13]~
    · waitcnt(clkfreq/200 + cnt)
    · phsa := phsa/ 100
    · adcval := phsa
    ·
    ·

    ·
    · ctra[noparse][[/noparse]30..26] := %01000
    · ctra[noparse][[/noparse]5..0] := 14
    · frqa := 1
    · dira[noparse][[/noparse]14] := outa[noparse][[/noparse]14] := 1
    · waitcnt(clkfreq/1_000 + cnt)
    · phsa~
    · dira[noparse][[/noparse]14]~
    · waitcnt(clkfreq/200 + cnt)
    · phsa := phsa/ 100
    · adcvaltwo := phsa
    ·
    ·

    ·
    · ctra[noparse][[/noparse]30..26] := %01000
    · ctra[noparse][[/noparse]5..0] := 15
    · frqa := 1
    · dira[noparse][[/noparse]15] := outa[noparse][[/noparse]15] := 1
    · waitcnt(clkfreq/1_000 + cnt)
    · phsa~
    · dira[noparse][[/noparse]15]~
    · waitcnt(clkfreq/200 + cnt)
    · phsa := phsa/ 100
    · adcvalthree := phsa

    · if i1 == "1"
    ···
    ··· if adcvalthree < 40
    ···· debug.str(erase)
    ···· debug.str(goback)
    ···· counter := 0
    ····


    ··
    ·
    ·
    · if adcval < 10
    ·· chardata := ss
    ·· adcval := 1_000

    · if adcval < 21
    ·· chardata := tt
    ·· adcval := 1_000

    · if adcval < 34
    ·· chardata := uu
    ·· adcval := 1_000

    · if adcval < 46
    ·· chardata := vv
    ·· adcval := 1_000

    · if adcval < 58
    ·· chardata := ww
    ·· adcval := 1_000

    · if adcval < 70
    ·· chardata := xx
    ·· adcval := 1_000
    ··

    · if adcval < 80
    ·· chardata := yy
    ·· adcval := 1_000

    · if adcval < 94
    ·· chardata := "z"
    ·· adcval := 1_000

    · if adcval < 105
    ··· i1 := "i"
    ··· j2 := "j"
    ··· k3 := "k"
    ··· l4 := "l"
    ··· m5 := "m"
    ··· n6 := "n"
    ··· o7 := "o"
    ··· p8 := "p"
    ··· q9 := "q"
    ··· r0 := "r"
    ··· ss := "s"
    ··· tt := "t"
    ··· uu := "u"
    ··· vv := "v"
    ··· ww := "w"
    ··· xx := "x"
    ··· yy := "y"
    ··· space := "a"
    ···
    ·· adcval := 1_000
    ··
    · if adcval < 120
    ·· i1 := "1"
    ·· j2 := "2"
    ·· k3 := "3"
    ·· l4 := "4"
    ·· m5 := "5"
    ·· n6 := "6"
    ·· o7 := "7"
    ·· p8 := "8"
    ·· q9 := "9"
    ·· r0 := "0"
    ·· ss := ","
    ·· tt := "."
    ·· uu := "?"
    ·· vv := "-"
    ·· ww := "'"
    ·· xx := "+"
    ·· yy := " "
    ·· space := " "
    ·· adcval := 1_000


    ··

    ··
    · if adcvaltwo < 10
    ·· chardata := i1
    ·· adcvaltwo := 1_000

    · if adcvaltwo < 21
    ·· chardata := j2
    ·· adcvaltwo := 1_000

    · if adcvaltwo < 34
    ·· chardata := k3
    ·· adcvaltwo := 1_000

    · if adcvaltwo < 46
    ·· chardata := l4
    ·· adcvaltwo := 1_000

    · if adcvaltwo < 58
    ·· chardata := m5
    ·· adcvaltwo := 1_000

    · if adcvaltwo < 70
    ·· chardata := n6
    ·· adcvaltwo := 1_000
    ··

    · if adcvaltwo < 80
    ·· chardata := o7
    ·· adcvaltwo := 1_000

    · if adcvaltwo < 94
    ·· chardata := p8
    ·· adcvaltwo := 1_000

    · if adcvaltwo < 105
    ·· chardata := q9
    ·· adcvaltwo := 1_000
    ··
    · if adcvaltwo < 120
    ·· chardata := r0
    ·· adcvaltwo := 1_000




    · if adcvalthree < 40
    ··· chardata := "a"
    ··· adcvalthree := 1_000


    · if adcvalthree < 100
    ·· chardata := "b"
    ·· adcvalthree := 1_000

    · if adcvalthree < 160
    ·· chardata := "c"
    ·· adcvalthree := 1_000
    ··

    · if adcvalthree < 220
    ·· chardata := "d"
    ·· adcvalthree := 1_000

    · if adcvalthree < 285
    ·· chardata := "e"
    ·· adcvalthree := 1_000

    · if adcvalthree < 350
    ·· chardata := "f"
    ·· adcvalthree := 1_000
    ··
    · if adcvalthree < 410
    ·· chardata := "g"
    ·· adcvalthree := 1_000

    · if adcvalthree < 470
    ·· chardata := "h"
    ·· adcvalthree := 1_000
  • RavenkallenRavenkallen Posts: 1,057
    edited 2010-04-23 02:09
    The above program, reads the three adcs from the resistor divider network of the keyboard. It then outputs the correct ascii character and displays it on a serial lcd. Once counter reaches 40 it then erases the display and reads back the whole array, But it just will not work. The keyboard and display work fine, but i need this to work. Thanks for the help.
  • kuronekokuroneko Posts: 3,623
    edited 2010-04-23 02:11
    Your message array can hold 22 bytes yet your index (counter) can go way over 40. Is that intentional?
  • RavenkallenRavenkallen Posts: 1,057
    edited 2010-04-23 02:17
    @ Kurneko.....Uh, no. I mean i don't know. This is just one of of the many things i tried. I just didn't want to overflow the array. I have tried everything(Or so it seems), and i just can't get the array thing to work. Plus the lcd·is a 20 by 2 so once it reaches 40 it erases the display.·I must be the dumbest dude on the earth
  • kuronekokuroneko Posts: 3,623
    edited 2010-04-23 02:23
    The array access itself is fine, just make sure you stay inside range with the index. Can you describe how the array is to be accessed, e.g. fill with 20 values, find the maximum, clear array, stuff like that?
  • RavenkallenRavenkallen Posts: 1,057
    edited 2010-04-23 02:33
    I want to fill the array with the data from the keyboard. The array needs to be 40 bytes in length, but i just chose 20 for now('cause that is how long the first line of the display is. The answer is probably really simple and i just can't figure it out. Maybe you could post a little demo program on how to use arrays. Thanks again for taking your time.
  • kuronekokuroneko Posts: 3,623
    edited 2010-04-23 03:14
    Ravenkallen said...
    Maybe you could post a little demo program on how to use arrays.
    Well, as demo programs using arrays go this one is probably not the best but maybe it gives you the general idea.
    • it fills a byte array of size length with characters
    • once the array is filled all of it is sent to the display
    • then the array is refilled (overwritten)
    CON
     _clkmode = XTAL1|PLL16X
     _xinfreq = 5_000_000
    
      length = 20
      
    OBJ
      serial: "FullDuplexSerial"
    
    VAR
      long  index
      byte  input[noparse][[/noparse]length]
    
    PUB main
    
      serial.start(31, 30, %0000, 115200)
    
      repeat
        input[noparse][[/noparse]index++] := serial.rx                         ' read character data
        ifnot index //= length                              ' buffer full
          repeat length                                     ' send character data
            serial.tx(input[noparse][[/noparse]index++])                       ' to display
          serial.tx(13)                                     ' next line
          index := 0                                        ' reset buffer index
    
    DAT
    
  • RavenkallenRavenkallen Posts: 1,057
    edited 2010-04-23 03:30
    Thanks, kuroneko. I will try it out tomorrow morning. It is like 11:30 here in New Hampshire, so i must get some sleep.
  • RavenkallenRavenkallen Posts: 1,057
    edited 2010-04-23 14:32
    Well, i tried the demo. All i have to do now is try to interface with my experiment. Thanks for the help
  • RavenkallenRavenkallen Posts: 1,057
    edited 2010-04-23 17:20
    AHHHH, I fiannaly figured it out. I am such a idiot. I made each array element equal a 0 variable, instead of having the variable equal the array element. It works just fine now. Thanks guys for the help.
Sign In or Register to comment.