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

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

电路部分

Arduino到面包板的引脚说明:

  1. 引脚D2连接到按钮1的一端,再通过电阻接地,按钮1另一端接5V。
  2. 引脚D7连接到按钮2的一端,再通过电阻接地,按钮2另一端接5V。
  3. 引脚D8通过电阻连接到LED正极,负极接地。
  4. 引脚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制作摩尔斯电码收发器的更多相关文章

  1. 摩尔斯电码(Morse Code)Csharp实现

    摩尔斯电码,在早期的"生产斗争"生活中,扮演了很重要的角色,作为一种信息编码标准,摩尔斯电码拥有其他编码方案无法超越的长久生命.摩尔斯电码在海事通讯中被作为国际标准一直使用到199 ...

  2. 算法提高 9-3摩尔斯电码 map

    算法提高 9-3摩尔斯电码 时间限制:1.0s   内存限制:256.0MB     问题描述 摩尔斯电码破译.类似于乔林教材第213页的例6.5,要求输入摩尔斯码,返回英文.请不要使用"z ...

  3. 算法笔记_085:蓝桥杯练习 9-3摩尔斯电码(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 摩尔斯电码破译.类似于乔林教材第213页的例6.5,要求输入摩尔斯码,返回英文.请不要使用"zylib.h",只能使用 ...

  4. Java实现 蓝桥杯 算法提高 摩尔斯电码

    算法提高 9-3摩尔斯电码 时间限制:1.0s 内存限制:256.0MB 提交此题 问题描述 摩尔斯电码破译.类似于乔林教材第213页的例6.5,要求输入摩尔斯码,返回英文.请不要使用"zy ...

  5. 蓝桥杯 算法提高 9-3摩尔斯电码 _c++ Map容器用法

    //****|*|*-**|*-**|--- #include <iostream> #include <map> #include <vector> #inclu ...

  6. [CTF]摩斯电码

    摩尔斯电码 -----------转载 https://morse.supfree.net/ 摩尔斯电码定义了包括:英文字母A-Z(无大小写区分)十进制数字0-9,以及"?"&qu ...

  7. [Swift]LeetCode804. 唯一摩尔斯密码词 | Unique Morse Code Words

    International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...

  8. 力扣(LeetCode)804. 唯一摩尔斯密码词

    国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: "a" 对应 ".-", "b" 对应 &q ...

  9. python实现的摩斯电码解码\编码器

    代码地址如下:http://www.demodashi.com/demo/14743.html 详细说明: 现在这年头谍战片.警匪片动不动就用摩斯密码来传递信息,一方面可以用来耍帅,另外一方面好像不插 ...

  10. Leetcode804.Unique Morse Code Words唯一摩尔斯密码词

    国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: "a" 对应 ".-", "b" 对应 &q ...

随机推荐

  1. 前端仿京东、天猫带搜索历史搜索栏搜索框searchBar

    快速实现前端仿京东.天猫带搜索历史搜索栏搜索框searchBar, 使用简单便利, 请访问uni-app插件市场地址:https://ext.dcloud.net.cn/plugin?id=12529 ...

  2. [随笔]记一此更新win10后mysql服务消失的问题

    十几天前系统自动更新 没在意 几天前用php连mysql的时候 报错 Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] 由于目标计算 ...

  3. 关于 axios 是什么?以及怎么用?

    〇.前言 Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 Node.js 中.简单的讲就是可以发送 Get.Post 请求. 诸如 Vue.React.Angular 等前 ...

  4. 一文了解 io.Copy 函数

    1. 引言 io.Copy 函数是一个非常好用的函数,能够非常方便得将数据进行拷贝.本文我们将从io.Copy 函数的基本定义出发,讲述其基本使用和实现原理,以及一些注意事项,基于此完成对io.Cop ...

  5. TCP和UDP的应用场景

    传输层的两个协议,TCP(Transmission Control Protocol,传输控制协议)和UDP(User Datagram Protocol,用户数据报协议),有各自的应用场景. TCP ...

  6. Tomcat改jar

    Tomcat改jar 插件修改 <!-- <plugin>--> <!-- <groupId>org.apache.maven.plugins</gro ...

  7. OO第二次大作业

    前言 前言的前言 第二篇blog跟上一篇只隔了将近一个月,但是感觉心境上好像发生了很多的变化,认识到了自己存在的很多不足(可能是菜单折磨的),感觉对很多东西都一知半解,希望在写完这篇总结性blog之后 ...

  8. 你不知道的 HTTP Referer

    前言 上周突然发现自己的自己站点的图片全都403了,之前还是好好的,图片咋就全都访问不了呢?由于我每次发文章都是先发了掘金,然后再从掘金拷贝到我自己的站点,这样我就不用在自己的站点去上传图片了,非常方 ...

  9. python:时间模块dateutil

    安装 pip install python-dateutil dateutil模块主要有两个函数,parser和rrule. 其中parser是根据字符串解析成datetime,而rrule则是根据定 ...

  10. RedHat8静默安装was

    前言 was(websphere application server),类似weblogic.tomcat,由IBM开发的一种企业级Java容器. 系统版本:redhat 8.2 was版本:was ...