STUDY BLOG
디바운싱을 이용한 LED 제어 본문
버튼 1 누르면 - 빨간LED켜짐 / 한번 더 누르면 - 꺼짐
버튼 2 누르면 - 연두LED켜짐 / 한번 더 누르면 - 꺼짐
버튼 3 누르면 - 노랑LED켜짐 / 한번 더 누르면 - 꺼짐
#include <iostream>
#include <wiringPi.h>
#include <unistd.h>
using namespace std;
#define LED_GPIO_R 18 // this is GPIO17, pin 11
#define LED_GPIO_G 13
#define LED_GPIO_Y 12
#define BUTTON_GPIO_R 27 // this is GPIO27, pin 13
#define BUTTON_GPIO_G 17
#define BUTTON_GPIO_Y 19
#define DEBOUNCE_TIME 200 // debounce time in ms
bool x = 0;
bool y = 0;
bool z = 0;
// the Interrupt Service Routine (ISR) to light the LED - debounced
void lightLED_R(void){
static unsigned long lastISRTime = 0, x = 1;
unsigned long currentISRTime = millis();
if (currentISRTime - lastISRTime > DEBOUNCE_TIME){
x=!x;
digitalWrite(LED_GPIO_R,x); // turn the LED on
delay(1000);
}
lastISRTime = currentISRTime;
}
void lightLED_G(void) {
static unsigned long lastISRTime =0 ,y=1;
unsigned long currentISRTime = millis();
if (currentISRTime - lastISRTime > DEBOUNCE_TIME) {
y=!y;
digitalWrite(LED_GPIO_G,y);
delay(1000);
}
lastISRTime = currentISRTime;
}
void lightLED_Y(void) {
static unsigned long lastISRTime = 0, z=1;
unsigned long currentISRTime = millis();
if (currentISRTime - lastISRTime > DEBOUNCE_TIME) {
z=!z;
digitalWrite(LED_GPIO_Y, z);
delay(1000);
}
lastISRTime = currentISRTime;
}
int main() { // must be run as root
wiringPiSetupGpio(); // use the GPIO numbering
pinMode(LED_GPIO_R, OUTPUT); // the LED
pinMode(LED_GPIO_G, OUTPUT);
pinMode(LED_GPIO_Y, OUTPUT);
pinMode(BUTTON_GPIO_R, INPUT);
pinMode(BUTTON_GPIO_G, INPUT);
pinMode(BUTTON_GPIO_Y, INPUT); // the Button
while(1){
// call the lightLED() ISR on the rising edge (i.e., button press)
wiringPiISR(BUTTON_GPIO_R, INT_EDGE_RISING, &lightLED_R);
wiringPiISR(BUTTON_GPIO_G, INT_EDGE_RISING, &lightLED_G);
wiringPiISR(BUTTON_GPIO_Y, INT_EDGE_RISING, &lightLED_Y);
}
// program ends after 10s
}
'Hardware > Raspberry Pi' 카테고리의 다른 글
온도센서로 RGB 모듈 제어 (0) | 2021.04.08 |
---|---|
초음파센서로 RGB 모듈 제어 (0) | 2021.04.08 |
LED 번갈아 페이딩 (0) | 2021.04.07 |