0

Here is my python code that I am using. I am trying to use a wii remote to trigger a buzzer. I thought this would be an interesting use for my old wii remote. The code that interacts with the buzzer works fine because I used an example script to test it. However, when I try and run it I keep getting this one error (see bottom). I am new to python and would appreciate any help.

   import cwiid
   from gpiozero import LED
   import time
   import os

   Buzzer1 = LED(17)

   Buzzer2 = LED(27)

   def ConnectRemote():

        os.system("rfkill unblock bluetooth")

        print 'Press 1+2 On The Remote...'

        global wm

        wm = wiid.Wiimote()

        print 'Connection Established!\n'
        wm.led = 1
        wm.rumble = 1
        time.sleep(0.25)
        wm.rumble = 0
        time.sleep(0.5)
        wm.rpt_mode = cwiid.RPT_BTN

   def TryToConnect():

        while True:

            ConnectRemote()
            break

   while True:

        buttons = wm.state['buttons']

        #shutdown function using plus and minus buttons
        if (buttons - cwiid.BTN_PLUS - cwiid.BTN_MINUS == 0):
                print '\nClosing Connection To Wiimote'
                wm.rumble = 1
                time.sleep(0.25)
                wm.rumble = 0
                os.system("rfkill block bluetooth")
                TryToConnect()

    if (buttons & cwiid.BTN_A):
            print 'Buzzer On'
            Buzzer1.on()
            Buzzer2.on()
    else:
            Buzzer1.off()
            Buzzer2.off()

and yet I keep getting an error of

    Traceback (most recent call last):
    File "WiimoteLEDs.py", line 36, in <module>
    buttons = wm.state['buttons']
    NameError: global name 'wm' is not defined

Can anyone help? Thanks in advance

1 Answers1

0

I think you should initialized variable wm before using this variable in function. This bug is related to 'Global name not defined'

AJackTi
  • 63
  • 2
  • 7