0

I'm writing a small morse code translator.

But for some reason, it's not working.

For some reason, the broadcasting part is not working... I can't get it. Any help is welcome !

Here is the code:

#import RPi.GPIO as GPIO
import re,time, pdb

def get_user_text():
    user_text = raw_input("Please enter the message you would lile to broadcast. >> ")
    user_text = user_text.lower()
    word_list = list(user_text)
    return word_list

def text_to_morse_code(alpha_text):
    morse_code = []
    for letter in alpha_text:
        if letter == "a" or letter == "à" or letter == "â" or letter == "ä":
            morse_code.append("01")
        if letter == "b":
            morse_code.append("1000")
        if letter == "c":
            morse_code.append("1010")
        if letter == "d":
            morse_code.append("100")
        if letter == "e" or letter == "è" or letter == "é" or letter == "ê":
            morse_code.append("0")
        if letter == "f":
            morse_code.append("0010")
        if letter == "g":
            morse_code.append("110")
        if letter == "h":
            morse_code.append("0000")
        if letter == "i" or letter == "î" or letter == "ï":
            morse_code.append("00")
        if letter == "j":
            morse_code.append("0111")
        if letter == "k":
            morse_code.append("101")
        if letter == "l":
            morse_code.append("0100")
        if letter == "m":
            morse_code.append("11")
        if letter == "n":
            morse_code.append("10")
        if letter == "o":
            morse_code.append("111")
        if letter == "p":
            morse_code.append("0110")
        if letter == "q":
            morse_code.append("1101")
        if letter == "r":
            morse_code.append("010")
        if letter == "s":
            morse_code.append("111")
        if letter == "t":
            morse_code.append("0")
        if letter == "u":
            morse_code.append("001")
        if letter == "v":
            morse_code.append("0001")
        if letter == "w":
            morse_code.append("011")
        if letter == "x":
            morse_code.append("1001")
        if letter == "y":
            morse_code.append("1011")
        if letter == "z":
            morse_code.append("1100")
        if letter == ".":
            morse_code.append("010101")
        if letter == " ":
            morse_code.append(" ")
        else:
            pass
    morse_code = ''.join(map(str, morse_code))
    morse_code = list(morse_code)
    return morse_code

def broadcast_code(code_to_broadcast, pin):

    # Set the board as BOARD
    #GPIO.setmode(GPIO.BOARD)
    print("Set the board to BOARD")

    # Setup the n th pin to OUTPUT
    #GPIO.setup(pin, GPIO.OUT)
    print("Set the "+str(pin)+"th to OUTPUT")

    # Starting the broadcast
    print("Starting Broadcast")
    start_broadcast = [0,1,0,1]

    for number in start_broadcast:
        if number == 1:
            #GPIO.output(pin,True)
            time.sleep(1)
            #GPIO.output(pin, False)
            print(number)
        if number == 0:
            #GPIO.output(pin,True)
            time.sleep(0.5)
            #GPIO.output(pin, False)
            print(number)

    print("Broadcasting")
    code_to_broadcast = code_to_broadcast
    for number in code_to_broadcast:
        if number == 1:
            #GPIO.output(pin,True)
            time.sleep(1)
            #GPIO.output(pin, False)
            print(number)
        if number == 0:
            #GPIO.output(pin,True)
            time.sleep(0.5)
            #GPIO.output(pin, False)
            print(number)

    #Boardcast end of transmission.
    print("Ending Boardcast")
    end_broadcast = [0,0,0,1,0,1]

    for number in end_broadcast:
        if number == 1:
            #GPIO.output(pin,True)
            time.sleep(1)
            #GPIO.output(pin, False)
            print(number)
        if number == 0:
            #GPIO.output(pin,True)
            time.sleep(0.5)
            #GPIO.output(pin, False)
            print(number)



    #GPIO.cleanup()
    print("Cleaned up the board.")

def get_code_broadcast():
    #
    #GPIO.output(pin,True)
    print("Hello")

if __name__ == '__main__':

    code = get_user_text()
    code = text_to_morse_code(code)
    broadcast_code(code,7)

And the output I get :

$ Please enter the message you would lile to broadcast. >> Hello
Set the board to BOARD
Set the 7th to OUTPUT
Starting Broadcast
0
1
0
1
Broadcasting
Ending Boardcast
0
0
0
1
0
1
Cleaned up the board.
3
  • 2
    instead of using an if block that is 26 levels , you couldve just used a dict. Commented Mar 22, 2015 at 21:42
  • How would I do that ? i'm very new to python. Commented Mar 22, 2015 at 22:05
  • 5.5. Dictionaries Commented Mar 22, 2015 at 22:36

2 Answers 2

1

Example with dict:

morse_dict = {
    'a': '01',
    'b': '1000',
    'c': '1010',
}

def get_morse_code(text):
    morse_code = []
    for n in text:
        morse_code.append(morse_dict[n])
    return morse_code

And now you can do

>>> print(get_morse_code('abcba'))

All you need to do is to expand the dict with all the morse stuff.

Sign up to request clarification or add additional context in comments.

Comments

0

Your question is about this loop:

for number in code_to_broadcast:
    if number == 1:
        #GPIO.output(pin,True)
        time.sleep(1)
        #GPIO.output(pin, False)
        print(number)
    if number == 0:
        #GPIO.output(pin,True)
        time.sleep(0.5)
        #GPIO.output(pin, False)
        print(number)

The variable code_to_broadcase is a string. This is the root of your problem.

The string "0" is not equal to the integer 0 in Python. Similarly, "1" is not equal to 1. Your code would work if you fixed the comparisons:

for number in code_to_broadcast:
    if number == "1":                  # test against a string
        #GPIO.output(pin,True)
        time.sleep(1)
        #GPIO.output(pin, False)
        print(number)
    if number == "0":                  # here too
        #GPIO.output(pin,True)
        time.sleep(0.5)
        #GPIO.output(pin, False)
        print(number)

Alternatively, you could probably change the string to a list of integers instead, and use your current loop code unmodified.

Note that because the only difference between your two blocks is the amount of time you sleep for, you can simplify things by simply conditionalizing the amount of the delay:

for number in code_to_broadcast:
    #GPIO.output(pin,True)
    time.sleep(1 if number == "1" else 0.5)  # use a conditional expression
    #GPIO.output(pin, False)
    print(number)

There are a number of other places your code could be improved (mostly by factoring out repeated coded). I'd strongly suggest using a dictionary to store the translation between letters and morse code dits and dahs, rather than using a very long series of if statements. (If you do keep the if chain, all of the ifs after the first should probably be elifs, since they'll only be true if none of the previous ones were. Using elif will let Python stop testing the later conditions if an earlier one was True.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.