Shop OBEX P1 Docs P2 Docs Learn Events
Comparing DAT with Variable Values — Parallax Forums

Comparing DAT with Variable Values

coryco2coryco2 Posts: 107
edited 2013-12-26 15:01 in Propeller 1
I'm trying to compare five current values in a DAT table, which has been updated by another method, with their previous values; and call another method if any of the DAT items is not the same value as during the last loop. I know the DataItem values are being changed sometimes, but ChangeChecker (below) when called seems unable to detect this, and never calls SomeOtherMethod. Can anyone please tell me why?
PUB ChangeChecker  |  prevItem0, prevItem1, prevItem2, prevItem3, prevItem4

   IF (prevItem0 <> DataItem[0])  AND  (prevItem1 <> DataItem[1])  AND (prevItem2 <> DataItem[2])  AND (prevItem3 <> DataItem[3])  AND (prevItem4 <> DataItem[4])
     SomeOtherMethod
   prevItem0 := DataItem[0]
   prevItem1 := DataItem[1]
   prevItem2 := DataItem[2]
   prevItem3 := DataItem[3]
   prevItem4 := DataItem[4]


DAT

   DataItem   byte   $11, $22, $33, $44, $55, $66, $77, $88, 0


Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-12-26 14:44
    As you code is now, "SomeOtherMethod" will only be called if all of the first five elements of "DataItem" have been changed. Did you want to take action if any of them had changed? If so you'd want to use "or" instead of "and".

    BTW, you could use "bytemove" to set your one array equal to the other but it's a bit of a trick with local variables since all local variables are longs. It could be done though. It's also possible to set your comparison up in a loop but the way you have it now should get the job done.

    Another issue is your local variables are not preserved from one call to ChangeChecker to another. You need you're previous value array as a global array if you're not remaining in "ChangeChecker" method between comparisons. By having the previous values as global variables you could then use a byte array which would make using 'bytemove" easier.
  • Heater.Heater. Posts: 21,230
    edited 2013-12-26 14:49
    coryco2.

    You have declared all the prevItemX variables as local variables to the ChangeChecker method.
    That means they are created when the method is called and disappear when the method returns.
    So, you "if" comparisons are comparing DataItem[X] against uninitialized values.
    Then you update the prevItemX local variables from DataItem[X] but those values disappear when ChangeChecker returns.

    You need to move the prevItemX variables out to a VAR section to fix that at least.
  • coryco2coryco2 Posts: 107
    edited 2013-12-26 15:01
    Ah, I see. I didn't realize that local variables are not preserved. And you're right about my logical operators too, Duane...or, you're right. ;-)

    Thanks!
Sign In or Register to comment.