Shop OBEX P1 Docs P2 Docs Learn Events
Shorten VAR Byte Statements? — Parallax Forums

Shorten VAR Byte Statements?

HumanoidoHumanoido Posts: 5,770
edited 2010-09-06 14:32 in Propeller 1
VAR
...
byte state30
byte state31
byte state32
byte state33
byte state34
byte state35
byte state36
byte state37
byte state38
byte state39
...


The program has a lot of these statements.
Is there any way to shorten it, to one statement?
Thanks in advance.

Humanoido

Comments

  • Heater.Heater. Posts: 21,230
    edited 2010-09-02 02:21
    What about using an array:
    byte state[10]
    
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-09-02 09:15
    Or, if you don't want an array, this also works:
    VAR
      ...
      byte state30, state31, state32, state33, state34
      byte state35, state36, state37, state38, state39
      ...
    
    But, based on how you've named your variables, I suspect an array would be a better solution.

    -Phil
  • HumanoidoHumanoido Posts: 5,770
    edited 2010-09-06 11:22
    Or, if you don't want an array, this also works:
    VAR
      ...
      byte state30, state31, state32, state33, state34
      byte state35, state36, state37, state38, state39
      ...
    
    But, based on how you've named your variables, I suspect an array would be a better solution.

    -Phil
    Phil, will that code use considerably less memory over an array?
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-09-06 11:27
    The amount memory used for the VARs will be the same, whether they're individual variables or a large array. But, by using an array and code with indexing, rather than treating each byte as a special case, you could save code space.

    -Phil
  • localrogerlocalroger Posts: 3,452
    edited 2010-09-06 14:32
    I am wondering if you actually need separate bytes for these or if they might not be consts defining different states of your system? You don't actually need to reserve storage for consts, they just become names for the states which you can compare in event code.

    con

    #0 {start at zero}
    st_idle {cons with no val increment by 1}
    st_wakeup
    st_lookaround
    st_verify_id
    st_approach
    st_bite

    I can't really imagine what you would be doing with separate state vars enumerated like that. The whole point of a state machine is to have as few state vars as possible so you can avoid pathological or unanticipated combination states.
Sign In or Register to comment.