Joanne Posted February 26, 2015 Posted February 26, 2015 (edited) I'm having a play with my Pi and some LED's. I have a poster which tells you how to get an LED to blink using the following code: i = 1 While i < 4 : print( 'Cycle: ' + str( i )) print( 'Set Output True - LED ON' ) GPIO.output( 7 , True ) ; sleep( 1 ) print( 'Set Output False - LED OFF' ) GPIO.output( 7 , False ) ; sleep( 1 ) ; i += 1 GPIO.cleanup() This works fine. I have made a sequence based on the above code to resemble a traffic light... Red on: GPIO.output( 7 , True ) ; sleep( 5 ) Amber on: GPIO.output( 9 , True ) ; sleep( 2 ) Red off: GPIO.output( 7 , False ) Amber off: GPIO.output( 9 , False ) Green on: GPIO.output( 11 , True ) ; sleep ( 5 ) etcs... HOWEVER, I get an error on my first line... NameError: name 'GPIO' is not defined. I can run the first code fine, then follow it with my code in the same window and it gives me the error. Much confusion on account of using the same command as the code that works... Remember why I hate coding now!! Can anyone show me the light? Picture of my setup: Edited February 26, 2015 by Joanne
SovietRussia Posted February 26, 2015 Posted February 26, 2015 GPIO needs defining import RPi.GPIO as GPIO
Joanne Posted February 26, 2015 Author Posted February 26, 2015 I've tried that... I had to define it for the flashing LED... and it works for that... but not my code. Dat confusion! Just to clarify, I write the import command in python shell and then I run my code in GNU nano...
GREED Posted February 26, 2015 Posted February 26, 2015 Ah glad to see my inspiration to play with the Pi again has caused confusion! 1
Joanne Posted February 26, 2015 Author Posted February 26, 2015 (edited) Hah!! Damn you!! OK so if I put the import RPi.GPIO as GPIO command into nano I get 'bash: import: command not found' LAME! EDIT: missed vital (possibly) steps of: GPIO.setmode(GPIO.BOARD) GPIO.setup( 7 , GPIO.OUT) Edited February 26, 2015 by Joanne
DMcGuigan Posted February 26, 2015 Posted February 26, 2015 Hah!! Damn you!! OK so if I put the import RPi.GPIO as GPIO command into nano I get 'bash: import: command not found' LAME! Quick google's suggesting the shebang line's missing at the top of the file import: command not found
Joanne Posted February 26, 2015 Author Posted February 26, 2015 (edited) Nope... same... NameError: name 'GPIO' is not defined But I can run the blinking LED code just fine.... where GPIO is mysteriously defined! OK, I gave in and went on the website that the kid is sold by... this is their code... going to try it now... import time import RPi.GPIO as GPIO GPIO.cleanup() GPIO.setmode(GPIO.BOARD) GPIO.setup(11,GPIO.OUT) GPIO.setup(13,GPIO.OUT) GPIO.setup(15,GPIO.OUT) GPIO.setup(12,GPIO.IN) while True: GPIO.output(11,GPIO.HIGH) if (GPIO.input(12) == True): print(“pressed”) time.sleep(3) GPIO.output(13,GPIO.HIGH) time.sleep(3) GPIO.output(11,GPIO.LOW) GPIO.output(11,GPIO.LOW) GPIO.output(13,GPIO.LOW) GPIO.output(15,GPIO.HIGH) time.sleep(3) GPIO.output(15,GPIO.LOW) GPIO.output(13,GPIO.HIGH) time.sleep(3) GPIO.output(13,GPIO.LOW) print(“End”) Edited February 26, 2015 by Joanne
LosOjos Posted February 26, 2015 Posted February 26, 2015 (edited) Just to clarify, I write the import command in python shell and then I run my code in GNU nano... The import statement ought to be at the start of your script. You seem to have a bit of a weird workflow, generally you use the python shell to try out some commands or when using python as a utility (i.e. to calculate a one off formula) and any code you intend to reuse or debug will be saved as a script. Each time you run a script, you're essentially feeding the commands in to a 'fresh' python shell, whereas typing directly in to a python shell, python remembers. This means if you've somehow previously defined a reference incorrectly, python will remember this until you redefine it (or restart the shell to clear the memory). TL;DR: put your import in your script (first line(s)), save your script then run it from bash (i.e. python myScript.py) EDIT: I'm assuming you're running this over SSH as you mentioned you're using nano to edit scipts - if you have a GUI, it's much easier to use IDLE Edited February 26, 2015 by LosOjos
SovietRussia Posted February 26, 2015 Posted February 26, 2015 Nope... same... NameError: name 'GPIO' is not defined But I can run the blinking LED code just fine.... where GPIO is mysteriously defined! OK, I gave in and went on the website that the kid is sold by... this is their code... going to try it now... import time import RPi.GPIO as GPIO GPIO.cleanup() GPIO.setmode(GPIO.BOARD) GPIO.setup(11,GPIO.OUT) GPIO.setup(13,GPIO.OUT) GPIO.setup(15,GPIO.OUT) GPIO.setup(12,GPIO.IN) while True: GPIO.output(11,GPIO.HIGH) if (GPIO.input(12) == True): print(“pressed”) time.sleep(3) GPIO.output(13,GPIO.HIGH) time.sleep(3) GPIO.output(11,GPIO.LOW) GPIO.output(11,GPIO.LOW) GPIO.output(13,GPIO.LOW) GPIO.output(15,GPIO.HIGH) time.sleep(3) GPIO.output(15,GPIO.LOW) GPIO.output(13,GPIO.HIGH) time.sleep(3) GPIO.output(13,GPIO.LOW) print(“End”) Just a warning on that code, it will never ever stop (Unless you terminate the script! - I had it once where I couldn't terminate the script and my Pi crashed)
Joanne Posted February 26, 2015 Author Posted February 26, 2015 I'll be honest... I've no idea what I'm doing!! How should I end it?
LosOjos Posted February 26, 2015 Posted February 26, 2015 I'll be honest... I've no idea what I'm doing!! How should I end it? the line 'while True:' initiates an infinite loop ('while' blocks execute until their condition is False, but as True can never be False, it's an infinite loop). To end it, you either need to edit the loop (you could do 'for i in range(0, 10):' to run the loop 10 times for instance) or just remove the loop completely so it only runs through once. Have a look at this website, it'll get you up and running in Python in no time Welcome - Learn Python - Free Interactive Python Tutorial
Joanne Posted February 26, 2015 Author Posted February 26, 2015 (edited) Well... I've got a red flashing LED... UPON CLOSER INSPECTION: my lights are flashing... confused as to why the green and yellow LEDs are so dull though... all have the same gauge resistor... Edited February 26, 2015 by Joanne
pcstru Posted February 26, 2015 Posted February 26, 2015 (edited) I'm curious about the wiring. You seem to have the LEDs connected to pins 1,3,5 and 7,9,11. Generally I think you want something like : GPIO (n) -> Resistor -> LED anode, LED cathode -> GND (Ground) -OR- GPIO (n) -> LED anode, LED cathode -> Resistor -> GND (Ground) So you could have the red, yellow and green wires into pind 3,5 and 7 (just move them up one) and then on the other side of the breadboard, run all the GND connections back to pin 9. You will need to change the code to account for the change in pins. 3-----(+)LED(-)---/\/\/\/\---+ | 5-----(+)LED(-)---/\/\/\/\---+ | 7-----(+)LED(-)---/\/\/\/\---+ | 9----------------------------+ (hopefully makes some sense! The /\/\/\/\ is a resistor!) (Disclaimer : I haven't played with the GPIO pins on the Pi, I'm just going off the reference documentation on pins). Edited February 26, 2015 by pcstru
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now