Getting Python to activate GPIO on certain words.


12 posts
by mac550 » Fri Mar 15, 2013 7:28 pm
Hi, I am a complete rookie when it comes to programming so please bear with me as I'm going to sound and feel really stupid asking this.

I am trying to figure out how to get my Pi to activate a GPIO pin when a user types certain things.
For example: if a user types Hello, the Python code would activate GPIO 7, if a user types Heello, nothing would happen.

I have tried using strings, vars, raw_input(), input() but it either gives me syntax errors or just activates the GPIO as soon as I run the script without any user input.

Below is just one of the things I've tried.
Code: Select all
while 1:
if raw_input:
      raw_input("Hello")
      GPIO.output(7, False)
else:
      GPIO.output(7, True)


Like I said, I am a complete novice with programming but I have been messing about with this one thing for about 6 hours and I still have no idea how to do it.
Could someone kindly help me out and point out what I'm doing wrong.
Thank you very much.
Morgan
Posts: 6
Joined: Fri Mar 15, 2013 7:04 pm
Location: Cornwall, UK
by sej7278 » Fri Mar 15, 2013 8:50 pm
Something like this:

Code: Select all
#!/usr/bin/python

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(7, GPIO.OUT)

keywords = ['hello','good','evening','and','welcome']

while 1:
    typing = raw_input('Enter a word: ')

    if typing in keywords:
        GPIO.output(7, True)
        print "Correct spelling!"
    else:
        GPIO.output(7, False)
        print "Try again."
Posts: 231
Joined: Tue Jan 22, 2013 6:43 pm
by mac550 » Fri Mar 15, 2013 9:51 pm
That is exactly what I'm after. I never knew about the keywords. My script is now working as it should. Thank you very very much for you time and help. :D :D
Posts: 6
Joined: Fri Mar 15, 2013 7:04 pm
Location: Cornwall, UK
by alexeames » Fri Mar 15, 2013 10:17 pm
mac550 wrote:That is exactly what I'm after. I never knew about the keywords. My script is now working as it should. Thank you very very much for you time and help. :D :D


You could change 'keywords' to pretty much anything you want, it's just the name of what's called a 'list variable' - a highly useful python feature. :D
My Pi uses 2 watts - what what? ---- HiRes early production Pi photos RS Front Back | Farnell Front Back
User avatar
Posts: 2018
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
by mac550 » Fri Mar 15, 2013 10:22 pm
Oh right, So I guess in a way, its the same as str and var or is it not?
Posts: 6
Joined: Fri Mar 15, 2013 7:04 pm
Location: Cornwall, UK
by sej7278 » Fri Mar 15, 2013 10:55 pm
mac550 wrote:Oh right, So I guess in a way, its the same as str and var or is it not?


"keywords" is just the name of the array (list in python-speak) you can rename it, its not a function like str()

you could expand it to read your words from a text file into a list using readlines()

glad it works for you anyway, are you going to connect it to an LED or something?
Posts: 231
Joined: Tue Jan 22, 2013 6:43 pm
by mac550 » Sat Mar 16, 2013 12:30 am
I get it now, I think. I have been playing around and I was just wondering, I have got my script such if a user types 'Hello', GPIO 7 will activate, if a user types 'Goodbye', GPIO 7 will deactivate. Is there a way of putting more keyword functions within that loop? for example if the user types 'Hello' turning on GPIO 7, the user can then type 'Go' for GPIO 11 to activate/deactivate (having its own loop). Then user types 'Goodbye' to deactivates everything and turns off the 'Go' command.

The GPIO pins will be connected via a 24K ohm resistor to a relay board.

Thanks again for your help.
Posts: 6
Joined: Fri Mar 15, 2013 7:04 pm
Location: Cornwall, UK
by alexeames » Sat Mar 16, 2013 8:39 am
It should just be a question of adding another block (in the same loop) that starts with if.

Code: Select all
if typing == "Go":
    # code to activate the port you want
    # don't forget to initiate port 11 at the start of the program
    # before you try to set it high or low


You can check for both conditions in the same loop. If 'Go' is not typed, that block is completely ignored until the next iteration of the loop.
My Pi uses 2 watts - what what? ---- HiRes early production Pi photos RS Front Back | Farnell Front Back
User avatar
Posts: 2018
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
by mac550 » Sat Mar 16, 2013 4:02 pm
I have tried adding the 'Go' loop inside the 'Hello' loop but Python ignores the 'Go' loop and just prints the last line of that 'Go' part of the code.
I've tried putting the 'Go' code inside with and without its own loop and I've tried putting it completely outside the 'Hello' block with and without its own loop.

Again, sorry for the lack of knowledge in programming.
Posts: 6
Joined: Fri Mar 15, 2013 7:04 pm
Location: Cornwall, UK
by alexeames » Sat Mar 16, 2013 10:02 pm
I didn't mean for that if ......: block to be in a separate loop. It should go straight after the previous ifs.

You're going to need to post your code if you need any more specific help.
My Pi uses 2 watts - what what? ---- HiRes early production Pi photos RS Front Back | Farnell Front Back
User avatar
Posts: 2018
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
by mac550 » Sat Mar 16, 2013 10:53 pm
I think I have done that. With this code it gets to the last part but then it doesn't go back to to top of the main loop after GPIO.input(15)
GPIO 15 is now a button instead of 'Go'.

Here is the main loop which is the part that's buggy.

Code: Select all
while 1:
   typing = raw_input('Type Hello To Continue: ')   
   if typing in hello:
      sleep(0.5)
      GPIO.output(7, True)
      sleep(0.5)
      GPIO.output(11, False)
      sleep(0.5)
      GPIO.output(12, False)
      sleep(0.5)
         
   else:
      if typing in goodbye:
         print 'Goodbye'
         GPIO.output(7, False)
         GPIO.output(11, True)
         GPIO.output(12, True)
         
   while 1:
      if GPIO.input(15):
         GPIO.output(13, False)
      else:
         GPIO.output(13, True)


I hope this helps.
Posts: 6
Joined: Fri Mar 15, 2013 7:04 pm
Location: Cornwall, UK
by alexeames » Sun Mar 17, 2013 8:44 am
you've got 2 while 1: loops. It will get stuck forever in the second one and not revert to the top of the first.

Better to test for some condition in the second loop - or do you even need it to be a loop?

Also you have else:
then an if:

You could combine those both into a single elif: like this...
Code: Select all
else:
      if typing in goodbye:
         print 'Goodbye'
         GPIO.output(7, False)
         GPIO.output(11, True)
         GPIO.output(12, True)

   elif typing in goodbye:
     print 'Goodbye'
     GPIO.output(7, False)
     GPIO.output(11, True)
     GPIO.output(12, True)
My Pi uses 2 watts - what what? ---- HiRes early production Pi photos RS Front Back | Farnell Front Back
User avatar
Posts: 2018
Joined: Sat Mar 03, 2012 11:57 am
Location: UK