我试图让我的Arduino在莫尔斯电码中眨眼“SOS”作为我的第一个真正的编程项目。
我已经成功完成了这项工作,但是现在我想更有效地编写代码。这是我的版本。如何实现while循环以使S闪烁两次?这似乎使用计数器跟踪迭代变得复杂。
int led = 13;
//yj Integer representing dit (morse ' . ')
int dit = 500;
//yj Integer representing dah (morse ' - '>)
//yj Testing a multiple of another constant integer to make the timing scalable.
int dah = dit*3;
int blankTime = 100;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dit); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dit); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dit); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dah); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dah); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dah); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dit); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dit); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(dit); // wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(blankTime); // wait
}
//TODO:
//Loop counter implementation for multiples (SOS = 3 dit, 3 dah, 3 dit.)
答案 0 :(得分:0)
我建议使用一个简单的函数来生成莫尔斯代码而不是一次又一次地编写交互。它可以说是这样的。
GenerateMorseCode(char MorseArray [],int len);
您可以使用dit和dah的某些标识符填充MorseArray:可以使用点(。)和hipen( - )。所以3 dit,3dah和3 dit。你的数组将是
...---...
数组的长度为9.现在在GenerateMorseCode函数中,你需要做的就是解析MorseArray。你可以使用像这样的简单开关盒
GenerateMorseCode(char MorseArray[], int len)
{
for(int i =0i<len;i++)
{
switch(MorseArray[i])
{
case '.':
//Your led logic in case of dot
break;
case '-':
//Your led logic in case of dah
break;
}
}
}
您还可以将此函数包装在另一个函数中,该函数按照指定的循环继续调用GenerateMorseCode函数。要生成MorseArray,您可以使用摩尔斯电码创建char数组的映射。您可以继续添加新的枚举并更新相应的Morsemap。像这样的东西
#define MAX_ELEM 12
enum MorseCode
{
A_M = 0,
B_M,
C_M,
SOS_M
};
typedef struct MorseMap
{
char array[MAX_ELEM];
};
MorseMap mm[3] = {{'.','-','\0'},{'-','.','.','.','\0'},{'-','.','-','.','\0'} };
现在也不需要长度,它可以从char数组计算。要访问任何摩尔斯电码,请使用mm [A_M]或mm [SOS_M]
GenerateMorseCode(char arr[])
{
int len = strlen(arry);
//Above for loop and switch case case
}
答案 1 :(得分:0)
您想要一种方法来表示“破折号”和一种表示“点”的方法。 以下是使用Uno上的第13号引脚的示例:http://www.devfactor.net/2014/09/22/arduino-made-easy-output-messages-in-morse-code/
/*
Morse SOS
*/
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
for (int a = 0; a < 3; a++) {
dot();
}
for (int b = 0; b < 3; b++) {
dash();
}
for (int c = 0; c < 3; c++) {
dot();
}
}
void dot()
{
digitalWrite(13, HIGH);
delay(250);
digitalWrite(13, LOW);
delay(250);
}
void dash()
{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(250);
}
请注意,dash()和dot()方法使重复更加优雅,并且需要的代码少于对每个短划线或点进行硬编码。