LED 闪烁实验(3)–完整摩斯密码

摩尔斯电码(又译为摩斯密码,Morse code)是一种时通时断的信号代码,通过不同的排列顺序来表达不同的英文字母、数字和标点符号。它发明于1837年,发明者有争议,是美国人塞缪尔·莫尔斯或者艾尔菲德·维尔。 摩尔斯电码是一种早期的数字化通信形式,但是它不同于现代只使用零和一两种状态的二进制代码,它的代码包括五种: 点、划、点和划之间的停顿、每个字符间短的停顿(在点和划之间)、每个词之间中等的停顿以及句子之间长的停顿。

由于之前弄了一个S.O.S的摩斯密码,我突发奇想,想把我们常用的英语转换成摩斯密码来使用。使用上面的连接即可。

摩斯密码

摩尔斯电码由两种基本信号组成:短促的点信号“·”,读“滴”;保持一定时间的长信号“—”,读“嗒”。间隔时间:滴=1t,嗒=3t,滴嗒间=1t,字符间=3t,单词间=7t。

代码如下:

int ledPin = 13;
String comdata="";
int t = 150;

void setup()
{

	//设置数字引脚模式为输出
	pinMode(ledPin, OUTPUT);
	//设置通信频率
	Serial.begin(9600);

}

void loop()
{
	while (Serial.available() > 0)
	{
		comdata += char(Serial.read());
		delay(2);
	}
	for (int i=0; i < comdata.length(); i++) 
        { 
           if (comdata[i] == ' ') 
             { 
               delay(4* t); 
             } 
           morseCode(comdata[i]); 
           Serial.println(comdata[i]); } //这里可以删除,方便调试用
       if (comdata.length() > 0)
	{
		Serial.println(comdata);  //这句也可以删除,为了调试用
		comdata = "";
	}

}

void morseCode(char char1) 
{
	switch (char1)
	{
	case 0:
		//-----
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3 * t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3 * t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3 * t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3 * t);
		digitalWrite(ledPin, LOW);
		delay(3*t);
		break;
	case 1:
		//.----
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3 * t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3 * t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3 * t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3 * t);
		digitalWrite(ledPin, LOW);
		delay(3*t);
		break;
	case 2:
		//..---
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3 * t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3 * t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3 * t);
		digitalWrite(ledPin, LOW);
		delay(3*t);
		break;
	case 3:
		//...--
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3 * t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3 * t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 4:
		//....-
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3 * t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 5:
		//.....
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 6:
		//-....
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 7:
		//--...
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 8:
		//---..
		digitalWrite(ledPin, HIGH);
		delay(3 * t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3 * t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 9:
		//----.
		digitalWrite(ledPin, HIGH);
		delay(3 * t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3 * t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 'a':
		//.-
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3 * t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 'b':
		//-...
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay( t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 'c':
		//-.-.
		digitalWrite(ledPin, HIGH);
		delay(3 * t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 'd':
		//-..
		digitalWrite(ledPin, HIGH);
		delay(3 * t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 'e':
		//.
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 'f':
		//..-.
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3 * t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 'g':
		//--.
		digitalWrite(ledPin, HIGH);
		delay(3 * t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 'h':
		//....
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 'i':
		//....
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 'j':
		//.---
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 'k':
		//-.-
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 'l':
		//.-..
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 'm':
		//--
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 'n':
		//-.
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 'o':
		//---
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 'p':
		//.--.
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 'q':
		//--.-
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 'r':
		//.-.
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 's':
		//...
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 't':
		//-
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 'u':
		//..-
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 'v':
		//...-
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 'w':
		//.--
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 'x':
		//-..-
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 'y':
		//-.--
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;
	case 'z':
		//--..
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(3*t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(t);
		digitalWrite(ledPin, HIGH);
		delay(t);
		digitalWrite(ledPin, LOW);
		delay(3 * t);
		break;

	default:
		break;
	}
}

String Comdata() 
{
	String comdata1;
	while (Serial.available()>0)
	{
		comdata += Serial.read();
		delay(2);
	}
	if (comdata.length() > 0)
	{
		Serial.println(comdata);
		comdata = "";
	}
	return comdata;
}
0 Comments
Leave a Reply