1

我正试图让我的 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.)
4

2 回答 2

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() 方法使重复更加优雅 - 并且比硬编码每个破折号或点所需的代码更少。

于 2014-09-23T18:07:40.703 回答
0

我建议使用一个简单的函数来生成莫尔斯电码,而不是一次又一次地编写交互。它可以是这样的。

GenerateMorseCode(char MorseArray[], int len);

您可以用一些标识符来填充 MorseArray,以表示 dit 和 dah:也许您可以使用点 (.) 和 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 
}
于 2013-05-16T11:46:10.123 回答