0

Trying to turn on an LED using an interactive slider using pyfirmata library. I simply want to turn it off or on using the slider. Here is my code (I do not know what I'm doing wrong):

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()

Here is the stack error trace to help in debugging, however I had to cut most of it since I wasn't able to post all of the stack onto here:

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
Spencer Trinh
  • 743
  • 12
  • 31
  • Are you trying PWM or just normal on-off ? – Ubdus Samad Aug 05 '17 at 03:24
  • And please post the stack trace of your error as well! – Ubdus Samad Aug 05 '17 at 03:25
  • just normal on-off for now – Spencer Trinh Aug 05 '17 at 03:39
  • and what errors are you getting? Please post those too. – Ubdus Samad Aug 05 '17 at 03:39
  • would I just simply copy+paste the stack trace of the error? – Spencer Trinh Aug 05 '17 at 03:39
  • Edit your question, with the stack trace included! – Ubdus Samad Aug 05 '17 at 03:40
  • what port are you using? 8080 – Ubdus Samad Aug 05 '17 at 03:57
  • Sorry for the 8080, I suggest you to try flashing the board with the basic Arduino IDE first and then check whether it's working or not, if it works then copy the port from the IDE and then try to flash the board with it. – Ubdus Samad Aug 05 '17 at 04:00
  • i'm not entirely sure if I understand you. I did try to see if the LED works using a different more simpler code without using `interactive`. It seems the problem may lie in the `interactive` code. I am using port = com5. When I use the Arduino IDE I am also able to turn on/off the LED. – Spencer Trinh Aug 05 '17 at 04:21
  • What port are you using at the IDE? , As far as I remember It's not simply named as com5 rather it's named like devTTy12 or something like that. So if you're using Python to control it, you'll have to give that devTTy thingy rather than just writing com5. I am telling you this because I had the same problem while graphing raw data from my board through python. I was using com2 and it never worked and then I changed it to devTTy thingy and it worked. – Ubdus Samad Aug 05 '17 at 04:28
  • If you're available we may take it to chat! – Ubdus Samad Aug 05 '17 at 04:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/151123/discussion-between-spencer-trinh-and-ubdus-samad). – Spencer Trinh Aug 05 '17 at 04:36

0 Answers0