electronmage
Posts: 52
Joined: Fri Sep 27, 2013 11:40 am
Location: Columbus, Ohio

PiFace Inputs

Sun Oct 12, 2014 1:21 pm

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?
Electronmage

--------------------

I like Pi!

electronmage
Posts: 52
Joined: Fri Sep 27, 2013 11:40 am
Location: Columbus, Ohio

Re: PiFace Inputs

Mon Oct 13, 2014 2:41 pm

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).
Electronmage

--------------------

I like Pi!

DirkS
Posts: 10515
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: PiFace Inputs

Mon Oct 13, 2014 2:53 pm

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.

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)
gr.
Dirk.

electronmage
Posts: 52
Joined: Fri Sep 27, 2013 11:40 am
Location: Columbus, Ohio

Re: PiFace Inputs

Mon Oct 13, 2014 3:08 pm

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
Electronmage

--------------------

I like Pi!

User avatar
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

Mon Oct 13, 2014 3:19 pm

electronmage wrote:

Code: Select all

import pifacedigitalio as p
p.input_port.value
and

Code: Select all

import pifacedigitalio
p = pifacedigitalio.PiFaceDigital()
p.input_port.value
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.
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.

DirkS
Posts: 10515
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: PiFace Inputs

Mon Oct 13, 2014 3:26 pm

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.

electronmage
Posts: 52
Joined: Fri Sep 27, 2013 11:40 am
Location: Columbus, Ohio

Re: PiFace Inputs

Mon Oct 13, 2014 6:07 pm

Does the pifacedigitalio work for both Python and Python3, in all (most) cases?
Electronmage

--------------------

I like Pi!

DirkS
Posts: 10515
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: PiFace Inputs

Mon Oct 13, 2014 10:48 pm

electronmage wrote:Does the pifacedigitalio work for both Python and Python3, in all (most) cases?
According to https://github.com/piface/pifacedigitalio it does, but you have to install the python3 version of the software

Code: Select all

sudo apt-get install python3-pifacedigitalio
Gr.
Dirk.

electronmage
Posts: 52
Joined: Fri Sep 27, 2013 11:40 am
Location: Columbus, Ohio

Re: PiFace Inputs

Tue Oct 14, 2014 10:38 pm

Silly question:

What does the

if ___name___ == "___main___":

signify?

Not so much the if statement (that is somewhat obvious), but the underscores.
Electronmage

--------------------

I like Pi!

billio
Posts: 71
Joined: Thu Dec 15, 2011 8:25 am
Contact: Website

Re: PiFace Inputs

Tue Oct 14, 2014 11:35 pm

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.

electronmage
Posts: 52
Joined: Fri Sep 27, 2013 11:40 am
Location: Columbus, Ohio

Re: PiFace Inputs

Wed Oct 15, 2014 12:16 am

DirkS,

Your response about moving the button equation was spot on. It works just fine now.
Electronmage

--------------------

I like Pi!

Return to “Python”