|
| 1 | +#!/usr/bin/env python |
| 2 | +import RPi.GPIO as GPIO |
| 3 | +import time |
| 4 | + |
| 5 | +GPIO.setmode(GPIO.BCM) |
| 6 | + |
| 7 | +digit = 0 |
| 8 | + |
| 9 | +#Pinouts |
| 10 | +A = 22 |
| 11 | +B = 27 |
| 12 | +C = 4 |
| 13 | +D = 24 |
| 14 | +E = 25 |
| 15 | +F = 18 |
| 16 | +G =23 |
| 17 | +DP = 17 |
| 18 | + |
| 19 | +upswitch = 11 |
| 20 | +downswitch = 7 |
| 21 | +GPIO.setup(upswitch, GPIO.IN) |
| 22 | +GPIO.setup(downswitch, GPIO.IN) |
| 23 | + |
| 24 | + |
| 25 | +GPIO.setup(A, GPIO.OUT) |
| 26 | +GPIO.setup(B, GPIO.OUT) |
| 27 | +GPIO.setup(C, GPIO.OUT) |
| 28 | +GPIO.setup(D, GPIO.OUT) |
| 29 | +GPIO.setup(E, GPIO.OUT) |
| 30 | +GPIO.setup(F, GPIO.OUT) |
| 31 | +GPIO.setup(G, GPIO.OUT) |
| 32 | +GPIO.setup(DP, GPIO.OUT) |
| 33 | + |
| 34 | +GPIO.output(A,GPIO.LOW) |
| 35 | +GPIO.output(B,GPIO.LOW) |
| 36 | +GPIO.output(C,GPIO.LOW) |
| 37 | +GPIO.output(D,GPIO.LOW) |
| 38 | +GPIO.output(E,GPIO.LOW) |
| 39 | +GPIO.output(F,GPIO.LOW) |
| 40 | +GPIO.output(G,GPIO.LOW) |
| 41 | +GPIO.output(DP,GPIO.LOW) |
| 42 | + |
| 43 | +def displaydigit(digit): |
| 44 | + if digit == 0: |
| 45 | + GPIO.output(A, GPIO.HIGH) |
| 46 | + GPIO.output(B, GPIO.HIGH) |
| 47 | + GPIO.output(C, GPIO.HIGH) |
| 48 | + GPIO.output(D, GPIO.HIGH) |
| 49 | + GPIO.output(E, GPIO.HIGH) |
| 50 | + GPIO.output(F, GPIO.HIGH) |
| 51 | + GPIO.output(G, GPIO.LOW) |
| 52 | + GPIO.output(DP, GPIO.LOW) |
| 53 | + return |
| 54 | + elif digit == 1: |
| 55 | + GPIO.output(A, GPIO.LOW) |
| 56 | + GPIO.output(B, GPIO.HIGH) |
| 57 | + GPIO.output(C, GPIO.HIGH) |
| 58 | + GPIO.output(D, GPIO.LOW) |
| 59 | + GPIO.output(E, GPIO.LOW) |
| 60 | + GPIO.output(F, GPIO.LOW) |
| 61 | + GPIO.output(G, GPIO.LOW) |
| 62 | + GPIO.output(DP, GPIO.LOW) |
| 63 | + return |
| 64 | + elif digit == 2: |
| 65 | + GPIO.output(A, GPIO.HIGH) |
| 66 | + GPIO.output(B, GPIO.HIGH) |
| 67 | + GPIO.output(C, GPIO.LOW) |
| 68 | + GPIO.output(D, GPIO.HIGH) |
| 69 | + GPIO.output(E, GPIO.HIGH) |
| 70 | + GPIO.output(F, GPIO.LOW) |
| 71 | + GPIO.output(G, GPIO.HIGH) |
| 72 | + GPIO.output(DP, GPIO.LOW) |
| 73 | + return |
| 74 | + elif digit == 3: |
| 75 | + GPIO.output(A, GPIO.HIGH) |
| 76 | + GPIO.output(B, GPIO.HIGH) |
| 77 | + GPIO.output(C, GPIO.HIGH) |
| 78 | + GPIO.output(D, GPIO.HIGH) |
| 79 | + GPIO.output(E, GPIO.LOW) |
| 80 | + GPIO.output(F, GPIO.LOW) |
| 81 | + GPIO.output(G, GPIO.HIGH) |
| 82 | + GPIO.output(DP, GPIO.LOW) |
| 83 | + return |
| 84 | + elif digit == 4: |
| 85 | + GPIO.output(A, GPIO.LOW) |
| 86 | + GPIO.output(B, GPIO.HIGH) |
| 87 | + GPIO.output(C, GPIO.HIGH) |
| 88 | + GPIO.output(D, GPIO.LOW) |
| 89 | + GPIO.output(E, GPIO.LOW) |
| 90 | + GPIO.output(F, GPIO.HIGH) |
| 91 | + GPIO.output(G, GPIO.HIGH) |
| 92 | + GPIO.output(DP, GPIO.LOW) |
| 93 | + return |
| 94 | + elif digit == 5: |
| 95 | + GPIO.output(A, GPIO.HIGH) |
| 96 | + GPIO.output(B, GPIO.LOW) |
| 97 | + GPIO.output(C, GPIO.HIGH) |
| 98 | + GPIO.output(D, GPIO.HIGH) |
| 99 | + GPIO.output(E, GPIO.LOW) |
| 100 | + GPIO.output(F, GPIO.HIGH) |
| 101 | + GPIO.output(G, GPIO.HIGH) |
| 102 | + GPIO.output(DP, GPIO.LOW) |
| 103 | + return |
| 104 | + elif digit == 6: |
| 105 | + GPIO.output(A, GPIO.HIGH) |
| 106 | + GPIO.output(B, GPIO.LOW) |
| 107 | + GPIO.output(C, GPIO.HIGH) |
| 108 | + GPIO.output(D, GPIO.HIGH) |
| 109 | + GPIO.output(E, GPIO.HIGH) |
| 110 | + GPIO.output(F, GPIO.HIGH) |
| 111 | + GPIO.output(G, GPIO.HIGH) |
| 112 | + GPIO.output(DP, GPIO.LOW) |
| 113 | + return |
| 114 | + elif digit == 7: |
| 115 | + GPIO.output(A, GPIO.HIGH) |
| 116 | + GPIO.output(B, GPIO.HIGH) |
| 117 | + GPIO.output(C, GPIO.HIGH) |
| 118 | + GPIO.output(D, GPIO.LOW) |
| 119 | + GPIO.output(E, GPIO.LOW) |
| 120 | + GPIO.output(F, GPIO.LOW) |
| 121 | + GPIO.output(G, GPIO.LOW) |
| 122 | + GPIO.output(DP, GPIO.LOW) |
| 123 | + return |
| 124 | + elif digit == 8: |
| 125 | + GPIO.output(A, GPIO.HIGH) |
| 126 | + GPIO.output(B, GPIO.HIGH) |
| 127 | + GPIO.output(C, GPIO.HIGH) |
| 128 | + GPIO.output(D, GPIO.HIGH) |
| 129 | + GPIO.output(E, GPIO.HIGH) |
| 130 | + GPIO.output(F, GPIO.HIGH) |
| 131 | + GPIO.output(G, GPIO.HIGH) |
| 132 | + GPIO.output(DP, GPIO.LOW) |
| 133 | + return |
| 134 | + elif digit == 9: |
| 135 | + GPIO.output(A, GPIO.HIGH) |
| 136 | + GPIO.output(B, GPIO.HIGH) |
| 137 | + GPIO.output(C, GPIO.HIGH) |
| 138 | + GPIO.output(D, GPIO.HIGH) |
| 139 | + GPIO.output(E, GPIO.LOW) |
| 140 | + GPIO.output(F, GPIO.HIGH) |
| 141 | + GPIO.output(G, GPIO.HIGH) |
| 142 | + GPIO.output(DP, GPIO.LOW) |
| 143 | + return |
| 144 | + |
| 145 | +def displaySequence(sequencelist, interstepdelay, enddelay): |
| 146 | + #Iterates though every step of the sequence |
| 147 | + for sequencestepdict in sequencelist: |
| 148 | + #Iterates through every LED segment in the sequence step |
| 149 | + for ledsegment in sequencestepdict: |
| 150 | + GPIO.output(ledsegment, sequencestepdict[ledsegment]) |
| 151 | + time.sleep(interstepdelay) |
| 152 | + time.sleep(enddelay) |
| 153 | + |
| 154 | +### |
| 155 | +#Initialization animation |
| 156 | +#Each dictionary is a animation step in the sequence |
| 157 | +off = GPIO.LOW |
| 158 | +on = GPIO.HIGH |
| 159 | + |
| 160 | +POSTsequence = [{'A': on}, {'A': off,'B': on}, {'B': off, 'C': on}, {'C': off, 'D': on}, {'D': off, 'E': on}, {'E': off, 'F': on}, {'F': off, 'G': on}, {'G': off, 'A': on}, {'A': off,'B': on}, {'B': off, 'C': on}, {'C': off, 'D': on}, {'D': off, 'E': on}, {'E': off, 'F': on}, {'F': off, 'G': on}, {'G': off}, {'A': on, 'B': on, 'C': on, 'D': on, 'E': on, 'F': on, 'G': on, 'DP': on}, {'A': off, 'B': off, 'C': off, 'D': off, 'E': off, 'F': off, 'G': off, 'DP': off}, {'A': on, 'B': on, 'C': on, 'D': on, 'E': on, 'F': on, 'G': on, 'DP': on}, {'A': off, 'B': off, 'C': off, 'D': off, 'E': off, 'F': off, 'G': off, 'DP': off}] |
| 161 | + |
| 162 | +displaySequence(POSTsequence, .1, .25) |
| 163 | +### |
| 164 | + |
| 165 | +prev_down = 0 |
| 166 | +prev_up = 0 |
| 167 | +updigit = GPIO.input(upswitch) |
| 168 | +downdigit = GPIO.input(downswitch) |
| 169 | + |
| 170 | +while True: |
| 171 | + #take a reading |
| 172 | + displaydigit(digit) |
| 173 | + #if the last reading was low and this one high, print |
| 174 | + if ((not prev_up) and updigit): |
| 175 | + digit = abs(digit + 1) |
| 176 | + #update previous input |
| 177 | + |
| 178 | + #slight pause to debounce |
| 179 | + #if the last reading was low and this one high, print |
| 180 | + if ((not prev_down) and downdigit): |
| 181 | + digit = abs(digit - 1) |
| 182 | + #update previous input |
| 183 | + prev_down = downdigit |
| 184 | + prev_up = updigit |
| 185 | + #slight pause to debounce |
| 186 | + time.sleep(0.05) |
| 187 | + |
| 188 | + |
| 189 | +GPIO.cleanup() |
0 commit comments