I am able to perform an analog read without problems, but digital reads return None.
My code is below. If anyone can tell me what I am doing wrong I would appreciate it.
Thanks.
# -*- coding: utf-8 -*-
import pyfirmata, pyfirmata.util
import time
board = pyfirmata.Arduino('/dev/ttyACM0', baudrate=57600)
iterator = pyfirmata.util.Iterator(board)
iterator.start()
button = board.get_pin('d:12:i')
button.enable_reporting()
# delay for a second
time.sleep(1)
for i in range(10):
value = button.read()
print value
time.sleep(1)
So I have made progress using the code below. If the button switch is open when the program starts, it returns None until the button is closed. It then reports a true or false value correctly after the initial switch closure. If the button is closed when the program starts, it returns a true and I never see the None again. Just plain weird and totally non-intuitive. It also adds the extra burden for my application to test for None since I need to report data back even if the user has not actuated the switch. Makes you wonder why some simple documentation is not provided for this package. I hope this posting will save others some grief.
# -*- coding: utf-8 -*-
import pyfirmata, pyfirmata.util
import time
board = pyfirmata.Arduino('/dev/ttyACM0', baudrate=57600)
iterator = pyfirmata.util.Iterator(board)
iterator.start()
time.sleep(1)
button = board.get_pin('d:13:i')
time.sleep(1)
button.enable_reporting()
# delay for a second
time.sleep(1)
for i in range(100):
print "Button state: %s" % button.read()
# The return values are: True False, and None
if str(button.read()) == 'True':
print "Button pressed"
elif str(button.read()) == 'False':
print "Button not pressed"
else:
print "Button was never pressed"
board.pass_time(0.5)
board.exit()
Where are you telling the firmata code on the Arduino that the pins are input pins? The sketch on the Arduino figures that out when you read from the pin, but it should not have to.
Perhaps that explains the weird behavior you are experiencing. Use the capabilities correctly, and it is not necessary to document the abnormalities you'll experience from using it incorrectly.
In any case, Firmata is not supported on the Arduino. It runs; it sometimes works, but it is not actively supported. There has never been a post here where anyone says "Hey, I'm here from Firmata, and we're working on that". Or anything even remotely like that.
PaulS,
I think you are confusing Firmata for Arduino with pyfirmata. Pyfirmata is a host based library used to communicate with Arduino Standardfirmata. The problems I encountered were not due to the Arduino libraries or examples, nor did I ever say that. The problems are within the pyfirmata host library.
I simply wrote my initial post hoping that someone here would have gotten pyfirmata to work with digital inputs and could point out my problems.
As to your comment about not using the interface correctly - you did not ask, but only assumed that I never tried using "button.mode = pyfirmata.INPUT" to see if that helped. I did try it and there was absolutely no difference in behavior. So I am using the interface correctly.
Please read people's posts carefully before turning the flame on.
Please read people's posts carefully before turning the flame on.
I read your post carefully. That is why my first question was about what was loaded on the Arduino. There are several firmata sketches for the Arduino. A given firmata-related sketch on another platform needs to be communicating with the correct firmata sketch on the Arduino.
The problems are within the pyfirmata host library.
I simply wrote my initial post hoping that someone here would have gotten pyfirmata to work with digital inputs and could point out my problems.
If you have the servo firmata sketch on the Arduino, then I'd expect to see the same issues - digital pints not working as you expected. Hence my questions.
As to your comment about not using the interface correctly - you did not ask, but only assumed that I never tried using "button.mode = pyfirmata.INPUT" to see if that helped.
Guilty as charged. For that, I apologize.
I did try it and there was absolutely no difference in behavior. So I am using the interface correctly.
The only thing missing from this thread, until now, was a statement that you had indeed try setting the pins to INPUT.