Shop OBEX P1 Docs P2 Docs Learn Events
Assigning values to array variables — Parallax Forums

Assigning values to array variables

TOMCHTOMCH Posts: 3
edited 2014-04-30 20:50 in BASIC Stamp
I am wondering if I can easily enter values to array variables at the beginning of a program without having to do one variable at a time?
I do remember in a other basic dialect, that you could add them after the declaration separated by a coma.
it looked something like this:
MYARRAY VAR BYTE(8) 112,43,67,32,87,789,0,1
Is something similar possible here?

Thanks

Thomas

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2014-04-29 20:15
    No. You cannot initialize variables that way (other than their automatic initialization to zero. However, you can use the DATA statement to load byte or word data into the EEPROM as part of the program code, then access it using a READ statement. Look at the description and examples of these statements in the Reference Manual. Do remember that there are only 26 bytes of variable space available, so initialization using assignment statements is not unreasonable and you can have several assignment statements on a line of program code (separated by colons).
  • TOMCHTOMCH Posts: 3
    edited 2014-04-30 08:21
    Thank you Mike, that was very helpful.
    here is one more question: I have an byte type variable array and want to extract the each bit individually
    I know I can use a variable to point to the right array variable, but can I also use a variable for the qualifyer like BIT0, BIT1, etc.
    So I guess I am looking for something like:
    myvariable.BIT(mycounter1).(myarrayindex)
    would that work or is there an easier solution?
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2014-04-30 09:15
    The syntax would be,
    myvariable.bit0(myByteIndex * 8 + myBitIndex)

    where myBitIndex goes from 0 to 7 to specify a bit within byte number myByteIndex.
  • TOMCHTOMCH Posts: 3
    edited 2014-04-30 20:08
    Thanks

    I think I understand, but it somehow does not want to work when put into an IF statement... it wants a label after the THEN, but when I do, it does not want to recognize the ELSE statement.
    Also, can I use the HIGH statement with the variable like I did?
    Here is my program:
    FOR COUNTER1 = 0 TO 9
    FOR COUNTER2 = 0 TO 6
    IF NUMBER.BIT0(COUNTER1*8+COUNTER2)=1 THEN
    HIGH (9+COUNTER2)
    ELSE
    LOW (9+COUNTER2)
    NEXT
    WAIT 1000
    NEXT
  • Mike GreenMike Green Posts: 23,101
    edited 2014-04-30 20:50
    You have to specify that you're using PBASIC 2.5. If you don't have a ' {$PBASIC 2.5} directive, the compiler won't accept ELSE or ENDIF and expects a label after THEN. Please look at the Stamp Reference Manual for details and examples.

    The HIGH and LOW statements will accept any otherwise valid expression for the I/O pin number.
Sign In or Register to comment.