使用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 ...
随机推荐
- JIRA安装
JIRA安装 操作系统: 阿里云centos6.8 域名: yan.jzhsc.com 1.安装与配置JAVA sudo -u root -H bash # 在oracle官网下载JDK,安装并配置环 ...
- spring连接数据库mysql报错 state 08S01 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
spring连接数据库mysql报错errorCode0,state08S01com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Comm ...
- 前端vue基于原生check增强单选多选插件
前端vue基于原生check增强单选多选插件, 下载完整代码请访问uni-app插件市场地址:https://ext.dcloud.net.cn/plugin?id=12979 效果图如下: ...
- 【大数据OLAP技术新书推荐】 字节跳动、阿里巴巴大厂资深架构师程序员多年实践经验总结《ClickHouse入门、实战与进阶》
ClickHouse 领域集大成之作-ClickHouse 入门进阶实战的标准参考书-日常工作案头必备! 如果需要购买阅读的话,可以点击: https://item.jd.com/1007763561 ...
- React学习时,outlet配置(token判定,页面path监听)
尽管写过 outlet 路由的配置. 考虑到 token 判定和 路由页 变更,我不了解v6是不是有更详解的做法. 决定调一下配置,期望 在任何页面异步更新时,token 都可以在跳转前 被检测到,防 ...
- 【WALT】WALT入口 update_task_ravg() 代码详解
目录 [WALT]WALT入口 update_task_ravg() 代码详解 代码展示 代码逻辑 ⑴ 判断是否进入 WALT 算法 ⑵ 获取 WALT 算法中上一个窗口的开始时间 ⑶ 如果任务刚初始 ...
- Android 自定义view中根据状态修改drawable图片
原文地址:Android 自定义view中根据状态修改drawable图片 - Stars-One的杂货小窝 本文涉及知识点: Android里的selector图片使用 底部导航栏的使用 自定义vi ...
- 图扑 AR 技术应用与管理:施工建造、机柜扫描、办公室导航解决方案
随着科技的不断革新和创新,越来越多的行业开始迎来数字化时代的变革.建筑行业作为人类历史上最重要的产业之一,在数字化转型方面同样也在不断推进.图扑软件结合 AR 技术的应用,为建筑行业带来了更加便捷高效 ...
- FlutterWeb部署到服务器
目标:把flutter web项目部署到自己的服务器上,可以使用自己的服务器IP访问 前提:服务器已经安装了nginx, 这是我的flutter配置 edz@lwqdeMacBook-Pro ~ % ...
- 【Mybatis】动态SQL
目录 动态SQL if语句 动态SQL if+where语句 动态SQL if+set语句 动态SQL choose(when,otherwise)语句 动态SQL trim语句 动态SQL SQL片 ...