[Tutor] Python GPIO Code Help Needed

Alan Gauld alan.gauld at btinternet.com
Mon Oct 27 01:16:09 CET 2014


On 26/10/14 22:42, Bill Bright wrote:
> Thanks to all for the help. The code below seems to work. My question is
> why does oldstates need to be multiplied by 32?

Its creating 32 instances of True. So you can cater for up to 32 
different switch positions.

> will only read 32 switch changes? That would be a problem.

No it can manage up to 32 different switches. Each switch can toggle 
between True/False as often as it likes but oldstates will store the 
previous state for each switch.

> oldstates=[True]*32
>
> def changed(channel):
>          newstate = GPIO.input(channel)
>          change = oldstates[channel] and not newstate
>          oldstates[channel] = newstate

Here is where it compares the old and new state of the given switch/channel

> while True:
>          if changed(23):
>                  os.system('mpg321 -g 95 a.mp3')
>          if changed(24):
>                  os.system('mpg321 -g 95 b.mp3')
>          if changed(25):
>                  os.system('mpg321 -g 95 c.mp3')
>          sleep(1.1);

HTH

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list