0

ユーザーが Android でメッセージを入力し、LED を切り替えて Arduino でモールス符号で表示できるアプリを作成するとします。モールス符号のメッセージは、一連のダッシュ (LONGS) とドット (ショート) で構成されます。これらのダッシュとドットは、正しい時間単位で 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: ダッシュ モールス
LETTER: モールスの文字の終わり WORD: モールスの単語の終わり STOP: モールスの終わり ex: メッセージ「SOS」は (SHORT,SHORT,SHORT としてエンコードされます) ,LETTER,LONG,LONG,LONG,LETTER,SHORT,SHORT,SHORT,LETTER,WORD,STOP)

この機能を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 = short,short,short) に応じて、先ほど示したのと同じ方法で書き込み出力を作成しますが、digitalWrite() を新しいものに変更する必要があります。各文字のモールス符号を作成する関数。そのため、if 条件は文字ごとになります (つまりif (msg[i] == 83)、10 進 ASCII の S)。

于 2015-10-27T10:37:17.233 に答える