I am new to programming, python and raspberry-pi, but I am keen to learn more. After doing a few basic tutorials I though it might be best to give it a shot and just come up with a task in which I could practice my current skills. I prepared a csv file with morse-code and wrote a little script to access the csv and translate it into a blinking LED. It works! Now I would like to learn a bit more and improve my code. How would a simpler/faster/neater solution look like?
I would appreciate any kind of constructive criticism. (I made a few comments for myself. I hope it is not too annoying)
import RPi.GPIO as GPIO
import time
import csv
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
#cleaning out the csv
with open('Morse.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
letters = [] #a new list with letters only
codes = [] #a new list with morse code only
next(readCSV) #jump over the column description line and an empty line
next(readCSV)
for row in readCSV: #fill up the new lists
letter = row[0]
code = row
code.remove(row[0])
letters.append(letter) #append letters only
codes.append(code) #append everything but letters
LetterInput = input('what do you want to morse? ')
MorseCode = [] #a new list where the final code for the LED input is saved
for SingLetter in LetterInput: #iterate through the input
SingLetUp = SingLetter.upper() #capitalize letters so they can be found in csv
if SingLetUp not in letters: #jump over spaces and special characters
continue
MorseDex = letters.index(SingLetUp) #find code that corresponds with letter by matching indices
SingCode = codes[MorseDex]
MorseCode.append(SingCode) #fill up list
#iterate through list in list to seperate single words
for pos in MorseCode:
for sing in pos:
print(sing)
x = 'short'
y = 'long'
z = 'none'
#LED blinks while parsing through the list
if sing == x:
GPIO.output(18, GPIO.HIGH)
time.sleep(0.2)
GPIO.output(18, GPIO.LOW)
time.sleep(0.5)
if sing == y:
GPIO.output(18, GPIO.HIGH)
time.sleep(1)
GPIO.output(18, GPIO.LOW)
time.sleep(0.5)
if sing == z:
GPIO.output(18, GPIO.LOW)
time.sleep(1)
GPIO.output(18, GPIO.LOW)
GPIO.cleanup()
And here is the csv file
letter,position 1,position 2,position 3,position 4,position 5
,,,,,
A,short,long,none,none,none
B,long,short,short,short,none
C,long,short,long,short,none
D,long,short,short,none,none
E,short,none,none,none,none
F,short,short,long,short,none
G,long,long,short,none,none
H,short,short,short,short,none
I,short,short,none,none,none
J,short,long,long,long,none
K,long,short,long,none,none
L,short,long,short,short,none
M,long,long,none,none,none
N,long,short,none,none,none
O,long,long,long,none,none
P,short,long,long,short,none
Q,long,long,short,long,none
R,short,long,short,none,none
S,short,short,short,none,none
T,long,none,none,none,none
U,short,short,long,none,none
V,short,short,short,long,none
W,short,long,long,none,none
X,long,short,short,long,none
Y,long,short,long,long,none
Z,long,long,short,short,none
1,short,long,long,long,long
2,short,short,long,long,long
3,short,short,short,long,long
4,short,short,short,short,long
5,short,short,short,short,short
6,long,short,short,short,short
7,long,long,short,short,short
8,long,long,long,short,short
9,long,long,long,long,short
0,long,long,long,long,long
Morse.csv
looks like, is there any way you could add it to your question (separate code section?) or paste it to something like bpaste and add a link to it? \$\endgroup\$