假设我想创建允许用户在 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 这个功能?