首页 > 解决方案 > 数一数二开关量输入

问题描述

#define GRN_LED 23
#define RED_LED 25
#define YEL_LED 12

#define RED_SW 22
#define GRN_SW 17
#define YEL_SW 6

int red_sw;
int grn_sw;
int yel_sw;

int red_cnt=0;
int grn_cnt=0;
int yel_cnt=0;
int tot = 0;


int main(void)
{
    wiringPiSetupGpio();
    pinMode(RED_SW, INPUT);
    pinMode(GRN_SW, INPUT);
    pinMode(YEL_SW, INPUT);
    
    
    while(1)
    {
        if(digitalRead(RED_SW)==LOW)
        {   
            red_cnt++;
            printf("Click: R(%d) G(%d) Y(%d)\n", red_cnt, grn_cnt, yel_cnt);
        }
        
        if(digitalRead(GRN_SW)==HIGH)
        {   
            grn_cnt++;
            printf("Click: R(%d) G(%d) Y(%d)\n", red_cnt, grn_cnt, yel_cnt);
        }
        
        if(digitalRead(YEL_SW)==LOW)
        {   
            yel_cnt++;
            printf("Click: R(%d) G(%d) Y(%d)\n", red_cnt, grn_cnt, yel_cnt);
        }
    }
        
    
            

    return 0;
} 

![文本]( https://stackoverflow.com/image [![在此处输入图像描述] 1 ] 1 .jpg)

我正在使用树莓派。我正在尝试计算切换次数。如果我按下红色开关,红色开关只数一个。但就像上图一样,当我按下开关时,它会继续计算数字。我怎样才能只数一次按一次?

标签: raspberry-piswitch-statement

解决方案


推荐阅读