注意:不要直视激光头,它会对你的眼睛造成很大的伤害。
注意:不要直视激光头,它会对你的眼睛造成很大的伤害。
注意:不要直视激光头,它会对你的眼睛造成很大的伤害。
OK,今天来讲一个稍微复杂点的实验
关键是摩尔斯密码比较复杂
复杂在我们不太用,或者说我们一般来说记不住,除非你是特工,一般人不会花时间去记
我们用代码实现,就是为了做实验,实现功能就可以了
代码的逻辑可能稍微花点时间和心思
01 摩尔斯密码
字母:
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','N','U','V','W','X','Y','Z'
用摩尔斯密码表示
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.","...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."
数字
'0','1','2','3','4','5','6','7','8','9'
用摩尔斯密码表示
"-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..","----."
其中如果用激光传感器表示,-就是长亮,·就是短亮
比如A为.-那就是
短亮 长亮
比如9为----.
就是
长亮 长亮 长亮 长亮 短亮
比如我的名字首字母
WYF
就是
".--" "-.--" "..-."
就是
短亮 长亮 长亮 停顿 长亮 短亮 长亮 长亮 停顿 短亮 短亮 长亮 短亮
我们收到这样子的光照,就知道是WYF了
这只是举个例子,一般我们生活中不会用到,就是做实验练习用的
02 硬件
● Arduino Uno主板* 1
● USB数据线* 1
● 激光发射器模块* 1
● 面包板* 1
● 9V方型电池* 1
● 跳线若干
激光传感器
连线图
03 代码
//#include "retrieval.h"
/*串口输入A-Z,0-9,激光灯发出相应莫尔斯码*/
#define SIZE 26
const int laserPin=7;
static int dotDelay=200;
static int lineDelay=1000;
int numStore=27;
char LETTERS[26]={
'A','B','C','D','E','F','G','H','I','J',
'K','L','M','N','O','P','Q','R','S','N',
'U','V','W','X','Y','Z'
};
char NUMBERS[10]={
'0','1','2','3','4','5','6','7','8','9'
};
String letters[SIZE]={
// A to I
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
// J to R
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
// S to Z
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."
};
String numbers[10]={
"-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..",
"----."
};
void setup() {
// put your setup code here, to run once:
pinMode(laserPin,OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
char ch=0;
if(Serial.available()>0)
{
ch=Serial.read();
}
Serial.println(ch);
laserLetter(ch);
laserNumber(ch);
}
void laserLetter(char ch)
{
digitalWrite(laserPin,LOW);
for(int i=0;i<26;i++)
{
if(ch==LETTERS[i])
{
numStore=i;
laserLetterBlink(numStore);
}
}
}
void laserNumber(char ch)
{
digitalWrite(laserPin,LOW);
for(int i=0;i<10;i++)
{
if(ch==NUMBERS[i])
{
numStore=i;
laserNumberBlink(numStore);
}
}
}
void laserLetterBlink(int num)
{
String str=letters[numStore];
Serial.println(str);
delay(1000);
Serial.println(numStore);
delay(1000);
int j=0;
j=str.length();
Serial.println(j);
delay(1000);
char ch1[j]={0};
for(int i=0;i<j;i++)
{
ch1[i]=str[i];
if(ch1[i]=='.')
{
digitalWrite(laserPin,HIGH);
delay(dotDelay);
digitalWrite(laserPin,LOW);
delay(1000);
}
if(ch1[i]=='-')
{
digitalWrite(laserPin,HIGH);
delay(lineDelay);
digitalWrite(laserPin,LOW);
delay(1000);
}
}
digitalWrite(laserPin,LOW);
delay(6000);
}
void laserNumberBlink(int num)
{
String str=numbers[numStore];
Serial.println(str);
delay(1000);
Serial.println(numStore);
delay(1000);
int j=0;
j=str.length();
Serial.println(j);
delay(1000);
char ch1[j]={0};
for(int i=0;i<j;i++)
{
ch1[i]=str[i];
if(ch1[i]=='.')
{
digitalWrite(laserPin,HIGH);
delay(dotDelay);
digitalWrite(laserPin,LOW);
delay(1000);
}
if(ch1[i]=='-')
{
digitalWrite(laserPin,HIGH);
delay(lineDelay);
digitalWrite(laserPin,LOW);
delay(1000);
}
}
digitalWrite(laserPin,LOW);
delay(6000);
}
代码讲起来太复杂,我就不讲了,大家自己慢慢学习
我花了一年的时间把arduino用熟练,大家就辛苦下,自己读代码哈
03 运行结果
我们运行的结果就是
我们通过串口输入的字母或者数字,激光传感器发射出相应代码的光线
我们用视频来体现
输入WYF5201314
激光传感器分别输出
".--" "-.--" "..-." "....." "..---" "-----" ".----" "...--" ".----" "....-"
W Y F 5 2 0 1 3 1 4
视频制作是我的短板,在学习,大家多看几次就看懂了哈
我会继续学习视频剪辑的
辛苦大家了
串口显示
W 字母
".--" 摩尔斯密码
22 字母表中的位置
3 3个字符.--
激光传感器实现摩尔斯密码
视频制作确实不够好
大家多看几次,请大家多担待