0

假设我想创建允许用户在 Android 上输入消息并通过切换 LED 在 Arduino 上以摩尔斯电码显示的应用程序。摩尔斯电码中的消息由一系列破折号(长)和点(短)组成。这些破折号和圆点可以通过打开 LED 来显示正确的时间单位数。

#include <Usb.h>
#include <AndroidAccessory.h>
#define  LED_PIN  13

#define SHORT 0
#define LONG 1
#define LETTER 2
#define WORD 3
#define STOP 4

#define UNIT 250

AndroidAccessory acc("testing",
        "morse_code",
        "CoolAccessory",
        "1.0",
        "http://www.example.com/CoolAccessory",
                "0000000012345678");
void setup()
{
  // set communiation speed
  Serial.begin(115200);
  pinMode(LED_PIN, OUTPUT);
  acc.powerOn();
}

void loop()
{
  byte msg[125];
  if (acc.isConnected()) {
    int len = acc.read(msg, sizeof(msg), 1); // read data into msg variable
    if (len > 0) { // Only do something if a message has been received.
        displayMorseCode(msg, len);

    }
  } 
  else
    digitalWrite(LED_PIN , LOW); // turn off light
}

//For toggle the LED for certain length of time use the delay() 
//call delay(UNIT) to pause execution of UNIT milliseconds
//long unit *3 , short = unit
void displayMorseCode(byte* msg, int len) {

  // TODO :Interpret the message toggle LED on and off to display the 
           morse code
 if (msg[0] == 1) 
    digitalWrite(LED_PIN,HIGH); 
  else
    digitalWrite(LED_PIN,LOW); 

}  

该消息由以下值组成,这些值已定义为常量:

SHORT:莫尔斯的一个点
LONG:一个破折号 morse
LETTER:莫尔斯字母的结尾 WORD:莫尔斯单词的结尾 STOP:莫尔斯的结尾 ex:消息“SOS”编码为 (SHORT,SHORT,SHORT ,字母,长,长,长,字母,短,短,短,字母,单词,停止)

如何实现 displayMorseCode 这个功能?

4

1 回答 1

0
//For toggle the LED for certain length of time use the delay() 
//call delay(UNIT) to pause execution of UNIT milliseconds
//long unit *3 , short = unit
void displayMorseCode(byte* msg, int len) {
int delayT = 0;
int unit = 300;
  // TODO :Interpret the message toggle LED on and off to display the 
           morse code
for (int i = 0 ; i< len; i++)
{
if (msg[i] == 1) {
    digitalWrite(LED_PIN,HIGH); 
    delayT = 3*unit;
}
else {
    digitalWrite(LED_PIN,LOW); 
    delayT = unit;
}

delay(delayT);

} 

这是一个非常简单的答案,它将根据接收到的字节改变持续时间。现在您必须为每个字节创建一个字典,以便根据字节(即 S = 短、短、短)创建写入输出,就像我向您展示的那样,但您应该将 digitalWrite() 更改为新的函数,它将创建每个字母的莫尔斯电码。所以 if 条件将适用于每个字母(即if (msg[i] == 83)- 十进制 ASCII 中的 S)

于 2015-10-27T10:37:17.233 回答