我正试图编译一个开关语句来创建从核板的各种密码锁。起初,我试图通过创建位掩码和分配整数来解决在switch语句中使用多个数字输入的问题,这似乎已经好了,但是当我试图让我的switch语句运行时,它被卡在case 0上。
对于开始情况,0应该没有按钮被按下,但它只在我按下开关1时激活。我的第二个问题是,我的语句中没有其他的case会被激活。
我无法访问调试器,因为mbed与我的核板不兼容,我无法获得Keil Studio工作,所以我很困惑。有没有人对我的声明有什么问题,或者是否有一种替代方法可以在开关声明中引用我的数字输入,这可能会使它更容易?
我是一个编码n00b,一直在努力找到很多参考我的问题,我看过的任何示例代码似乎都没有问题,我看不出我在哪里偏离了该代码。
代码如下:
// You are to use these ojects to read the switch inputs
DigitalIn SW1(USER_BUTTON);
DigitalIn SW2(BTN1_PIN);
DigitalIn SW3(BTN2_PIN);
DigitalInOut SW4(BTN3_PIN, PIN_INPUT, PullDown, 0);
DigitalInOut SW5(BTN4_PIN, PIN_INPUT, PullDown, 0);
// You are to use this object to control the LEDs
BusOut leds(TRAF_RED1_PIN, TRAF_YEL1_PIN, TRAF_GRN1_PIN);
// Use this to sound an error
Buzzer alarm;
int main()
{
while (true)
{
leds = 0;
// Beep
alarm.playTone("A", Buzzer::HIGHER_OCTAVE);
wait_us(250000);
alarm.rest();
// Wait for the blue button using a while loop
while (SW1==0) { };
// For full marks, debounce the switches with suitable delays
// This is a "combination lock" activity. Write some code to detect the following sequence of press-and-release inputs
// SW1, SW2, SW5, SW3 and SW4, SW2 and SW3
// If the full sequence is entered, correctly, the green LED should flash 3 times
// If a sequence of inputs was entered incorrectly, the red LED should light and the buzzer should sound for 5 seconds
// For full marks, debounce the switches and use flow control structures and arrays to avoid deep nesting of code
// ***** MODIFY THE CODE BELOW HERE *****
// ***** MODIFY THE CODE ABOVE HERE *****
int Inputs = (SW1==0) << 0 | (SW2==1) << 1 | (SW3==1) << 2 | (SW4==1) << 3 | (SW5==1) << 4;
int i;
switch (Inputs) {
case 0:
printf("Please Enter Combinationn");
if (false) {
alarm.playTone("A", Buzzer::HIGHER_OCTAVE);
wait_us(250000);
alarm.rest();
leds = 4;
wait_us(5000000);
leds = 0;
}
break;
case 1:
printf("Input 1 is Correctn");
if (false) {
alarm.playTone("A", Buzzer::HIGHER_OCTAVE);
wait_us(250000);
alarm.rest();
leds = 4;
wait_us(5000000);
leds = 0;
}
break;
case 2:
printf("Input 2 is Correctn");
if (false) {
alarm.playTone("A", Buzzer::HIGHER_OCTAVE);
wait_us(250000);
alarm.rest();
leds = 4;
wait_us(5000000);
leds = 0;
}
break;
case 16:
printf("Input 3 is Correctn");
if (false) {
alarm.playTone("A", Buzzer::HIGHER_OCTAVE);
wait_us(250000);
alarm.rest();
leds = 4;
wait_us(5000000);
leds = 0;
}
break;
case 12:
printf("Input 4 is Correctn");
if (false) {
alarm.playTone("A", Buzzer::HIGHER_OCTAVE);
wait_us(250000);
alarm.rest();
leds = 4;
wait_us(5000000);
leds = 0;
}
break;
case 6:
printf("Combination is Correct!n");
for (int i = 0; i < 3; i = i +1)
{
leds = 1;
wait_us(1000000);
leds = 0;
wait_us(1000000);
}
if (false) {
alarm.playTone("A", Buzzer::HIGHER_OCTAVE);
wait_us(250000);
alarm.rest();
leds = 4;
wait_us(5000000);
leds = 0;
}
你可以创建一个位掩码整数,每个位对应一个按钮的状态,像这样:
unsigned int inputs = (SW1==0) << 0 | (SW2==0) << 1 | (SW3==0) << 2 | (SW4==0) << 3 | (SW5==0) << 4;
如果你以前没有见过这些运算符,<<
是一个左移运算符,|
是一个逻辑或运算符,你可以在任何一本像样的c++书籍或介绍性资源中找到它们。
你的switch语句可能像这样:
switch(inputs) {
case 0: // No buttons pressed
break;
case 1: // SW1 pressed (bit 0 is 1)
break;
case 2: // SW2 pressed (bit 1 is 1)
break;
case 4: // SW3 pressed (bit 2 is 1)
break;
case 6: // SW2 and SW3 pressed
break;
case 8: // SW4 pressed (bit 4 is 1)
break;
// ...
case 31: // All switches pressed
break;
}