Every time I press a button on the wiimote controlling my robot (Rover 5 with PiFace), the pi restarts. My code is as follows:
import cwiid
import time
import pifacedigitalio as pfio
pfio.init()
button_delay = 0.1
print 'Press 1 + 2 on your Wii Remote now ...'
time.sleep(1)
# Try to connect to the Wiimote & quit if not found
try:
wii=cwiid.Wiimote()
except RuntimeError:
print "Can't connect to Wiimote"
quit()
print 'Wiimote connected'
wii.rpt_mode = cwiid.RPT_BTN
while True:
buttons = wii.state['buttons']
if (buttons & cwiid.BTN_UP):
#Forwards
time.sleep(button_delay)
pfio.digital_write(2, 1)
pfio.digital_write(3, 1)
elif (buttons & cwiid.BTN_DOWN):
#Backwards
time.sleep(button_delay)
pfio.digital_write(4, 1)
pfio.digital_write(5, 1)
elif (buttons & cwiid.BTN_LEFT):
#Left
time.sleep(button_delay)
pfio.digital_write(2, 1)
pfio.digital_write(5, 1)
elif(buttons & cwiid.BTN_RIGHT):
#Right
time.sleep(button_delay)
pfio.digital_write(3, 1)
pfio.digital_write(4, 1)
else:
pfio.digital_write(2, 0)
pfio.digital_write(3, 0)
pfio.digital_write(4, 0)
pfio.digital_write(5, 0)
#press button A to stop all motors
if (buttons & cwiid.BTN_A):
time.sleep(button_delay)
for i in pins:
pfio.digital_write(2, 0)
pfio.digital_write(3, 0)
pfio.digital_write(4, 0)
pfio.digital_write(5, 0)