raspberrypi/gpio/key_light.c
2020-09-10 13:23:23 +08:00

48 lines
1.2 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* raspberrypi key light
* 功能key等于1时亮的灯灭、灭的灯亮
*/
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define LIGHT 3 // 灯PIN引脚
#define KEY 4 // 按键PIN引脚
int main(int argc, char **argv) {
daemon(1, 1);
if(-1 == wiringPiSetup())
{
printf("wiringPi setup error\n");
exit(-1);
}
pinMode(KEY, INPUT); // 按键输入模式
pullUpDnControl(KEY, PUD_DOWN); // 按键启用下拉电阻引脚电平拉到GND
pinMode(LIGHT, OUTPUT); // 灯PIN引脚输出模式
int light_status;
int key_status;
while(1){
light_status = digitalRead(LIGHT); // 灯状态
key_status = digitalRead(KEY); // 按键状态
if (key_status == 1) {
if (light_status == 0) {
digitalWrite(LIGHT, HIGH); // 开灯
}
if (light_status == 1) {
digitalWrite(LIGHT, LOW); // 关灯
}
}
printf("%d\n", digitalRead(KEY));
delay(200);
}
}