使用Arduino制作摩尔斯电码收发器

摩尔斯电码通过不同的排列顺序来表达不同的英文字母、数字和标点符号等。在今天,国际摩尔斯电码依然被使用着。比如,摩尔斯电码最广为人知的用法发送求救信号SOS,SOS信号的组合方式为:

。再比如,假设我们通过摩尔斯电码发送“Arduino”,组合方式为:“.- .-. -.. ..- .. -. —”。
如何使用Arduino制作摩尔斯电码收发器
电路部分
Arduino到面包板的引脚说明:


- 引脚D2连接到按钮1的一端,再通过电阻接地,按钮1另一端接5V。
- 引脚D7连接到按钮2的一端,再通过电阻接地,按钮2另一端接5V。
- 引脚D8通过电阻连接到LED正极,负极接地。
- 引脚D12通过电阻与蜂鸣器正极连接,负极接地。
代码部分
文末是完整的代码,将其保存为MorseCode.ino,然后打开Arduino IDE “File->Open->MorseCode.ino”,上传到Arduino。上传完成后,打开串口监视器,你将看到如下的显示内容:

摩尔斯码译码器的操作顺序:首先通过点击按钮1和按钮2来写摩尔斯码;字母之间的空格,可在上面的输入框中填2,然后按回车键。单词之间的空格,在上面的输入框中填3,然后按回车键。所有输入完成后,在上面的输入框中填1,然后按回车键,将会翻译摩尔斯码的内容。翻译后的莫尔斯电码将显示在串口监视器的下面。例如:我们来写“.- .-. -.. ..- .. -. —”,翻译后将显示为“ARDUINO”。

这样,使用Arduino制作的摩尔斯码收发器就算完成了。
完整Arduino代码如下:
/*
This Program is for demonstration of MORSE CODE Communication
which was use to send information secretly using codes of combinations dots . and dashes -
Thanks to open source community */ #define SIZE 26
const int ledPin = 8;
const int speakerPin = 12;
const int dotButton = 2;
const int dashButton = 7; String morseCode = "";
String text = "";
int characterAscii = 0;
int startPos = 0, endPos = 0;
int startPos1 = 0, endPos1 = 0;
String characterCode = "";
int dashButtonState = 0;
int dotButtonState = 0; //Array of MorseCode for letters of English Language A to Z
String letters[SIZE] = { // A to I
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
// J to R
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
// S to Z
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."
}; void setup() {
// put your setup code here, to run once:
//Getting things Ready
pinMode(ledPin, OUTPUT);
pinMode(dotButton, INPUT);
pinMode(dashButton, INPUT);
Serial.begin(9600); Serial.println("*************************************************************");
Serial.println(" Demonstration of Morse Code ");
Serial.println("************************************************************* ");
Serial.println("\nInstructions");
Serial.println("1. First Write Your Morse code");
Serial.println("2. When you are done Write 1 on above input box and Press Enter or click Send Button ");
Serial.println("3. For Space between letters write 2 and Press Enter ");
Serial.println("4. For Space between words write 3 and Press Enter "); Serial.println("5. Thats all Translation of Morse Code will then be Shown "); Serial.println("\n\nEnter Your Morse Code Here ");
} void loop() {
// put your main code here, to run repeatedly: while (Serial.available() > 0) {
int ascii = Serial.read(); switch (ascii) {
case 49: // 49 is Ascii value of 1 Serial.print("\n");
morseCode.concat('#'); // Placeing # at the end of morseCode to simplify further processing Serial.print("\nYour Morse code Translation : "); endPos1 = morseCode.indexOf('#'); while (endPos1 < morseCode.length()) {
extractLetters(morseCode.substring(startPos1, endPos1)); // This function would extract Letter as name suggest and would convert code to text SIMPLE!
startPos1 = endPos1 + 1;
if (startPos1 == morseCode.length()) {
break;
}
endPos1 = morseCode.indexOf('#', startPos1);
}
startPos1 = 0;
endPos1 = 0; text = ""; // For New Translation
morseCode = "";
Serial.println("\n\nEnter Your Morse Code Here "); break; case 50: // 50 is Ascii value of 2 morseCode.concat("@");
Serial.print("@");
delay(200); break; case 51: // 51 is Ascii value of 3 morseCode.concat("#");
Serial.print("#");
delay(200); break;
}
} process();
} void turnONLedSpeaker(int du) {
//Turn ON LED
digitalWrite(ledPin, HIGH);
tone(speakerPin, 4699, du); // tone(speakerPin, frequency, duration in milliSec)
} void process() { dotButtonState = digitalRead(dotButton);
dashButtonState = digitalRead(dashButton); if (dashButtonState == HIGH) {
turnONLedSpeaker(400); morseCode.concat("-"); // Storing code in variable morseCode with the help of concatenation function
Serial.print("-"); //Prints User entered Code
delay(200);
} else if (dotButtonState == HIGH) {
turnONLedSpeaker(300); morseCode.concat(".");
Serial.print(".");
delay(200); } else {
//Turn OFF LED
digitalWrite(ledPin, LOW);
}
} char convertIntoText(String characterCode) {
characterAscii = 65; for (int index = 0; index < SIZE; index++) {
if (characterCode == letters[index]) {
return characterAscii;
}
characterAscii++;
}
} void extractLetters(String words) {
words.concat('@'); // Placeing @ at the end of word to simplify further processing endPos = words.indexOf('@'); //Loop to extracting single character morse Code from string of word
while (endPos < words.length()) {
characterCode = words.substring(startPos, endPos); //Now CharacterCode will now convert in text text.concat(convertIntoText(characterCode)); startPos = endPos + 1;
characterCode = ""; // if condition is just to terminate loop when our extracting single character code is complete thats all
if (startPos == words.length()) {
break;
} endPos = words.indexOf('@', startPos);
} Serial.print(text);
Serial.print(" ");
startPos = 0;
endPos = 0;
text = "";
}
使用Arduino制作摩尔斯电码收发器的更多相关文章
- 摩尔斯电码(Morse Code)Csharp实现
摩尔斯电码,在早期的"生产斗争"生活中,扮演了很重要的角色,作为一种信息编码标准,摩尔斯电码拥有其他编码方案无法超越的长久生命.摩尔斯电码在海事通讯中被作为国际标准一直使用到199 ...
- 算法提高 9-3摩尔斯电码 map
算法提高 9-3摩尔斯电码 时间限制:1.0s 内存限制:256.0MB 问题描述 摩尔斯电码破译.类似于乔林教材第213页的例6.5,要求输入摩尔斯码,返回英文.请不要使用"z ...
- 算法笔记_085:蓝桥杯练习 9-3摩尔斯电码(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 摩尔斯电码破译.类似于乔林教材第213页的例6.5,要求输入摩尔斯码,返回英文.请不要使用"zylib.h",只能使用 ...
- Java实现 蓝桥杯 算法提高 摩尔斯电码
算法提高 9-3摩尔斯电码 时间限制:1.0s 内存限制:256.0MB 提交此题 问题描述 摩尔斯电码破译.类似于乔林教材第213页的例6.5,要求输入摩尔斯码,返回英文.请不要使用"zy ...
- 蓝桥杯 算法提高 9-3摩尔斯电码 _c++ Map容器用法
//****|*|*-**|*-**|--- #include <iostream> #include <map> #include <vector> #inclu ...
- [CTF]摩斯电码
摩尔斯电码 -----------转载 https://morse.supfree.net/ 摩尔斯电码定义了包括:英文字母A-Z(无大小写区分)十进制数字0-9,以及"?"&qu ...
- [Swift]LeetCode804. 唯一摩尔斯密码词 | Unique Morse Code Words
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- 力扣(LeetCode)804. 唯一摩尔斯密码词
国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: "a" 对应 ".-", "b" 对应 &q ...
- python实现的摩斯电码解码\编码器
代码地址如下:http://www.demodashi.com/demo/14743.html 详细说明: 现在这年头谍战片.警匪片动不动就用摩斯密码来传递信息,一方面可以用来耍帅,另外一方面好像不插 ...
- Leetcode804.Unique Morse Code Words唯一摩尔斯密码词
国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: "a" 对应 ".-", "b" 对应 &q ...
随机推荐
- C++容器(vector、deque、list、map)
(1) vector:将元素置于一个动态数组中,可以随机存储元素(也就是用索引直接存取). 数组尾部添加或删除元素非常迅速.但在中部或头部就比较费时. *代码演示:* 取: at在下标越界时会抛出异常 ...
- 从零开始整SpringBoot-工具与插件
工具 工具 名称 地址 IDEA https://www.jetbrains.com/idea/ JDK1.8 https://www.oracle.com/java/technologies/jav ...
- Java使用joml计算机图形学库,将3D坐标旋转正交投影转为2D坐标
最近遇到了一个困扰我许久的难题,现将解决方案分享出来 由于我们的项目侧重点在前端绘图,导致了前后端工作量不协调,我后端接口很快就能写完,而前端一个图要画好久,领导见状将前端的任务分到后端一部分用Jav ...
- PostgreSQL 12 文档: 部分 VI. 参考
部分 VI. 参考 这份参考中的条目意欲提供关于相应主题的权威.完整和正式的总结.关于使用PostgreSQL的更多信息(以叙述.教程或例子的形式)可以在本书的其他部分找到.见每个参考页面上列出的交叉 ...
- PostgreSQL 12 文档: 部分 V. 服务器编程
部分 V. 服务器编程 这部分关于使用用户定义的函数.数据类型.触发器等扩展服务器功能.这些是高级主题,读者应该在理解了有关PostgreSQL的所有其他用户文档之后才阅读这些主题.这一部分的后面一些 ...
- Jupyter无法运行websocket错误
Juputer无法连接 系websocket连接出问题,浏览器控制台报错 ws 连接异常 WebSocket connection to 'ws://localhost:8888/api/kernel ...
- Android BottomNavigation底部导航栏使用
原文地址: Android BottomNavigation底部导航栏使用 - Stars-One的杂货小窝 基本使用 本文侧重点记录一些特殊的样式设置,所以基本使用这里就简单概述一下,详细图文可以去 ...
- 【Python】万字长文,Locust 性能测试指北(上)
Locust Locust 是比较常见的性能测试工具,底层基于 gevent.官方介绍 它是一款易于使用.可编写脚本且可扩展的性能测试工具,可以让我们使用常规 Python 代码定义用户的行为,而不必 ...
- 2023-07-12:RocketMQ如何做到消息不丢失?
2023-07-12:RocketMQ如何做到消息不丢失? 答案2023-07-12: RocketMQ通过刷盘机制.消息拉取机制和ACK机制等多种方式来确保消息投递的可靠性,防止消息丢失. 1.刷盘 ...
- AIGC:新AI时代,推动数字人进化的引擎
摘要:CV.NLP.大模型...AI技术的加持下,让数字人内外在更加生动真实.在未来的发展中,数字人的应用场景越来越广泛,并将发挥出重要的作用,让美好照进生活. 本文分享自华为云社区<AIGC: ...