Just beginning to work with python and piface. I am having trouble reading the state of the input switch using any method. The state doesn't seem to change when trying to read the switch, however the emulator shows that the switch is being pressed.
The example program that comes with pifacedigitio for reading the switch states doesn't seem to work either.
Any ideas?
-
- Posts: 52
- Joined: Fri Sep 27, 2013 11:40 am
- Location: Columbus, Ohio
PiFace Inputs
Electronmage
--------------------
I like Pi!
--------------------
I like Pi!
-
- Posts: 52
- Joined: Fri Sep 27, 2013 11:40 am
- Location: Columbus, Ohio
Re: PiFace Inputs
Update:
I have gotten the inputs to work using the following, but only in the Python3 main window. As soon as I try running it in a module, it doesn't work
>>>import pifacedigitalio
>>>p = pifacedigitalio.PiFaceDigital()
>>>p.input_port.value
and pressing the various input switches (S1 through S4), and the decimal equivalent of the binary reads out in the Python3 window.
------------------------
import pifacedigitalio
from time import sleep
p=pifacedigitalio.PiFaceDigital()
portval=p.input_port.value
while True:
sleep(1)
print ("input value:", portval)
---------------------
Any suggestions as to why this isn't working when I try to use the same lines in a module (input.py).
I have gotten the inputs to work using the following, but only in the Python3 main window. As soon as I try running it in a module, it doesn't work
>>>import pifacedigitalio
>>>p = pifacedigitalio.PiFaceDigital()
>>>p.input_port.value
and pressing the various input switches (S1 through S4), and the decimal equivalent of the binary reads out in the Python3 window.
------------------------
import pifacedigitalio
from time import sleep
p=pifacedigitalio.PiFaceDigital()
portval=p.input_port.value
while True:
sleep(1)
print ("input value:", portval)
---------------------
Any suggestions as to why this isn't working when I try to use the same lines in a module (input.py).
Electronmage
--------------------
I like Pi!
--------------------
I like Pi!
Re: PiFace Inputs
Looks as if you only read the input once, so you'll get the same answer every time
You need it every time you start the loop.
gr.
Dirk.
You need it every time you start the loop.
Code: Select all
import pifacedigitalio
from time import sleep
p=pifacedigitalio.PiFaceDigital()
while True:
portval=p.input_port.value
print ("input value:", portval)
sleep(1)
Dirk.
-
- Posts: 52
- Joined: Fri Sep 27, 2013 11:40 am
- Location: Columbus, Ohio
Re: PiFace Inputs
Thanks. I will try this when I get home.
One more extra bonus question.
Is there a difference between
import pifacedigitalio as p
p.input_port.value
and
import pifacedigitalio
p = pifacedigitalio.PiFaceDigital()
p.input_port.value
One more extra bonus question.
Is there a difference between
import pifacedigitalio as p
p.input_port.value
and
import pifacedigitalio
p = pifacedigitalio.PiFaceDigital()
p.input_port.value
Electronmage
--------------------
I like Pi!
--------------------
I like Pi!
- DougieLawson
- Posts: 40579
- Joined: Sun Jun 16, 2013 11:19 pm
- Location: A small cave in deepest darkest Basingstoke, UK
- Contact: Website Twitter
Re: PiFace Inputs
The second form needs 15 more characters (typed accurately) every time you call a PiFace function or use a public value in the PiFace object oriented class. When your code expands from three lines to 300 lines you'll soon know why the first form is preferred.electronmage wrote:andCode: Select all
import pifacedigitalio as p p.input_port.value
Code: Select all
import pifacedigitalio p = pifacedigitalio.PiFaceDigital() p.input_port.value
Any language using left-hand whitespace for syntax is ridiculous
Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.
Any requirement to use a crystal ball or mind reading will result in me ignoring your question.
Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.
Any requirement to use a crystal ball or mind reading will result in me ignoring your question.
Re: PiFace Inputs
AIUI with 'import as' you just create an alias and it is essentially a shorter way to do the same as the second bit of code.
Though I have been known to be wrong occasionally
Gr.
Dirk.
Though I have been known to be wrong occasionally

Gr.
Dirk.
-
- Posts: 52
- Joined: Fri Sep 27, 2013 11:40 am
- Location: Columbus, Ohio
Re: PiFace Inputs
Does the pifacedigitalio work for both Python and Python3, in all (most) cases?
Electronmage
--------------------
I like Pi!
--------------------
I like Pi!
Re: PiFace Inputs
According to https://github.com/piface/pifacedigitalio it does, but you have to install the python3 version of the softwareelectronmage wrote:Does the pifacedigitalio work for both Python and Python3, in all (most) cases?
Code: Select all
sudo apt-get install python3-pifacedigitalio
Dirk.
-
- Posts: 52
- Joined: Fri Sep 27, 2013 11:40 am
- Location: Columbus, Ohio
Re: PiFace Inputs
Silly question:
What does the
if ___name___ == "___main___":
signify?
Not so much the if statement (that is somewhat obvious), but the underscores.
What does the
if ___name___ == "___main___":
signify?
Not so much the if statement (that is somewhat obvious), but the underscores.
Electronmage
--------------------
I like Pi!
--------------------
I like Pi!
Re: PiFace Inputs
This discussion on "stackoverflow" will help.
http://stackoverflow.com/questions/4191 ... me-main-do
If you have any questions about Python, stackoverflow is a good place to look for an answer.
However, it is possibly better to use this forum for specific Raspberry Pi related Python questions, for example RPi GPIO, both for receiving an answer yourself and at the same time informing other RPi users.
http://stackoverflow.com/questions/4191 ... me-main-do
If you have any questions about Python, stackoverflow is a good place to look for an answer.
However, it is possibly better to use this forum for specific Raspberry Pi related Python questions, for example RPi GPIO, both for receiving an answer yourself and at the same time informing other RPi users.
-
- Posts: 52
- Joined: Fri Sep 27, 2013 11:40 am
- Location: Columbus, Ohio
Re: PiFace Inputs
DirkS,
Your response about moving the button equation was spot on. It works just fine now.
Your response about moving the button equation was spot on. It works just fine now.
Electronmage
--------------------
I like Pi!
--------------------
I like Pi!