我正在学习编码Arduino,所以为了做到这一点,我尝试用英语制作摩尔斯电码转换器。这是我的代码,我面临以下问题:
Arduino: 1.8.5 (Windows 10), Board: "Arduino/Genuino Uno"
C:\Users\Kunal Gehlot\OneDrive\Documents\Arduino\Morse_CODE\Morse_CODE.ino: In function 'void setup()':
Morse_CODE:16: error: request for member 'length' in 'in', which is of non-class type 'char [28]'
for (int i = 0; i < in.length; i++) {
^
Morse_CODE:18: error: request for member 'lentgh' in 'in', which is of non-class type 'char [28]'
n = in.lentgh[i];
^
exit status 1
request for member 'length' in 'in', which is of non-class type 'char [28]'
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
这是我的代码:
int Speaker1 = 4;
int Speaker2 = 6;
int LED = 2;
int Relay = 12;
char n = 0;
char in[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
void setup() {
pinMode(Speaker1, OUTPUT);
pinMode(Speaker2, OUTPUT);
pinMode(LED, OUTPUT);
pinMode(Relay, OUTPUT);
for (int i = 0; i < in.length; i++) {
n = in.lentgh[i];
switch (n) {
case 'A' :
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'B' :
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'C' :
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'D' :
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'E' :
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'F' :
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'G' :
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'H' :
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'I' :
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'J' :
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'K' :
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'L' :
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'M' :
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'N' :
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'O' :
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'P':
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'Q':
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'R':
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'S':
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'T':
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'U':
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'V':
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'W':
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'X':
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'Y':
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'Z':
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case ' ' :
delay (700);
break;
}
}
}
void loop() {
}
我这个项目的唯一目标是从串口监视器中取出输入文本并将其分成每个单独的字母,我还要在列表中添加符号并将其转换为LED中的摩尔斯电码,继电器电脉冲和扬声器。
答案 0 :(得分:0)
而不是使用in.lentgh [i],sizeof(in)应该用于for循环条件。
解决了代码:
int Speaker1 = 4;
int Speaker2 = 6;
int LED = 2;
int Relay = 12;
char n = '0';
char in[]= "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
void setup() {
pinMode(Speaker1, OUTPUT);
pinMode(Speaker2, OUTPUT);
pinMode(LED, OUTPUT);
pinMode(Relay, OUTPUT);
for (int i = 0; i < sizeof(in); i++) {
delay (1200);
n = in[i];
switch (n) {
case 'A' :
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'B' :
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'C' :
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'D' :
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'E' :
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'F' :
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'G' :
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'H' :
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'I' :
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'J' :
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'K' :
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'L' :
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'M' :
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'N' :
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'O' :
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'P' :
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'Q' :
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'R' :
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'S' :
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'T' :
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'U' :
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'V' :
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'W' :
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'X' :
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'Y' :
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case 'Z' :
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(300);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(100);
digitalWrite(Speaker1, HIGH);
delay(100);
digitalWrite(Speaker1, LOW);
delay(300);
break;
case ' ' :
delay (700);
break;
}
}
}
void loop() {
}
答案 1 :(得分:0)
正如其他人在评论中指出的那样,你应该使用strlen()来查找Arduino中字符数组的长度。 strlen()是标准Arduino库的一部分,因此您可以使用它而无需添加任何特殊的#include语句。
如果使用sizeof(in),代码将发送缓冲区中的每个字符,即使您感兴趣的字符串小于缓冲区。
例如,假设在[] =“ABC”中; sizeof(in)错误地返回4,因为“ABC”在“C”字符后面有一个隐藏的0字节。 strlen(in)在这种情况下返回3,正确的数字。
所以,
for(int i = 0; i&lt; in.length; i ++){ n = in.lentgh [i];
应该是
for(int i = 0; i&lt; strlen(in); i ++){ n =在[i];