0% found this document useful (0 votes)
4 views2 pages

Morse Code Decryption System

This document contains a Python script for a Morse code communication system using an LED and a light-dependent resistor (LDR). It includes functions for setting up the hardware, flashing Morse code sequences for letters and numbers, reading Morse code from the LDR, and decoding the received Morse code. The script operates in a loop, waiting for user input to flash characters or to read incoming Morse code signals.

Uploaded by

Anjali Patil
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Morse Code Decryption System

This document contains a Python script for a Morse code communication system using an LED and a light-dependent resistor (LDR). It includes functions for setting up the hardware, flashing Morse code sequences for letters and numbers, reading Morse code from the LDR, and decoding the received Morse code. The script operates in a loop, waiting for user input to flash characters or to read incoming Morse code signals.

Uploaded by

Anjali Patil
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import time

ledPin = 13
ldrPin = 0
threshold = 500 # Adjust this threshold based on your environment
dotDelay = 0.2
dashDelay = 0.6
charDelay = 0.8
wordDelay = 1.4

# For letters
letters = [
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", # A-I
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", # J-R
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." # S-Z
]

# For numbers
numbers = [
"-----", ".----", "..---", "...--", "....-", ".....",
"-....", "--...", "---..", "----."
]

import board
import digitalio

def setup():
global led, ldr
led = [Link](board.D13)
[Link] = [Link]
ldr = [Link](board.A0)
print("Serial started at 9600 baud")

def loop():
if True: # Replace with your serial input check
ch = input() # Replace with your serial read
if 'a' <= ch <= 'z':
flashSequence(letters[ord(ch) - ord('a')])
elif 'A' <= ch <= 'Z':
flashSequence(letters[ord(ch) - ord('A')])
elif '0' <= ch <= '9':
flashSequence(numbers[ord(ch) - ord('0')])
elif ch == ' ':
[Link](dotDelay * 4)

if [Link] < threshold:


morseCode = readMorseCode()
decodedChar = decodeMorseCode(morseCode)
print(decodedChar)

def flashSequence(sequence):
i = 0
while sequence[i] != None:
flashDotOrDash(sequence[i])
i += 1
[Link](dotDelay * 3)

def flashDotOrDash(dotOrDash):
[Link] = True
if dotOrDash == '.':
[Link](dotDelay)
else:
[Link](dashDelay)
[Link] = False
[Link](dotDelay)

def readMorseCode():
morseCode = ""
startTime = [Link]()
while True:
ldrValue = [Link]
if ldrValue < threshold:
signalTime = [Link]() - startTime
if signalTime > dashDelay:
morseCode += "-"
else:
morseCode += "."
[Link](dotDelay)
startTime = [Link]()
else:
pauseTime = [Link]() - startTime
if pauseTime > wordDelay:
break
return morseCode

def decodeMorseCode(morseCode):
for i in range(26):
if morseCode == letters[i]:
return chr(ord('A') + i)
for i in range(10):
if morseCode == numbers[i]:
return chr(ord('0') + i)
return '?'

setup()
while True:
loop()

You might also like