1

I'm currrently working on a simple count program for RPi-3 with a seven-segment-Display. I have two ways of counting from one to ten, one manually, always lighting up the single segments I need, and another way, which is based on a numpy matrix.

The manual way works fine but I have Problems with the matrix way of doing it. Here is the code with some explications for understanding:

import RPi.GPIO as GPIO
import time
import numpy

GPIO.setmode(GPIO.BOARD)

GPIO.setup(31, GPIO.OUT)  #up right
GPIO.setup(32, GPIO.OUT)  #up
GPIO.setup(33, GPIO.OUT)  #up left
GPIO.setup(35, GPIO.OUT)  #middle
GPIO.setup(36, GPIO.OUT)  #down right
GPIO.setup(37, GPIO.OUT)  #down left
GPIO.setup(38, GPIO.OUT)  #decimal point(in the bottom right corner)
GPIO.setup(40, GPIO.OUT)  #down

def all_off():                 #turns all segments off, works as well!
    GPIO.output(31, False)
    GPIO.output(32, False)
    GPIO.output(33, False)
    GPIO.output(35, False)
    GPIO.output(36, False)
    GPIO.output(37, False)
    GPIO.output(38, False)
    GPIO.output(40, False)

matrix = numpy.matrix([[1,0,0,0,1,0,0,0],  #those are the combinations
                      [1,1,0,1,0,1,0,1],   #from one to nine and finishing
                      [1,1,0,1,1,0,0,1],   #with zero
                      [1,0,1,1,1,0,0,0],   #first number of a line is value for Pin31 then 32,
                      [0,1,1,1,1,0,0,1],   #33, 35, 36, 37, 38, 40
                      [0,1,1,1,1,1,0,1],
                      [1,1,0,0,1,0,0,0],   #combinations are correct, btw!
                      [1,1,1,1,1,1,0,1],
                      [1,1,1,1,1,0,0,1]])

def set_to(byte):
    all_off()
    GPIO.output(31, int(byte[0]))  #<-- this is the line where the error 
    GPIO.output(32, int(byte[1]))  #occurs
    GPIO.output(33, int(byte[2]))
    GPIO.output(35, int(byte[3]))
    GPIO.output(36, int(byte[4]))
    GPIO.output(37, int(byte[5]))
    GPIO.output(38, int(byte[6]))
    GPIO.output(40, int(byte[7]))

for line in matrix:      #the actual counting program
    set_to(matrix[line])
    time.sleep(1)

GPIO.cleanup()

The problem is apparently, that the array can't be read? I don't get what the problem is or what to do to fix this issue. It seems to have something to do with the 'line' of the matrix is just anouther matrix(with just one line).

I really don't know what to do, please help!!

  • 1
    may this answer from another question could provide you a clue: http://stackoverflow.com/a/21697288/2696355 anyway, why do you use a numpy matrix? I think you could simply use a list of lists without any problem. – Tryph Aug 25 '16 at 12:32
  • 1
    I think you need to change the call to set_to, it looks like it should be set_to(line), you're currently trying to index an array, with another array, which is what it's complaining it can't turn into a scalar. I also agree with Tryph about just using lists. Also, provding the actual traceback may have made this easier to spot. – R.Sharp Aug 25 '16 at 12:45

1 Answers1

1

I have changed my strategy and now use an numpy array. Now I have a working code:

import RPi.GPIO as GPIO
import time
import numpy

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

GPIO.setup(31, GPIO.OUT)  
GPIO.setup(32, GPIO.OUT)  
GPIO.setup(33, GPIO.OUT)  
GPIO.setup(35, GPIO.OUT)  
GPIO.setup(36, GPIO.OUT)  
GPIO.setup(37, GPIO.OUT)  
GPIO.setup(38, GPIO.OUT)  
GPIO.setup(40, GPIO.OUT)  

pinlist = [(31, 0), (32, 1) ,(33, 2) ,(35, 3) ,(36, 4) ,(37, 5) ,(38, 6), (40, 7)]

def all_off():
    GPIO.output(31, False)
    GPIO.output(32, False)
    GPIO.output(33, False)
    GPIO.output(35, False)
    GPIO.output(36, False)
    GPIO.output(37, False)
    GPIO.output(38, False)
    GPIO.output(40, False)

matrix = numpy.array([[1,0,0,0,1,0,0,0],
                      [1,1,0,1,0,1,0,1],
                      [1,1,0,1,1,0,0,1],
                      [1,0,1,1,1,0,0,0],
                      [0,1,1,1,1,0,0,1],
                      [0,1,1,1,1,1,0,1],
                      [1,1,0,0,1,0,0,0],
                      [1,1,1,1,1,1,0,1],
                      [1,1,1,1,1,0,0,1]])

for i in range(0, 9):
    all_off()
    for (pin, j) in pinlist:
        GPIO.output(pin, int(matrix[i][j]))
    time.sleep(0.25)

GPIO.cleanup()

This works as following:

I created a list of all Pin's with their number(packed in a tuple). Those Tuple's are read by the for-loop inside of the big for-loop. Now I take reference to the "coordinates" of a single Point inside of my array. This means, that it's a bit slow(you can see the single segment's pop up), but I luckily don't have to care!

Nevertheless a big thank you for your support!

  • Could you please have a look and look for improvements on the code? Is this the most efficient way of doing it? – Major Samor Aug 25 '16 at 14:08