使用arduino处理代码项目,但事情似乎不起作用,我不确定原因。我打算使用16键键盘上的A-D按钮来更改一个名为activeSlot的变量,然后更改数字键在按下时写入的实际插槽。不过,事情似乎不起作用,我也不确定出了什么问题。
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'D','C','B','A'},
{'#','9','6','3'},
{'0','8','5','2'},
{'*','7','4','1'}
};
byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8, 9}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
基本键盘设置
//the amount of energy in each slot
int slotA = 0;
int slotB = 0;
int slotC = 0;
int slotD = 0;
//the amount of energy in the other slots, set to 0 at the beginning
int totalA = 0;
int totalB = 0;
int totalC = 0;
int totalD = 0;
//the total allocated energy, used for lighting the correct number of LEDs
int totalE = 0;
int activeSlot[] = {0};
int x = activeSlot[0];
整数的设置,所有从0开始,应在循环部分中更改
void setup() {
// put your setup code here, to run once:
//pinMode(2,INPUT);
//pinMode(3,INPUT);
//pinMode(4,INPUT);
//pinMode(5,INPUT);
//pinMode(6,INPUT);
//pinMode(7,INPUT);
//pinMode(8,INPUT);
//pinMode(9,INPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
pinMode(14,OUTPUT);
pinMode(15,OUTPUT);
pinMode(16,OUTPUT);
pinMode(17,OUTPUT);
pinMode(18,OUTPUT);
pinMode(19,OUTPUT);
Serial.begin(9600);
}
配置引脚
void loop() {
// put your main code here, to run repeatedly:
char key = keypad.getKey();
//the amount of energy in the other slots, set to 0 at the beginning
int totalA = 10 - (slotB + slotC + slotD);
int totalB = 10 - (slotA + slotC + slotD);
int totalC = 10 - (slotA + slotB + slotD);
int totalD = 10 - (slotA + slotB + slotC);
应该有10个能量/点可以分配给任何插槽。totalA是起始的10-每个其他插槽中的能量。如果剩余的能量比你按下该插槽的数字键少,那么它不应该改变变量。
// section for active slot keypress actions
if (key == 'A'){activeSlot[0] = 1;
Serial.println(x = activeSlot[0]);
}
if (key == 'B'){activeSlot[0] = 2;
Serial.println(x = activeSlot[0]);
}
if (key == 'C'){activeSlot[0] = 3;
Serial.println(x = activeSlot[0]);
}
if (key == 'D'){activeSlot[0] = 4;
Serial.println(x = activeSlot[0]);
}
x = activeSlot[0];
//1 energy section
if(key == '1'); //if _ is pressed
if(activeSlot[0] = 1); //if the slot is A
if(totalA >= 1); //if A has at least _ energy
slotA = 1; //set A to _
//B section
if(activeSlot[0] = 2);
if(totalB >= 1);
slotB = 1;
//C section
if(activeSlot[0] = 3);
if(totalC >= 1);
slotC = 1;
//D section
if(activeSlot[0] = 4);
if(totalD >= 1);
slotD = 1;
//2 energy section
if(key == '2');
if(activeSlot[0] = 1);
if(totalA >= 2);
slotA = 2;
if(activeSlot[0] = 2);
if(totalB >= 2);
slotB = 2;
if(activeSlot[0] = 3);
if(totalC >= 2);
slotC = 2;
if(activeSlot[0] = 4);
if(totalD >= 2);
slotD = 2;
//3 energy section
if(key == '3');
if(activeSlot[0] = 1);
if(totalA >= 3);
slotA = 3;
if(activeSlot[0] = 2);
if(totalB >= 3);
slotB = 3;
if(activeSlot[0] = 3);
if(totalC >= 3);
slotC = 3;
if(activeSlot[0] = 4);
if(totalD >= 3);
slotD = 3;
//4 energy section
if(key == '4');
if(activeSlot[0] = 1);
if(totalA >= 4);
slotA = 4;
if(activeSlot[0] = 2);
if(totalB >= 4);
slotB = 4;
if(activeSlot[0] = 3);
if(totalC >= 4);
slotC = 4;
if(activeSlot[0] = 4);
if(totalD >= 4);
slotD = 4;
//5 energy section
if(key == '5');
if(activeSlot[0] = 1);
if(totalA >= 5);
slotA = 5;
if(activeSlot[0] = 2);
if(totalB >= 5);
slotB = 5;
if(activeSlot[0] = 3);
if(totalC >= 5);
slotC = 5;
if(activeSlot[0] = 4);
if(totalD >= 5);
slotD = 5;
//6 energy section
if(key == '6');
if(activeSlot[0] = 1);
if(totalA >= 6);
slotA = 6;
if(activeSlot[0] = 2);
if(totalB >= 6);
slotB = 6;
if(activeSlot[0] = 3);
if(totalC >= 6);
slotC = 6;
if(activeSlot[0] = 4);
if(totalD >= 6);
slotD = 6;
//7 energy section
if(key == '7');
if(activeSlot[0] = 1);
if(totalA >= 7);
slotA = 7;
if(activeSlot[0] = 2);
if(totalB >= 7);
slotB = 7;
if(activeSlot[0] = 3);
if(totalC >= 7);
slotC = 7;
if(activeSlot[0] = 4);
if(totalD >= 7);
slotD = 7;
//0 energy section
if(key == '0');
if(activeSlot[0] = 1);
if(totalA >= 0);
slotA = 0;
if(activeSlot[0] = 2);
if(totalB >= 0);
slotB = 0;
if(activeSlot[0] = 3);
if(totalC >= 0);
slotC = 0;
if(activeSlot[0] = 4);
if(totalD >= 0);
slotD = 0;
//the amount of energy used in all the slots, for the LEDs
int totalE = slotA + slotB + slotC + slotD;
此部分检查是否按下了数字键。如果按下一个,它会检查activeSlot中要更改的插槽。如果剩余的总能量足够,那么它应该将该插槽更改为按下的数字键的值。
//LED SECTION
if(totalE == 0){
digitalWrite(13,LOW);
digitalWrite(12,LOW);
digitalWrite(11,LOW);
digitalWrite(10,LOW);
digitalWrite(14,LOW);
digitalWrite(15,LOW);
digitalWrite(16,LOW);
digitalWrite(17,LOW);
digitalWrite(18,LOW);
digitalWrite(19,LOW);
}
if(totalE == 1){
digitalWrite(13,HIGH);
digitalWrite(12,LOW);
digitalWrite(11,LOW);
digitalWrite(10,LOW);
digitalWrite(14,LOW);
digitalWrite(15,LOW);
digitalWrite(16,LOW);
digitalWrite(17,LOW);
digitalWrite(18,LOW);
digitalWrite(19,LOW);
}
if(totalE == 2){
digitalWrite(13,HIGH);
digitalWrite(12,HIGH);
digitalWrite(11,LOW);
digitalWrite(10,LOW);
digitalWrite(14,LOW);
digitalWrite(15,LOW);
digitalWrite(16,LOW);
digitalWrite(17,LOW);
digitalWrite(18,LOW);
digitalWrite(19,LOW);
}
if(totalE == 3){
digitalWrite(13,HIGH);
digitalWrite(12,HIGH);
digitalWrite(11,HIGH);
digitalWrite(10,LOW);
digitalWrite(14,LOW);
digitalWrite(15,LOW);
digitalWrite(16,LOW);
digitalWrite(17,LOW);
digitalWrite(18,LOW);
digitalWrite(19,LOW);
}
if(totalE == 4){
digitalWrite(13,HIGH);
digitalWrite(12,HIGH);
digitalWrite(11,HIGH);
digitalWrite(10,HIGH);
digitalWrite(14,LOW);
digitalWrite(15,LOW);
digitalWrite(16,LOW);
digitalWrite(17,LOW);
digitalWrite(18,LOW);
digitalWrite(19,LOW);
}
if(totalE == 5){
digitalWrite(13,HIGH);
digitalWrite(12,HIGH);
digitalWrite(11,HIGH);
digitalWrite(10,HIGH);
digitalWrite(14,HIGH);
digitalWrite(15,LOW);
digitalWrite(16,LOW);
digitalWrite(17,LOW);
digitalWrite(18,LOW);
digitalWrite(19,LOW);
}
if(totalE == 6){
digitalWrite(13,HIGH);
digitalWrite(12,HIGH);
digitalWrite(11,HIGH);
digitalWrite(10,HIGH);
digitalWrite(14,HIGH);
digitalWrite(15,HIGH);
digitalWrite(16,LOW);
digitalWrite(17,LOW);
digitalWrite(18,LOW);
digitalWrite(19,LOW);
}
if(totalE == 7){
digitalWrite(13,HIGH);
digitalWrite(12,HIGH);
digitalWrite(11,HIGH);
digitalWrite(10,HIGH);
digitalWrite(14,HIGH);
digitalWrite(15,HIGH);
digitalWrite(16,HIGH);
digitalWrite(17,LOW);
digitalWrite(18,LOW);
digitalWrite(19,LOW);
}
if(totalE == 8){
digitalWrite(13,HIGH);
digitalWrite(12,HIGH);
digitalWrite(11,HIGH);
digitalWrite(10,HIGH);
digitalWrite(14,HIGH);
digitalWrite(15,HIGH);
digitalWrite(16,HIGH);
digitalWrite(17,HIGH);
digitalWrite(18,LOW);
digitalWrite(19,LOW);
}
if(totalE == 9){
digitalWrite(13,HIGH);
digitalWrite(12,HIGH);
digitalWrite(11,HIGH);
digitalWrite(10,HIGH);
digitalWrite(14,HIGH);
digitalWrite(15,HIGH);
digitalWrite(16,HIGH);
digitalWrite(17,HIGH);
digitalWrite(18,HIGH);
digitalWrite(19,LOW);
}
if(totalE == 10){
digitalWrite(13,HIGH);
digitalWrite(12,HIGH);
digitalWrite(11,HIGH);
digitalWrite(10,HIGH);
digitalWrite(14,HIGH);
digitalWrite(15,HIGH);
digitalWrite(16,HIGH);
digitalWrite(17,HIGH);
digitalWrite(18,HIGH);
digitalWrite(19,HIGH);
}
if(key == '#'){
Serial.println(x = activeSlot[0]);
}
if(key == '*'){
Serial.println(x = totalE);
}
//end of void loop
}
这是LED部分。我打算用它来检查totalE(每个插槽的总值(,并用它来点亮正确数量的LED。totalE为0表示没有点亮,1表示1点亮,等等。它似乎没有任何作用,totalE似乎也没有改变。由于某些原因,activeSlot[0]也总是停留在4。
代码的这一部分
if(key == '1'); //if _ is pressed
if(activeSlot[0] = 1); //if the slot is A
if(totalA >= 1); //if A has at least _ energy
slotA = 1; //set A to _
几乎可以肯定的是没有做你期望的事情。每个if
语句后的分号;
终止if
语句,并且不执行任何操作。然后无条件地执行每个if
语句后面的行,就好像没有if
条件保护它们一样(因为在编写时没有(。此外,您将为activeSlot[0]
(activeSlot[0] = 1
(赋值,而不是检查相等性。您需要if (activeSlot[0] == 1)
。您当前的代码可以重写为(缩进表示作用域(
if(key == '1'); //if '1' is pressed, do nothing
if(activeSlot[0] = 1); //assign the value of 1 to activeSlot[0], then do nothing
if(totalA >= 1); //if A has at least 1 energy, do nothing
slotA = 1; //set A to 1
你很可能想把它重写为
if(key == '1') //if _ is pressed
if(activeSlot[0] == 1) //if the slot is A
if(totalA >= 1) //if A has at least _ energy
slotA = 1; //set A to _
请注意,if
语句后缺少分号,并且使用布尔==
运算符而不是赋值=
。
在if
语句和变量赋值的整个链的末尾,您已经(重写以更好地显示行为(
if(key == '0'); // do nothing
if(activeSlot[0] = 1); // activeSlot[0] always gets assigned 1
if(totalA >= 0); // do nothing
slotA = 0; // this happens always
if(activeSlot[0] = 2); // activeSlot[0] always gets assigned 2
if(totalB >= 0); // do nothing
slotB = 0; // this happens always
if(activeSlot[0] = 3); // activeSlot[0] always gets assigned 3
if(totalC >= 0); // do nothing
slotC = 0; // this happens always
if(activeSlot[0] = 4); // activeSlot[0] always gets assigned 4 (overwriting all previous assignments)
if(totalD >= 0); // do nothing
slotD = 0; // this happens always