Friday, February 27, 2015

Running a 8x40 LED Array from the Raspberry Pi

I have been trying to run one of the 8x40 LED signs from a Raspberry Pi.  For some reason I am getting a lot of glitches.  I know the Raspberry Pi can cause glitches when the GUI is running, but this is different.  UPDATE - I fixed the glitch it was a bad ground on the ribbon cable.

This is what it looks like.  A glitch is happening over the letter "Y".
Here is a video showing the glitching.
https://www.youtube.com/watch?v=n4tE-9H_Adg&feature=youtu.be

Another problem is that I cannot figure out how to do what is called macro substitution in python.  As a result the code is a bit long.  If I could say "if red&row[shift]=1" I could eliminate several lines of code. UPDATE - I have a solution.  Make the letter arrays into one big array for each letter.  Then use "if red[shift+row*5]".  I have used that technique for working with some other devices.

Here is my code to make it work:
# LED 8x40 Dual Color array
# Uses individual letters strung together
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)

GPIO.setup(17, GPIO.OUT) # Red Data
GPIO.setup(18, GPIO.OUT) # Green Data
GPIO.setup(27, GPIO.OUT) # Clock, 21 on older
GPIO.setup(22, GPIO.OUT) # Latch
GPIO.setup(23, GPIO.OUT) # Row Data 1
GPIO.setup(24, GPIO.OUT) # Row Data 2
GPIO.setup(25, GPIO.OUT) # Row Data 4
GPIO.setup(4, GPIO.OUT) # Row Enable

A1 =[0,0,0,0,0]
A2 =[0,0,1,0,0]
A3 =[0,1,0,1,0]
A4 =[1,0,0,0,1]
A5 =[1,1,1,1,1]
A6 =[1,0,0,0,1]
A7 =[1,0,0,0,1]
A8 =[1,0,0,0,1]

B1 =[0,0,0,0,0]
B2 =[1,1,1,1,0]
B3 =[1,0,0,0,1]
B4 =[1,0,0,0,1]
B5 =[1,1,1,1,0]
B6 =[1,0,0,0,1]
B7 =[1,0,0,0,1]
B8 =[1,1,1,1,0]

E1 =[0,0,0,0,0]
E2 =[1,1,1,1,1]
E3 =[1,0,0,0,0]
E4 =[1,0,0,0,0]
E5 =[1,1,1,1,0]
E6 =[1,0,0,0,0]
E7 =[1,0,0,0,0]
E8 =[1,1,1,1,1]

P1 =[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4+R4+Y4
Green5=R5+A5+Z5+P5+B5+Z5+R5+Y5
Green6=R6+A6+Z6+P6+B6+Z6+R6+Y6
Green7=R7+A7+Z7+P7+B7+Z7+R7+Y7
Green8=R8+A8+Z8+P8+B8+Z8+R8+Y8

# set up the loop
cycle= 0
while cycle < 1000:
  row = 0
  while row <9: 

    row = row+1
    # Send data to the shift registers
    shift = 39
    while shift >= 0:
        GPIO.output(17, GPIO.LOW)
        GPIO.output(18, GPIO.LOW)
        # determine if bit is set or clear data is NOT inverted
        if row==1:
          if Red1[shift] == 1: GPIO.output(17, GPIO.HIGH)
          if Green1[shift] == 1: GPIO.output(18, GPIO.HIGH)
        if row==2:
          if Red2[shift] == 1: GPIO.output(17, GPIO.HIGH)
          if Green2[shift] == 1: GPIO.output(18, GPIO.HIGH)
        if row==3:
          if Red3[shift] == 1: GPIO.output(17, GPIO.HIGH)
          if Green3[shift] == 1: GPIO.output(18, GPIO.HIGH)
        if row==4:
          if Red4[shift] == 1: GPIO.output(17, GPIO.HIGH)
          if Green4[shift] == 1: GPIO.output(18, GPIO.HIGH)
        if row==5:
          if Red5[shift] == 1: GPIO.output(17, GPIO.HIGH)
          if Green5[shift] == 1: GPIO.output(18, GPIO.HIGH)
        if row==6:
          if Red6[shift] == 1: GPIO.output(17, GPIO.HIGH)
          if Green6[shift] == 1: GPIO.output(18, GPIO.HIGH)
        if row==7:
          if Red7[shift] == 1: GPIO.output(17, GPIO.HIGH)
          if Green7[shift] == 1: GPIO.output(18, GPIO.HIGH)
        if row==8:
          if Red8[shift] == 1: GPIO.output(17, GPIO.HIGH)
          if Green8[shift] == 1: GPIO.output(18, GPIO.HIGH)
        # advance the clock
        GPIO.output(27, GPIO.HIGH); GPIO.output(27, GPIO.LOW)
        shift=shift-1
    # select the row data is inverted
    GPIO.output(4, GPIO.HIGH) # Turn off display
    if row==2: GPIO.output(23, GPIO.LOW); GPIO.output(24, GPIO.HIGH); GPIO.output(25, GPIO.HIGH)
    if row==3: GPIO.output(23, GPIO.HIGH); GPIO.output(24, GPIO.LOW); GPIO.output(25, GPIO.HIGH)
    if row==4: GPIO.output(23, GPIO.LOW); GPIO.output(24, GPIO.LOW); GPIO.output(25, GPIO.HIGH)
    if row==5: GPIO.output(23, GPIO.HIGH); GPIO.output(24, GPIO.HIGH); GPIO.output(25, GPIO.LOW)
    if row==6: GPIO.output(23, GPIO.LOW); GPIO.output(24, GPIO.HIGH); GPIO.output(25, GPIO.LOW)
    if row==7: GPIO.output(23, GPIO.HIGH); GPIO.output(24, GPIO.LOW); GPIO.output(25, GPIO.LOW)
    if row==8: GPIO.output(23, GPIO.LOW); GPIO.output(24, GPIO.LOW); GPIO.output(25, GPIO.LOW)
    # latch and display the data
    GPIO.output(22, GPIO.LOW); GPIO.output(22, GPIO.HIGH)
    GPIO.output(4, GPIO.LOW) # Turn back on
    # time.sleep(.0005)
  cycle=cycle+1 

 

No comments: