好吧,在过去的几个小时里,我试图用 arduino 制作一把锁
-有4个按钮,每个按钮都有数字(1,2,3,4) -您有 8 个代码空间
所以我从更改密码的函数开始。 (b1s ....是按钮状态)
问题是,有没有比将数组的每个部分都转换为 int 更容易的方法?感谢您有一个愉快的一天!
int passwordCreate() {
int x1[9];
int x2[9];
int x3[9];
int x4[9];
int a=9;
int c=0;
int space[a];
while(c < a) {
if(b1s==HIGH) {
x1[c]==c+1;
x2[c]==0;
x3[c]==0;
x4[c]==0;
c++;
}
else if(b2s==HIGH) {
x1[c]==0;
x2[c]==c+1;
x3[c]==0;
x4[c]==0;
c++;
}
else if(b3s==HIGH) {
x1[c]==0;
x2[c]==0;
x3[c]==c+1;
x4[c]==0;
c++;
}
else if(b4s==HIGH) {
x1[c]==0;
x2[c]==0;
x3[c]==0;
x4[c]==c+1;
c++;
}
}
最佳答案
更优雅的代码是:
int main() {
int x[4][9];
//...
passwordCreate(x);
//...
return(0);
}
void passwordCreate(int x[4][9]) {
int i;
int a=9;
int c=0;
int space[a];
while(c < a) {
for (i=0; i<4; i++)x[i][c]= 0;
if (b1s==HIGH) x[0][c]= ++c;
else if(b2s==HIGH) x[1][c]= ++c;
else if(b3s==HIGH) x[2][c]= ++c;
else if(b4s==HIGH) x[3][c]= ++c;
}
}
(并使用 =
进行赋值,而不是 ==
。)
(编辑:显示如何调用和返回数组。)
注意:如果所有 bIs 都很低,则会出现无限循环。
注意:任何时候只有一个 bIs 可能为高(任何下一个为高的 bIs 将被忽略)
关于c++ - 挑战 - Arduino 返回多个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30575610/