尝试使用pyfirmata库使用交互式滑块打开LED。我只是想使用滑块将其关闭或打开。这是我的代码(我不知道我做错了什么):
import pyfirmata
import time
from ipywidgets import widgets, interact
import ipywidgets as widgets
from IPython.display import display
pin=9
port='com5'
board = pyfirmata.Arduino(port)
def ledON():
board.digital[pin].write(1)
def ledOFF():
board.digital[pin].write(0)
def runit(LED=ledwidget.result):
if LED == 1:
ledON()
elif LED == 0:
ledOFF()
return(LED)
slider = widgets.IntSlider(value=0,min=0,max=1,step=1)
ledwidget = widgets.interactive (runit,a=slider)
display(ledwidget)
board.exit()
这是帮助调试的堆栈错误跟踪,但由于我无法将所有堆栈发布到此处,因此我不得不削减大部分内容:
SerialException Traceback (most recent call last) <ipython-input-115-6a0aba5925b9> in runit(LED)
19 ledON()
20 elif LED == 0:
---> 21 ledOFF()
22 return(LED)
23
<ipython-input-115-6a0aba5925b9> in ledOFF()
13
14 def ledOFF():
---> 15 board.digital[pin].write(0)
16
17 def runit(LED=ledwidget.result):
C:\Users\trinh\Anaconda3\lib\site-packages\pyfirmata\pyfirmata.py in write(self, value)
531 if self.mode is OUTPUT:
532 if self.port:
--> 533 self.port.write()
534 else:
535 msg = bytearray([DIGITAL_MESSAGE, self.pin_number, value])
C:\Users\trinh\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py in run_code(self, code_obj, result) 2879 self.hooks.pre_run_code_hook() 2880
#rprint('Running code', repr(code_obj)) # dbg
-> 2881 exec(code_obj, self.user_global_ns, self.user_ns) 2882 finally: 2883 # Reset our crash handler in place
C:\Users\trinh\Anaconda3\lib\site-packages\serial\serialwin32.py in write(self, data)
302 """Output the given byte string over the serial port."""
303 if not self.is_open:
--> 304 raise portNotOpenError
305 #~ if not isinstance(data, (bytes, bytearray)):
306 #~ raise TypeError('expected %s or bytearray, got %s' % (bytes, type(data)))
SerialException: Attempting to use a port that is not open