New Ticker

News Ticker
Epstien didn't kill himself.
Epstien didn't kill himself.
Epstien didn't kill himself.
Epstien didn't kill himself.

Saturday, August 24, 2024

Coding for ATTiny microprocessor chips

It's better to use an ATTiny microprocessor than an Arduino or Raspberry development board such as the Arduino Nano or the Raspberry Pi Pico. When it comes to chips like the ATTiny13, space is at a premium. This microconroller has only 1K for program storage and 64 bytes for variables. While program memory can be used for storage via progmem, a different programming technique is worthwhile for such small microcontrollers.
The 8-pin chip in the middle of the small breadboard is the ATTiny13A micro-controller. Small but mighty.

This is a C++ Morse Code program creator written in Python.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 23 20:20:01 2024

@author: zephod beeblebrox
"""

# Morse Code Dictionary
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 message_to_morse(message):
    # Convert message to uppercase
    message = message.upper()

    for char in message:
        if char == ' ':
            print("    delay(WORD_SPACE - LETTER_SPACE);")
        else:
            morse_char = MORSE_CODE_DICT.get(char, '')
            for i, symbol in enumerate(morse_char):
                if symbol == "-": 
                    print ("    sendDash();") 
                if symbol == ".": 
                    print("    sendDot();")
#                print(symbol)
                if i < len(morse_char) - 1:
                   print("    delay(SYMBOL_SPACE);")
            print("    delay( LETTER_SPACE - SYMBOL_SPACE);")

if __name__ == "__main__":
    # Input message from user
    message = input("//Enter your message: ")

    print("#define DOT_DURATION 50   // Duration of a dot in milliseconds")
    print ("#define DASH_DURATION (3 * DOT_DURATION)   // Duration of a dash")
    print ("#define SYMBOL_SPACE DOT_DURATION   // Space between symbols in the same letter")
    print("#define LETTER_SPACE (3 * DOT_DURATION)   // Space between letters")
    print("#define WORD_SPACE (7 * DOT_DURATION)   // Space between words")
    print("#define MESSAGE_DELAY 30000   // 30-second delay between messages")
    print("#define MORSE_PIN 4   // Define the pin used for Morse code output")
    print("void sendDot() {")
    print("    digitalWrite(MORSE_PIN, HIGH);   // Set the pin high")
    print("    delay(DOT_DURATION);")
    print("    digitalWrite(MORSE_PIN, LOW);   // Set the pin low")
    print("    }")
    print("void sendDash() {")
    print("    digitalWrite(MORSE_PIN, HIGH);   // Set the pin high")
    print("    delay(DASH_DURATION);")
    print("    digitalWrite(MORSE_PIN, LOW);   // Set the pin low")
    print("    }")
    print("void setup() {")
    print("    pinMode(MORSE_PIN, OUTPUT);   // Set the pin as an output")
    print("}")
    print("void loop() {")

    # Convert and print in Morse code format
    message_to_morse(message)
    
    print("    delay(MESSAGE_DELAY);")
    print("}") 

Entering a simple message such as "a b c" produces this output:

 //Enter your message: a b c

#define DOT_DURATION 50   // Duration of a dot in milliseconds

#define DASH_DURATION (3 * DOT_DURATION)   // Duration of a dash

#define SYMBOL_SPACE DOT_DURATION   // Space between symbols in the same letter

#define LETTER_SPACE (3 * DOT_DURATION)   // Space between letters

#define WORD_SPACE (7 * DOT_DURATION)   // Space between words

#define MESSAGE_DELAY 30000   // 30-second delay between messages

#define MORSE_PIN 4   // Define the pin used for Morse code output

void sendDot() {

    digitalWrite(MORSE_PIN, HIGH);   // Set the pin high

    delay(DOT_DURATION);

    digitalWrite(MORSE_PIN, LOW);   // Set the pin low

    }

void sendDash() {

    digitalWrite(MORSE_PIN, HIGH);   // Set the pin high

    delay(DASH_DURATION);

    digitalWrite(MORSE_PIN, LOW);   // Set the pin low

    }

void setup() {

    pinMode(MORSE_PIN, OUTPUT);   // Set the pin as an output

}

void loop() {

    sendDot();

    delay(SYMBOL_SPACE);

    sendDash();

    delay( LETTER_SPACE - SYMBOL_SPACE);

    delay(WORD_SPACE - LETTER_SPACE);

    sendDash();

    delay(SYMBOL_SPACE);

    sendDot();

    delay(SYMBOL_SPACE);

    sendDot();

    delay(SYMBOL_SPACE);

    sendDot();

    delay( LETTER_SPACE - SYMBOL_SPACE);

    delay(WORD_SPACE - LETTER_SPACE);

    sendDash();

    delay(SYMBOL_SPACE);

    sendDot();

    delay(SYMBOL_SPACE);

    sendDash();

    delay(SYMBOL_SPACE);

    sendDot();

    delay( LETTER_SPACE - SYMBOL_SPACE);

    delay(MESSAGE_DELAY);

}

And the Arduino compiler gives this message:

Sketch uses 414 bytes (40%) of program storage space. Maximum is 1024 bytes.

Global variables use 4 bytes (6%) of dynamic memory, leaving 60 bytes for local variables. Maximum is 64 bytes.