Shop OBEX P1 Docs P2 Docs Learn Events
Changing a global variable from inside a method? — Parallax Forums

Changing a global variable from inside a method?

bob_g4bbybob_g4bby Posts: 553
edited 2026-02-28 22:49 in PASM2/Spin2 (P2)

I want to add a method to my rotary encoder library for the 8encoder that allows the programmer to link a knob to a variable, so that turning the knob changes that variable.

Say I have a variable and a pointer:-
long myvar
^long myptr

This would set myptr to point to myvar - yes?
[myptr] := @myvar

I create a method :-
pub mymethod(knob#, ptr) | delta
delta := read_knob_change(knob#)
... some code ...

I use the method:-
mymethod(2, myptr)

What would 'some code' have to include to modify the value held in myvar, please?

Couldn't find any clues in the Spin2 manual and have tried all sorts, cheers, bob

Comments

  • JonnyMacJonnyMac Posts: 9,698
    edited 2026-03-01 00:29

    No matter what, you still have to access the encoder hardware in a method to assign your target variable(s). You get nothing using a pointer in this case.

    Where it might be useful is reading all with one method to any arbitrary array.

    pub get_encoders(p_data) | ch
    
      repeat ch from 0 to 7
        long[p_data][ch] := encoder_read(ch)
    

    This code works in the P1 and in the P2. The pointer p_data is the address of the first element of the array.

Sign In or Register to comment.