0

So, I am programming in python on my pi. Through a cwiid library I am connecting my wii controller. This works fine.

But, after my wii controller connects trough bluetooth, suddenly the leds won't work. The same line of code works before connecting, but doesn't work anymore after connecting. How is this possible?

My complete code:

if __name__ == '__main__':
from sys import path

path.append("..")


import RPi.GPIO as GPIO  
from time import sleep  
import os
import cwiid
from MotorPwm2 import MotorPwm
from GPIOFuckUp import GPIOFuckUp
from Basis import Basis

GPIOFuckUp()

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

motor = MotorPwm([10, 9, 8, 7])
basis = Basis()
startbool = False

running = True

left_light = 21
right_light = 20
left_siren = 16
right_siren = 26

GPIO.setup(left_light, GPIO.OUT)
GPIO.setup(right_light, GPIO.OUT)
GPIO.setup(left_siren, GPIO.OUT)
GPIO.setup(right_siren, GPIO.OUT)

left_pin = GPIO.PWM(left_light, 1000)
right_pin = GPIO.PWM(right_light, 1000)
left_pin_s = GPIO.PWM(left_siren, 1000)
right_pin_s = GPIO.PWM(right_siren, 1000)

left_pin.start(100)
right_pin.start(100)
left_pin_s.start(0)
right_pin_s.start(0)

# Code above works. Leds turn on, with no problems.

def turn_on_left():
    dim_left(100)

def turn_on_right():
    dim_right(100)

def turn_on_lefts():
    dim_lefts(100)

def turn_on_rights():
    dim_rights(100)

def turn_on():
    print 'aan'
    dim_left(100)
    dim_right(100)

def turn_off_lefts():
    dim_lefts(0)

def turn_off_rights():
    dim_rights(0)

def turn_off_left():
    dim_left(0)

def turn_off_right():
    dim_right(0)

def turn_off():
    dim_left(0)
    dim_right(0)

def dim(percentage):
    dim_left(percentage)
    dim_right(percentage)

def dim_left(percentage):
    left_pin.ChangeDutyCycle(percentage)
    print 'left'

def dim_right(percentage):
    right_pin.ChangeDutyCycle(percentage)
    print 'right'

def dim_lefts(percentage):
    left_pin_s.ChangeDutyCycle(percentage)

def dim_rights(percentage):
    right_pin_s.ChangeDutyCycle(percentage)

def siren_loop():
    while running:
        dim_lefts(100)
        dim_rights(100)
        sleep(0.05)

        turn_off_lefts()
        turn_off_rights()
        sleep(0.05)

        dim_rights(100)
        sleep(0.05)

        turn_on_rights()
        sleep(0.05)

        dim_lefts(100)
        sleep(0.05)

        dim_lefts(0)
        sleep(0.05)

# Def things above also work. Copied the code in another file just to try 
 the leds, and that also worked. 


button_delay = 0.1

print 'Press 1 + 2 on your Wii Remote now ...'

# Here the controller is connecting. After it is connected the leds turn of
# Before the following print lines are printed.

# Connect to the Wii Remote. If it times out
# then quit.
try:
    wii = cwiid.Wiimote()
    #GPIO.output(PIN_LED, 0)

except RuntimeError:
    print "Error opening wiimote connection"
    #GPIO.output(PIN_LED, 0)
    # Uncomment this line to shutdown the Pi if pairing fails
    os.system("sudo reboot")
    quit()

print 'Wii Remote connected...\n'
print 'Press some buttons!\n'
print 'Press PLUS and MINUS together to disconnect and quit.\n'


wii.rpt_mode = cwiid.RPT_BTN


while True:
    buttons = wii.state['buttons']

    # If Plus and Minus buttons pressed
    # together then rumble and quit.
    if (buttons - cwiid.BTN_PLUS - cwiid.BTN_MINUS == 0):
        print '\nClosing connection ...'
        wii.rumble = 1
        #GPIO.output(PIN_LED, 1)
        sleep(1)
        wii.rumble = 0
        #GPIO.output(PIN_LED, 0)
        os.system("sudo reboot")
        exit(wii)


   # Button presses below work and print the correct lines. Just no leds :(

    if (buttons & cwiid.BTN_LEFT):
        print 'Left pressed'
        motor.left_forward(50)
        sleep(button_delay)

    elif (buttons & cwiid.BTN_RIGHT):
        print 'Right pressed'
        motor.right_forward(50)
        sleep(button_delay)

    elif (buttons & cwiid.BTN_UP):
        print 'Up pressed'
        motor.forward(100)
        sleep(button_delay)

    elif (buttons & cwiid.BTN_DOWN):
        print 'Down pressed'
        motor.backward(100)
        sleep(button_delay)

    elif (buttons & cwiid.BTN_A):
        print 'A'
        if startbool == False:
            print 'start aan'
            startbool = True
            sleep(0.5)
        else:
            print 'start uit'
            startbool = False
            sleep(0.5)

    elif (buttons & cwiid.BTN_1):
        if running:
            turn_on()
            sleep(5)
        print '1'
        print 'Licht 5 sec aan'

    elif (buttons & cwiid.BTN_2):
        print '2'
        print 'sirene 5 sec aan'
        siren_loop()
        sleep(5)


    else:
       GPIO.cleanup()

So, for example, the start values turn the leds on when I run the program. This happens, the leds light up. But then, after the wii remote connects, they turn off and are impossible to turn on again.

Noralie
  • 139
  • 9
  • Could you please add some comments to your lines of code to make it easier to understand which section is broken? Thanks, – Harvey Fletcher Feb 02 '17 at 13:55
  • Updated it with some comments. – Noralie Feb 02 '17 at 14:00
  • Could it be that the initial connection is failing? I see that you have a runtime exception clause which reboots the pi. Temporarily, you could change this to a print statement to save time. Just have it print "Connection Failed" instead of possibly rebooting. If you see this text, it means your raspberry pi is rebooting itself and that's why the program stops. Please let me know what happens, thanks. – Harvey Fletcher Feb 02 '17 at 14:06
  • There is already a print statement there (print "Error opening wiimote connection") and this statement doesn't come up. The pi doesn't reboot, the controller connects normally and the buttons work. And like I said before: If I try to turn the lights on after that part, they just don't react. If I try to turn them on with the exact same code before that part, it works perfectly. – Noralie Feb 02 '17 at 14:08
  • Do the motors work when the buttons are pressed? I've assumed they don't but please correct me if i'm wrong. – Harvey Fletcher Feb 02 '17 at 14:14
  • They don't. In another file I use the same code to use the motors, and it does work there. – Noralie Feb 02 '17 at 14:16
  • I can't see any obvious cause to this. Although in this file I can't see an import for motor (since you've used motor.left_forward). I am sorry I can't be of more help in this question. I hope it gets answered shortly. – Harvey Fletcher Feb 02 '17 at 14:22
  • The motor is not the problem (yet) because I wanted to test the leds first (easier to do on a table, wouldn't want my robot to drive away xD) The import is MotorPwm2. – Noralie Feb 02 '17 at 14:24
  • I'm just finding this unusual since you say the exact same code works when run elsewhere. You've only called turn_on() once in your code, by the if running() at the end. I can't see it any of your other functions where you want the lights to turn on. But this is probably my misunderstanding of your code. – Harvey Fletcher Feb 02 '17 at 14:29
  • The exact same code for the lights works elsewere, without the wii controller connected. And that is true. When I press the button, it first prints the button statement. Then it prints what turn_on() says, then it prints what left and right dim says. In this whole proces, the lights don't turn on. So it goes to the different parts in the code, but the lights stay off. I pasted turn_on() in the beginning of the loop, as well as other statements, but they didn't work there either. – Noralie Feb 02 '17 at 14:31
  • I have no further suggestions as to why this doesn't work. – Harvey Fletcher Feb 02 '17 at 14:33

1 Answers1

0

After trying for another few hours, I finally found the problem.

The last line said:

else:
    GPIO.cleanup()

Which resetted all the pins, hence they were not working. Deleting the else made everything work again.

Now I know the problem it seems so simple xD

Noralie
  • 139
  • 9