🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Variable Corruption

    UIFlow
    1
    1
    252
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J
      joefox97
      last edited by

      Hello all, fairly new to M5, but have done embedded stuff for a while. I built this using UIFlow2, but also pasting the code below for those who prefer to see it in code.

      Screenshot 2025-01-30 at 12.32.17 AM.png

      I have 3 RFID readers hanging off a PAHub. I'm getting the RFID UIDs off the readers and storing them in a variable in the Loop. All good so far. I write them to variables RFIDZERO, RFIDONE and RFIDTWO (I was using other names, but thought it might have been getting confused with the other variable names).

      Inside the do loops, RFIDZERO and RFIDONE get set to their correct values from the RFID tags. Immediately outside the do loop, I print them again and now, RFIDONE is equal to RFIDZERO's value. No other assignment operators or operation has taken place. WTF?

      import M5
      from M5 import *
      from module import Module4In8Out
      from hardware import I2C
      from hardware import Pin
      from unit import PAHUBUnit
      from unit import RFIDUnit
      
      
      
      module_4in8out_0 = None
      i2c0 = None
      file_0 = None
      pahub_0 = None
      rfid_0 = None
      rfid_1 = None
      rfid_2 = None
      
      
      RFIDZERO = None
      WritingRFIDs = None
      RFIDONE = None
      RFIDTWO = None
      
      
      def btnA_wasClicked_event(state):
        global module_4in8out_0, i2c0, file_0, pahub_0, rfid_0, rfid_1, rfid_2, RFIDZERO, WritingRFIDs, RFIDONE, RFIDTWO
        WritingRFIDs = True
      
      
      def setup():
        global module_4in8out_0, i2c0, file_0, pahub_0, rfid_0, rfid_1, rfid_2, RFIDZERO, WritingRFIDs, RFIDONE, RFIDTWO
      
        M5.begin()
        Widgets.fillScreen(0x222222)
      
        BtnA.setCallback(type=BtnA.CB_TYPE.WAS_CLICKED, cb=btnA_wasClicked_event)
      
        module_4in8out_0 = Module4In8Out(address=0x45)
        i2c0 = I2C(0, scl=Pin(33), sda=Pin(32), freq=100000)
        pahub_0 = PAHUBUnit(i2c=i2c0, channel=0)
        rfid_2 = RFIDUnit(PAHUBUnit(i2c=i2c0, channel=2))
        rfid_1 = RFIDUnit(PAHUBUnit(i2c=i2c0, channel=1))
        rfid_0 = RFIDUnit(PAHUBUnit(i2c=i2c0, channel=0))
        WritingRFIDs = False
      
      
      def loop():
        global module_4in8out_0, i2c0, file_0, pahub_0, rfid_0, rfid_1, rfid_2, RFIDZERO, WritingRFIDs, RFIDONE, RFIDTWO
        M5.update()
        if not WritingRFIDs:
          if rfid_0.is_new_card_present():
            RFIDZERO = rfid_0.read_card_uid()
            print((str('RFID_0: ') + str(RFIDZERO)))
            rfid_0.close()
          if rfid_1.is_new_card_present():
            RFIDONE = rfid_1.read_card_uid()
            print((str('RFID_1: ') + str(RFIDONE)))
            rfid_1.close()
          if rfid_2.is_new_card_present():
            RFIDTWO = rfid_2.read_card_uid()
            print((str('RFID_2: ') + str(RFIDTWO)))
            rfid_2.close()
          print((str('RFID_0: ') + str(RFIDZERO)))
          print((str('RFID_1: ') + str(RFIDONE)))
          print((str('RFID_2: ') + str(RFIDTWO)))
        else:
          file_0 = open('/flash/rfidtags.txt', 'w')
          file_0.write(RFIDZERO)
          file_0.write('\r\n')
          file_0.write(RFIDONE)
          file_0.close()
          WritingRFIDs = False
          print('Wrote the file')
      
      
      if __name__ == '__main__':
        try:
          setup()
          while True:
            loop()
        except (Exception, KeyboardInterrupt) as e:
          try:
            from utility import print_error_msg
            print_error_msg(e)
          except ImportError:
            print("please update to latest firmware")
      
      1 Reply Last reply Reply Quote 0
      • First post
        Last post