User avatar
Bigcat123
Posts: 230
Joined: Thu Aug 23, 2012 2:41 pm

Morse machine problems...

Tue Sep 04, 2012 11:46 am

Sorry for my Python noobness!!!

OK... I am making a program in Python that will allow you to type in a string and then the RaspPI will convert it into Morse code. The Morse code will then flash out on 9 LEDs.

My problem is that I don't know how to get Python to 'scan' the string to see if the letters of the alphabet are in it... That probably didn't make sense so here is an example...

I wan't to convert the string 'SOS' into Morse code. I have all my functions programmed in to the code. But how does python read the inputted letters? Tell me if this doesn't make sense! :)


So here is my program and I am a python beginner so don't be to harsh:

Code: Select all

#Morse code machine
#Enter a string and then it will flash it out in Morse code...
#Uses 9 LEDs and 9 100 ohm resistors
import RPi.GPIO as gpio
import time

gpio.setmode(gpio.BCM)
gpio.setup(0, gpio.OUT)
gpio.setup(1, gpio.OUT)
gpio.setup(4, gpio.OUT)
gpio.setup(17, gpio.OUT)
gpio.setup(21, gpio.OUT)
gpio.setup(22, gpio.OUT)
gpio.setup(10, gpio.OUT)
gpio.setup(9, gpio.OUT)
gpio.setup(11, gpio.OUT)

gpio.output(0, gpio.LOW)
gpio.output(1, gpio.LOW)
gpio.output(4, gpio.LOW)
gpio.output(17, gpio.LOW)
gpio.output(21, gpio.LOW)
gpio.output(22, gpio.LOW)
gpio.output(10, gpio.LOW)
gpio.output(9, gpio.LOW)
gpio.output(11, gpio.LOW)


def dot():
    gpio.output(0, gpio.HIGH)
    gpio.output(1, gpio.HIGH)
    gpio.output(4, gpio.HIGH)
    gpio.output(17, gpio.HIGH)
    gpio.output(21, gpio.HIGH)
    gpio.output(22, gpio.HIGH)
    gpio.output(10, gpio.HIGH)
    gpio.output(9, gpio.HIGH)
    gpio.output(11, gpio.HIGH)
    time.sleep(0.8)
    gpio.output(0, gpio.LOW)
    gpio.output(1, gpio.LOW)
    gpio.output(4, gpio.LOW)
    gpio.output(17, gpio.LOW)
    gpio.output(21, gpio.LOW)
    gpio.output(22, gpio.LOW)
    gpio.output(10, gpio.LOW)
    gpio.output(9, gpio.LOW)
    gpio.output(11, gpio.LOW)
    time.sleep(0.8)

def dash():
    gpio.output(0, gpio.HIGH)
    gpio.output(1, gpio.HIGH)
    gpio.output(4, gpio.HIGH)
    gpio.output(17, gpio.HIGH)
    gpio.output(21, gpio.HIGH)
    gpio.output(22, gpio.HIGH)
    gpio.output(10, gpio.HIGH)
    gpio.output(9, gpio.HIGH)
    gpio.output(11, gpio.HIGH)
    time.sleep(2.4)
    gpio.output(0, gpio.LOW)
    gpio.output(1, gpio.LOW)
    gpio.output(4, gpio.LOW)
    gpio.output(17, gpio.LOW)
    gpio.output(21, gpio.LOW)
    gpio.output(22, gpio.LOW)
    gpio.output(10, gpio.LOW)
    gpio.output(9, gpio.LOW)
    gpio.output(11, gpio.LOW)
    time.sleep(2.4)

def a():
    dot()
    time.sleep(0.8)
    dash()
    time.sleep(0.8)

def b():
    dash()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)

def c():
    dash()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)
    dash()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)

def d():
    dash()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)

def e():
    dot()
    time.sleep(0.8)

def f():
    dot()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)
    dash()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)

def g():
    dash()
    time.sleep(0.8)
    dash()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)

def h():
    dot()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)

def i():
    dot()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)

def j():
    dot()
    time.sleep(0.8)
    dash()
    time.sleep(0.8)
    dash()
    time.sleep(0.8)
    dash()
    time.sleep(0.8)

def k():
    dash()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)
    dash()
    time.sleep(0.8)

def l():
    dot()
    time.sleep(0.8)
    dash()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)

def m():
    dash()
    time.sleep(0.8)
    dash()
    time.sleep(0.8)

def n():
    dash()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)

def o():
    dash()
    time.sleep(0.8)
    dash()
    time.sleep(0.8)
    dash()
    time.sleep(0.8)

def p():
    dot()
    time.sleep(0.8)
    dash()
    time.sleep(0.8)
    dash()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)

def q():
    dash()
    time.sleep(0.8)
    dash()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)
    dash()
    time.sleep(0.8)

def r():
    dot()
    time.sleep(0.8)
    dash()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)

def s():
    dot()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)

def t():
    dash()
    time.sleep(0.8)

def u():
    dot()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)
    dash()
    time.sleep(0.8)

def v():
    dot()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)
    dash()
    time.sleep(0.8)

def w():
    dot()
    time.sleep(0.8)
    dash()
    time.sleep(0.8)
    dash()
    time.sleep(0.8)

def x():
    dash()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)
    dash()
    time.sleep(0.8)

def y():
    dash()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)
    dash()
    time.sleep(0.8)
    dash()
    time.sleep(0.8)

def z():
    dash()
    time.sleep(0.8)
    dash()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)
    dot()
    time.sleep(0.8)

def space():
    time.sleep(4.8)

string = (input('The sentence you would like to convert to morse code: '))    

#CONFUSED HERE....  

if a in string:
    a()
if b in string:
    b()



input('\n\nPress Ctrl + C to exit')
Sorry that it is so long! :D
Just a beginner sharing his experiences on his way to geek nirvana...

lavers
Posts: 17
Joined: Mon Jul 09, 2012 3:45 pm
Location: Canada

Re: Morse machine problems...

Tue Sep 04, 2012 12:09 pm

Look up the 'for' loop in Python. For example, you could do something like:

Code: Select all

for letter in string:
    print letter
This would print each letter of the string, one per line.

bredman
Posts: 1415
Joined: Tue Jan 17, 2012 2:38 pm

Re: Morse machine problems...

Tue Sep 04, 2012 12:25 pm

An important principle in programming is to separate the data from the logic.

In your example, you have hard-coded the actions to be performed for each letter, this is why the program is so long and repetitive.

The better way would be to define the dash/dot sequence for each letter. The the program just looks up the sequence for this letter and executes the sequence.

I have never coded in Python, so I can't give you a definite example. But as an example of how to store the data

a = ".-"
b = "-..."

User avatar
SN
Posts: 1014
Joined: Mon Feb 13, 2012 8:06 pm
Location: Romiley, UK
Contact: Website

Re: Morse machine problems...

Tue Sep 04, 2012 12:27 pm

on the subject of good coding style you might want to move your definitions of what dots and dashes are required for each letter into an array of some kind, index that array by letter and then and use the indexed data to drive the leds rather than write an individual function for each letter :|

as a non-specified language example (so this means this isn't python, c or basic but somewhere between the three ;-))

Code: Select all

array('S') = "..."
array('O') = "---"
etc etc

msgtosend = "SOS"

for letter in msgtosend
   morsecode = array(letter)
   call emit(morsecode)
end for

func emit(morsecode)
   for dotdash in morsecode
      if dotdash = '.' then call dot
      if dotdash = "-" then call dash
   end for
end func
Steve N – binatone mk4->intellivision->zx81->spectrum->cbm64->cpc6128->520stfm->pc->raspi ?

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: Morse machine problems...

Tue Sep 04, 2012 12:31 pm

@Bigcat123: Maybe you should learn Python basics first. The basic control structures and data types are key to write programs. A dictionary mapping characters to dots and dahes and loops for all those gpio.* lines will shorten the program significantly.

Code: Select all

while not self.asleep():
    sheep += 1

User avatar
Mortimer
Posts: 924
Joined: Sun Jun 10, 2012 3:57 pm

Re: Morse machine problems...

Tue Sep 04, 2012 12:37 pm

Don't forget to program for the breaks. ;)
--------------
The purpose of a little toe is to ensure you keep your furniture in the right place.

brs
Posts: 70
Joined: Fri Aug 17, 2012 9:27 pm
Location: Zurich

Re: Morse machine problems...

Tue Sep 04, 2012 1:13 pm

I think the timing is a bit off. In particular, there are different types of spaces between, symbols, letters and words:

2 Spacing and length of the signals
2.1 A dash is equal to three dots.
2.2 The space between the signals forming the same letter is equal to one dot.
2.3 The space between two letters is equal to three dots.
2.4 The space between two words is equal to seven dots.

According to http://www.itu.int/dms_pubrec/itu-r/rec ... !PDF-E.pdf

bulldog5046
Posts: 36
Joined: Thu Jun 21, 2012 1:50 pm

Re: Morse machine problems...

Tue Sep 04, 2012 6:33 pm

I'm caveat my reply by letting you know i'm still learning myself, but something like this may work using your original code.

I'm just unsure if you can use a variable to create the function name your calling on?

Code: Select all

def translate(self, reponse):
	count=0
	word = len(response)
	when count <= word:
		letter = word[count]
		global letter()
		
translate(string)

Return to “Python”