[英]Correct use of syntax and functions parsing characters?
没有任何 Arduino 网络实用程序允许我在没有单元的情况下编译代码,而且它正在开发中(由于 COVID,严重延迟)。 但我等不及了,我只需要调整我的编码并尝试启动并运行一些东西。 有人可以告诉我,这行得通吗?
我对 C 和 Java 有点熟悉,所以这看起来不错,但是如果有问题,有人可以告诉我。 干杯。
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* MORSE FLASHER *
* *
* Author: *
* Version: 1.0 (26 July 2021) *
* *
* Parses an array of letters and numbers into Morse Code, which *
* are relayed via LED flashes with customisable time intervals. *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int led = 13; // The pin address for the LED.
int ditsLength = 150; // The length of a short “dit” flash.
int dahsLength = 450; // The length of a long “dah” flash.
int partDelay = 100; // The delay between pips for a character.
int charDelay = 300; // The delay between characters.
int wordDelay = 700; // The delay between words.
/**** The string to parse into Morse Code. */
char message[] = "Hello world this is Morse Code";
/*===== Flash a “dit” on the LED.
\*=============================*/
void dit()
{
digitalWrite(led, HIGH);
delay(ditsLength);
digitalWrite(led, LOW);
delay(partDelay);
}
/*===== Flash a “dah” on the LED.
\*=============================*/
void dah()
{
digitalWrite(led, HIGH);
delay(dahsLength);
digitalWrite(led, LOW);
delay(partDelay);
}
/*===== Pause between the end of a character and a new one.
\*=======================================================*/
void pause()
{
delay(charDelay);
}
/*===== Pause between the end of a word and a new one.
\*==================================================*/
void finish()
{
delay(wordDelay);
}
/*===== Break a character down into “dits” and “dahs”.
\*==================================================*/
void parseChar(char c)
{
if (c == ‘A’ || c == ‘a’)
{
dit(); dah(); pause();
}
else if (c == ‘B’ || c == ‘b’)
{
dah(); dit(); dit(); dit(); pause();
}
else if (c == ‘C’ || c == ‘c’)
{
dah(); dit(); dah(); dit(); pause();
}
else if (c == ‘D’ || c == ‘d’)
{
dah(); dit(); dit(); pause();
}
else if (c == ‘E’ || c == ‘e’)
{
dit(); pause();
}
else if (c == ‘F’ || c == ‘f’)
{
dit(); dit(); dah(); dit(); pause();
}
else if (c == ‘G’ || c == ‘g’)
{
dah(); dah(); dit(); pause();
}
else if (c == ‘H’ || c == ‘h’)
{
dit(); dit(); dit(); dit(); pause();
}
else if (c == ‘I’ || c == ‘i’)
{
dit(); dit(); pause();
}
else if (c == ‘J’ || c == ‘j’)
{
dit(); dah(); dah(); dah(); pause();
}
else if (c == ‘K’ || c == ‘k’)
{
dah(); dit(); dah(); pause();
}
else if (c == ‘L’ || c == ‘l’)
{
dit(); dah(); dit(); dit(); pause();
}
else if (c == ‘M’ || c == ‘m’)
{
dah(); dah(); pause();
}
else if (c == ‘N’ || c == ‘n’)
{
dah(); dit(); pause();
}
else if (c == ‘O’ || c == ‘o’)
{
dah(); dah(); dah(); pause();
}
else if (c == ‘P’ || c == ‘p’)
{
dit(); dah(); dah(); dit(); pause();
}
else if (c == ‘Q’ || c == ‘q’)
{
dah(); dah(); dit(); dah(); pause();
}
else if (c == ‘R’ || c == ‘r’)
{
dit(); dah(); dit(); pause();
}
else if (c == ‘S’ || c == ‘s’)
{
dit(); dit(); dit(); pause();
}
else if (c == ‘T’ || c == ‘t’)
{
dah(); pause();
}
else if (c == ‘U’ || c == ‘u’)
{
dit(); dit(); dah(); pause();
}
else if (c == ‘V’ || c == ‘v’)
{
dit(); dit(); dit(); dah(); pause();
}
else if (c == ‘W’ || c == ‘w’)
{
dit(); dah(); dah(); pause();
}
else if (c == ‘X’ || c == ‘x’)
{
dah(); dit(); dit(); dah(); pause();
}
else if (c == ‘Y’ || c == ‘y’)
{
dah(); dit(); dah(); dah(); pause();
}
else if (c == ‘Z’ || c == ‘z’)
{
dah(); dah(); dit(); dit(); pause();
}
else if (c == ‘1’)
{
dit(); dah(); dah(); dah(); dah(); pause();
}
else if (c == ‘2’)
{
dit(); dit(); dah(); dah(); dah(); pause();
}
else if (c == ‘3’)
{
dit(); dit(); dit(); dah(); dah(); pause();
}
else if (c == ‘4’)
{
dit(); dit(); dit(); dit(); dah(); pause();
}
else if (c == ‘5’)
{
dit(); dit(); dit(); dit(); dit(); pause();
}
else if (c == ‘6’)
{
dah(); dit(); dit(); dit(); dit(); pause();
}
else if (c == ‘7’)
{
dah(); dah(); dit(); dit(); dit(); pause();
}
else if (c == ‘8’)
{
dah(); dah(); dah(); dit(); dit(); pause();
}
else if (c == ‘9’)
{
dah(); dah(); dah(); dah(); dit(); pause();
}
else if (c == ‘0’)
{
dah(); dah(); dah(); dah(); dah(); pause();
}
else
{
/**** Treat the character as a “space”. */
finish();
}
}
/*===== Parse an array of characters into Morse Code pips.
\*======================================================*/
void parse(char[] toParse)
{
/**** Step through each character and parse it. */
for (int i = 0; i < sizeof(toParse); i++)
{
parseChar(toParse[i]);
}
}
/*===== Declare all variables.
\*==========================*/
void start()
{
/**** Set up the LED. */
pinMode(led, OUTPUT);
}
/*===== Run the Morse Flasher.
\*==========================*/
void loop()
{
parse(message[]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.