[分享] 摩尔斯电码灯

dcexpert   2020-4-20 16:38 楼主

来自:https://www.hackster.io/amoghsawant2000/morse-code-lamp-e4b7de

 

image.png

 

参考程序

from boltiot import Bolt
import time

api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"    # your bolt api key
device_id  = "xxxxxxxxxx"  #your bolt device id
mybolt = Bolt(api_key, device_id)

# morse code dictionary; source: https://en.wikipedia.org/wiki/Morse_code 
morse_code_dict = {"A":"._", "B":"_...", "C": "_._.", "D": "_..", "E": ".", "F": ".._.",
                   "G": "__.", "H": "....", "I": "..", "J": ".___", "K": "_._", "L": "._..",
                   "M": "__", "N": "_.", "O": "___", "P": ".__.", "Q": "__._", "R": "._.",
                   "S": "...", "T": "_", "U": ".._", "V": "..._", "W": ".__", "X": "_.._",
                   "Y": "_..__", "Z": "__..", "1": ".____", "2": "..___", "3": "...__", "4": "...._",
                   "5": ".....", "6": "_....", "7": "__...", "8": "___..", "9": "____.", "0": "_____", " ": ""}

def list_to_string(s):  # helper function to convert list to string
    string = ""

    for letter in s:
        string += letter

    return string

def filtered_string(string):    # this function filter unnecessary symbols from the important message
    ignore = "~`!@#$%^&*()_+{}|[]\:'<>?,./';-="
    # converting to list because strings are immutable
    string = list(string)   
    ignore = list(ignore)

    for i in range(len(string)):
        for j in range(len(ignore)):
            if string[i] == ignore[j]:
                string[i] = ""

    string = (list_to_string(string)).upper()

    return string

def morse_code_converter(string):   # function to convert main message to morse code
    string = filtered_string(string)
    morse_code = ""

    print("Filtered string: ", string)

    for i in string:
        morse_code = morse_code + morse_code_dict[i] + " "

    return morse_code

def led_blink(code):    # function to blink the led on bolt according to dits and dahs in morse code

# time gaps are according to the source: http://www.morsecodeclassnet.com/lesson3/
    for i in code:
        if i == ".":    # dit
            mybolt.digitalWrite('0', 'HIGH')
            time.sleep(0.1)
            mybolt.digitalWrite('0', 'LOW')

        elif i == "_":  #dah
            mybolt.digitalWrite('0', 'HIGH')
            time.sleep(0.3)
            mybolt.digitalWrite('0', 'LOW')

        elif i == " ":  # space between letters and words
            time.sleep(0.3)


string = input("Enter the string (type '-exit' to quit): ")

while string != "-exit":
    morse_code = morse_code_converter(string)
    print(morse_code)
    led_blink(morse_code)

    string = input("Enter the string (type '-exit' to quit): ")
    
# you can check the output of the code by verifing through this website: https://morsecode.world/international/translator.html

 

回复评论 (3)

用个蜂鸣器取代发光管,不是更好?

用发光管 的话,眼睛看着发光管,怎么纪录收到的摩尔斯电码?

点赞  2020-4-20 17:39
引用: maychang 发表于 2020-4-20 17:39 用个蜂鸣器取代发光管,不是更好? 用发光管 的话,眼睛看着发光管,怎么纪录收到的摩尔斯电码?

航海中是用光发莫尔斯码信号的,声音是在电影中用的,如果做实验用声音,调试时会让其他人受不了的。

 

原文中也提到了用蜂鸣器代替LED,在电路上是比较容易的。

点赞  2020-4-20 22:55
引用: dcexpert 发表于 2020-4-20 22:55 航海中是用光发莫尔斯码信号的,声音是在电影中用的,如果做实验用声音,调试时会让其他人受不了的。 ...

两船之间若使用声音传递莫尔斯码信号,接收方根本听不见。

实验时戴上耳机就是了。实际上接收莫尔斯码信号也都是用耳机的。

点赞  2020-4-21 08:32
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复