模块原理图
按键没有按下时SIG是高电平(高电平就是VCC的电压),G17脚读到的是信号1.亮绿灯.按下后引脚信号为0,亮红灯.
代码及注释:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| #include <wiringPi.h> #include <stdio.h> #define BtnPin0 #define Gpin1 #define Rpin2 void LED(char* color) { pinMode(Gpin, OUTPUT); pinMode(Rpin, OUTPUT); if (color == "RED") { digitalWrite(Rpin, HIGH); digitalWrite(Gpin, LOW); } else if (color == "GREEN") { digitalWrite(Rpin, LOW); digitalWrite(Gpin, HIGH); } else printf("LED Error"); } int main(void) { if(wiringPiSetup() == -1){ printf("setup wiringPi failed !"); return 1; } pinMode(BtnPin, INPUT); LED("GREEN"); while(1){ if(0 == digitalRead(BtnPin)){ delay(10); if(0 == digitalRead(BtnPin)){ LED("RED"); printf("Button is pressed\n"); } } else if(1 == digitalRead(BtnPin)){ delay(10); if(1 == digitalRead(BtnPin)){ while(!digitalRead(BtnPin)); LED("GREEN"); } } } return 0; }
|
BUTTON SWITCH 按键模块原理图链接:
BUTTON SWITCH 按键模块
树莓派3更多模块请点击链接:
从入门到放弃的学习RASPBERRYPI