This script appears to do nothing when invoked as sudo python segcount.py and when I try at the python prompt (sudo python), after loading all the functions and then poking some variables and calling display() I get the permissions error.
>>> value = 3
>>> print CODE
(1, 0, 0, 1, 1, 1, 1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 8, in display
RuntimeError: No access to /dev/mem. Try running as root!
I would really appreciate any pointers someone can give me to get past this hurdle. TIA.
Code: Select all
# segcount.py # Use Raspberry Pi GPIO to count on a seven segment display # Version 0.1 #!/usr/bin/python import time # Library to allow delay times import RPi.GPIO as GPIO # GPIO Library COUNT = 0 # Set start value CODE = (0,0,0,0,0,0,0) # Segment code flag SEGMENTS = (11,12,13,15,16,19,21) # Set Segments a to g to GPIO port numbers def main(): initialise() value = COUNT # Set count start point time.sleep(5) # Delay before we start while value < 10: # Going from zero to nine decode(value) display() count = count + 1 time.sleep(10) # Pause 10 secs after count gets to nine def initialise(): # Initialize GPIO global SEGMENTS GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) # Ignore warnings when re-running program loop = 0 while loop < 7: # Set GPIO ports to output mode led = SEGMENTS[loop] # led is the number of the GPIO port GPIO.setup(led, GPIO.OUT) loop = loop + 1 def decode(value): # Set segment values for each digit global CODE # Not very pretty - is there a library for this? if value == 0: CODE = (1,1,1,1,1,1,0) elif value == 1: CODE = (0,0,0,0,1,1,0) elif value == 2: CODE = (1,0,1,1,0,1,1) elif value == 3: CODE = (1,0,0,1,1,1,1) elif value == 4: CODE = (1,1,0,0,1,1,1) elif value == 5: CODE = (1,1,0,1,1,0,1) elif value == 6: CODE = (1,1,1,1,1,0,1) elif value == 7: CODE = (1,0,0,0,1,1,0) elif value == 8: CODE = (1,1,1,1,1,1,1) elif value == 9: CODE = (1,1,0,0,1,1,1) def display(): global CODE global SEGMENTS loop = 0 while loop < 7: led = SEGMENTS[loop] if CODE[loop] == 1: # Step through each segment and see if it should be on or off GPIO.output(led, False) # Reverse logic false sets output low, which turns led on else: GPIO.output(led, True) loop = loop + 1 time.sleep(1) # Leave value displayed before returning to main