Variable in another object
Hello
Can you tel me how to call a variable in another object?
I have declare a variable in VAR long keycode and I have to call this variable " keycode" in another object to print the value in a LCD screen and I don't make it.
Can you tel me how to call a variable in another object?
I have declare a variable in VAR long keycode and I have to call this variable " keycode" in another object to print the value in a LCD screen and I don't make it.

Comments
Andy
:
The first method is easy with single-element variables because you can substitute the object's method name where you want that variable. Again, I tend to use the second as this provides greater flexibility IMHO.
@Vega256
Im not sure about that.. but maybe the following code is going to answer your question.
I try the this code and I only get this symbol " ~ " on the LCD were it suppose to be the " keycode " any idea why ?
There is my main OBJ
{{ DS1302_LCD.spin}} CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 TX_PIN = 3 BAUD = 19_200 OBJ SN : "Simple_Numbers" rtc : "DS1302_full" LCD : "FullDuplexSerial" IR : "ir_reader_1" VAR byte hour, minute, second, day, month, year, dow byte cmd, out byte data[12] long Stack[50] long Stack1[50] PUB Main LCD.start(TX_PIN, TX_PIN, 00, 19_200) IR.Start_IR rtc.init( 0, 1, 2 ) LCD.str(string(12)) LCD.str(string(17)) repeat LCD.str(string(22)) rtc.readTime( @hour, @minute, @second ) 'read time from DS1302 LCD.str( SN.decx(hour,2) ) LCD.str( string(":")) LCD.str( SN.decx(minute,2)) LCD.str( string(":") ) LCD.str( SN.decx(second,2)) waitcnt( clkfreq + cnt ) LCD.str(string(148)) [COLOR=#b22222] LCD.str(IR.get_Keycode) [/COLOR] LCD.str(string(13)) ALARME PUB ALARME | M,S M := 05 S := 20 if minute == M and second < S LCD.str(string(222)) elseif LCD.str(string(232))And the LCD OBJ
CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 _irrpin = 4 'ir receiver module on this pin _device = 0 'accept any device code _dlyrpt = 6 'delay before acception code as repeat VAR word Direction word vi long keycode long Stack2[100] OBJ rcvir : "ir_reader_sony" LCD : "FullDuplexSerial.spin" PUB Start_IR cognew(Main, @Stack2) PUB Main rcvir.init(_irrpin,_device,_dlyrpt,true) 'startup repeat keycode := rcvir.fifo_get 'try a get from fifo if keycode == -1 'empty try again next out PUB out dira [16..23]~~ outa [16..23]~ case keycode 0: !outa[23] 1: !outa[22] 2: !outa[21] 3: !outa[20] 4: !outa[19] 5: !outa[18] 6: !outa[17] 7: !outa[16] 14: Speed_UP 15: Speed_DOWN 16: Flash_G← 17: Flash_D→ other :Flash PUB Flash dira [16..23]~~ outa [16..23]~ repeat 5 !outa [16..23] waitcnt ( _xinfreq + cnt ) !outa [16..23] waitcnt ( _xinfreq + cnt ) PUB Speed_UP vi <#= 9 vi ++ PUB Speed_DOWN vi #>= 2 vi -- PUB Flash_D→ dira [16..20]~~ outa [16..20]~ repeat 2 Direction := 24 repeat until Direction == 15 !outa [Direction] waitcnt ( _xinfreq/vi + cnt ) !outa [Direction] Direction -- PUB Flash_G← dira [16..20]~~ outa [16..20]~ repeat 2 Direction := 15 repeat until Direction == 24 !outa [Direction] waitcnt ( _xinfreq/vi + cnt ) !outa [Direction] Direction ++ [COLOR=#b22222]PUB get_keycode[/COLOR] [COLOR=#b22222] return keycode[/COLOR] DAT title byte "Push ir button for codes",13,13,0You can not print the keycode with the str methode, it's not a string. If you want the code as decimal number use:
LCD.dec(IR.get_keycode)
But the other problem is that your keycode variable is continuosly polled in the Main methode, so you will see mostly the value -1. You can poll into a temporary variable and update the keycode variable only when a new vaild code is received. So you see always the last vaild number:
PUB Main : tmp rcvir.init(_irrpin,_device,_dlyrpt,true) 'startup repeat tmp := rcvir.fifo_get 'try a get from fifo if tmp > -1 'keycode available keycode := tmp outAndy
CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 _irrpin = 4 'ir receiver module on this pin _device = 0 'accept any device code _dlyrpt = 6 'delay before acception code as repeat TX_PIN = 3 BAUD = 19_200 VAR word Direction word vi long keycode long Stack2[100] OBJ rcvir : "ir_reader_sony" LCD : "FullDuplexSerial.spin" PUB Start_IR LCD.start(TX_PIN, TX_PIN, 00, 19_200) 'start the tv terminal rcvir.init(_irrpin,_device,_dlyrpt,true) 'startup repeat [COLOR=#a52a2a] keycode := rcvir.fifo_get 'try a get from fifo[/COLOR] [COLOR=#a52a2a] if keycode == -1 'empty try again[/COLOR] [COLOR=#a52a2a] next[/COLOR] else [COLOR=#a52a2a] LCD.str(string(17,22))[/COLOR] [COLOR=#a52a2a] LCD.str(string(12))[/COLOR] [COLOR=#a52a2a] LCD.dec(keycode) 'show code [/COLOR] out PUB out dira [16..23]~~ outa [16..23]~ case keycode 0: !outa[23] 1: !outa[22] 2: !outa[21] 3: !outa[20] 4: !outa[19] 5: !outa[18] 6: !outa[17] 7: !outa[16] 14: Speed_UP 15: Speed_DOWN 16: Flash_G← 17: Flash_D→ other :Flash PUB Flash dira [16..23]~~ outa [16..23]~ repeat 5 !outa [16..23] waitcnt ( _xinfreq + cnt ) !outa [16..23] waitcnt ( _xinfreq + cnt ) PUB Speed_UP vi <#= 9 vi ++ PUB Speed_DOWN vi #>= 2 vi -- PUB Flash_D→ dira [16..20]~~ outa [16..20]~ repeat 2 Direction := 24 repeat until Direction == 15 !outa [Direction] waitcnt ( _xinfreq/vi + cnt ) !outa [Direction] Direction -- PUB Flash_G← dira [16..20]~~ outa [16..20]~ repeat 2 Direction := 15 repeat until Direction == 24 !outa [Direction] waitcnt ( _xinfreq/vi + cnt ) !outa [Direction] Direction ++ DAT title byte "Push ir button for codes",13,13,0If it is -1 then the NEXT command is executed, and this restarts the repeat loop, so all the code after NEXT is not executed until keycode is no longer -1.
The ELSE in your code has no effect if you have the following LCD.str() command at the same indention level.
Remember also that here you execute the polling and the print in the same cog, so they are sequential, while in the previous code the Main methode is executed in another cog than the get_keycode and the print methodes. So writing to the keycode and reading the current value happens in two parallel cogs totally un-synchronized.
Andy