<div dir="ltr"><div>Hi Rhys,</div><div><br></div><div>That's a good go with Python scripting.</div><div>There's some features in Python that can make the code a bit simpler.</div><div>Starting from the top I have done:<br></div><div>Type declarations on the function SendLetter (aesthetic rather than functionality).</div><div>Used a dictionary for the morse code instead of two lists. It's an associative array which is good for storing key-value pair data.</div><div>Used a for in loop to read every character of MessageToSend. For in loops can also be used to read all items in a list or dictionary.</div><div>Combined the two lines of print with a single f-string. Although I could have also done print(char, morse.get(char)) to achieve the same result, but f-strings are a very useful feature in Python and well worth reading up about.</div><div><br></div><div>When taking a value from a dictionary you can use either the get function or square brackets, e.g. SendLetter(morse[char])</div><div>The get function can have fallback value supplied in case you try using a key which does not exist, e.g. SendLetter(morse.get(char, 'alternate value'))</div><div><br></div><div>Ivan</div><div><br></div><div><br></div><div>from machine import Pin, Timer<br>import utime<br>led = Pin(25, Pin.OUT)<br>utimer = Timer()<br>led.value(1)<br>utime.sleep(1)<br>led.value(0)<br><br>def SendLetter(Letter: str) -> None:<br> led.value(0)<br> MaxLength = len(Letter)<br> for MorseCounter in range (0,MaxLength):<br> if Letter[MorseCounter] == '-':<br> led.value(1)<br> utime.sleep(1.5)<br> led.value(0)<br> else:<br> led.value(1)<br> utime.sleep(0.5)<br> led.value(0)<br> utime.sleep(1.5)<br><br>#Morse Code<br>morse = {<br> 'a': '.-',<br> 'b': '-...',<br> 'c': '-.-.',<br> 'd': '-..',<br> 'e': '.',<br> 'f': '..-.',<br> 'g': '--.',<br> 'h': '--.',<br> 'i': '..',<br> 'j': '.---',<br> 'k': '-.-',<br> 'l': '.-..',<br> 'm': '--',<br> 'n': '-.',<br> 'o': '---',<br> 'p': '.--.',<br> 'q': '--.-',<br> 'r': '.-.',<br> 's': '...',<br> 't': '-',<br> 'u': '..-',<br> 'v': '...-',<br> 'w': '.--',<br> 'x': '-..-',<br> 'y': '-.--',<br> 'z': '--..',<br>}<br><br>char: str<br>MessageToSend: str = 'monty python'<br>MessageToSend = MessageToSend.lower()<br><br>for char in MessageToSend:<br> if char != ' ':<br> SendLetter(morse.get(char))<br> print(f'{char} {morse.get(char)}')<br> else:<br> print('space')<br> utime.sleep(3.5)<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, 16 Apr 2022 at 19:25, Rhys Sage via Swlug <<a href="mailto:swlug@mailman.lug.org.uk">swlug@mailman.lug.org.uk</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">It could do with some tidying and inclusion of Morse number & symbols but this code seems to work on the Pi Pico for transmitting painfully slow Morse code via the onboard LED. I just felt like bragging about a Saturday morning's playing around.<br>
<br>
<br>
#!/usr/bin/env python3<br>
# -*- coding: utf-8 -*-<br>
"""<br>
Created on Sat Apr 16 11:02:05 2022<br>
@author: mary<br>
"""<br>
from machine import Pin, Timer<br>
import utime<br>
led = Pin(25, Pin.OUT)<br>
timer = Timer()<br>
led.value(1)<br>
utime.sleep(1)<br>
led.value(0)<br>
def SendLetter(Letter):<br>
led.value(0)<br>
MaxLength = len(Letter)<br>
for MorseCounter in range (0,MaxLength):<br>
if Letter[MorseCounter] == '-':<br>
led.value(1)<br>
utime.sleep(1.5)<br>
led.value(0)<br>
else:<br>
led.value(1)<br>
utime.sleep(0.5)<br>
led.value(0) <br>
utime.sleep(1.5)<br>
#Morse Code<br>
Alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p',<br>
'q','r','s','t','u','v','w','x','y','z']<br>
Morse = ['.-','-...','-.-.','-..','.','..-.','--.','--.','..','.---','-.-',<br>
'.-..','--','-.','---','.--.', <br>
'--.-','.-.','..','-','..-','...-','.--','-..-','-.--','--..']<br>
TimeUnit = 1 #in seconds)<br>
MessageToSend = 'monty python'<br>
MessageToSend = MessageToSend.lower()<br>
MaxNum = len(MessageToSend)<br>
for MessageCounter in range (0,MaxNum):<br>
if MessageToSend[MessageCounter] != ' ':<br>
SendLetter(Morse[MessageCounter])<br>
print (MessageToSend[MessageCounter])<br>
print (Morse[Alphabet.index(MessageToSend[MessageCounter])])<br>
else:<br>
print ('space')<br>
utime.sleep(3.5)<br>
<br>
<br>
<br>
<br>
Rhys Sage<br>
<br>
-- <br>
Swlug mailing list<br>
<a href="mailto:Swlug@mailman.lug.org.uk" target="_blank">Swlug@mailman.lug.org.uk</a><br>
<a href="https://mailman.lug.org.uk/mailman/listinfo/swlug" rel="noreferrer" target="_blank">https://mailman.lug.org.uk/mailman/listinfo/swlug</a><br>
</blockquote></div>