아두이노 인터럽트로 제어하는 HIT 센서
2019. 7. 9. 12:08ㆍ드론
인터럽트란?
실행 중인 프로그램을 일시 중단하고, 다른 프로그램을 끼워 넣어 실행시키는 것
attachInterrupt(pin_interrupt, ISR, mode)
-
인터럽트가 발생할 때 호출할 인터럽트 서비스 루틴(ISR)을 지정
- pin_interrupt은 인터럽트 번호(핀 번호가 다를 수 있다)
- ISR은 인터럽트 발생시 호출될 함수
- mode는 LOW, HIGH, CHANGE, RISING, FALLING으로 설정
-
아두이노 우노 인터럽트 핀은 총 2개이며, INT0는 2번, INT1은 3번핀
- 인터럽트 callback 함수는 파라미터를 전달하거나 리턴할 수 없음 (반드시 voidxx())
- ISR 안에서는 delay() 함수를 사용할 수 없음
- ISR 안에서 Serial data를 읽을 경우 소실됨
interrupts() 함수
- 인터럽트 발생 허용
nointerrupts() 함수
- 인터럽트 발생 금지
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int pinHit = 2; | |
int count = 0; | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(115200); | |
pinMode(pinHit, INPUT); | |
attachInterrupt(0, hitISR, FALLING); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
Serial.print("HIT : "); | |
Serial.println(count); | |
delay(500); | |
} | |
void hitISR(void) { | |
count++; | |
} |
한 번 입력받을 때 LED를 toggle 시키는 소스코드, flag 변수를 이용해 중복을 없앴다
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int pinHit = 2; | |
int pinLed = 13; | |
int ledStatus = LOW; | |
int count = 0; | |
volatile int flag = 0; | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(115200); | |
pinMode(pinHit, INPUT); | |
pinMode(pinLed, OUTPUT); | |
attachInterrupt(0, hitISR, FALLING); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
Serial.print("HIT : "); | |
Serial.println(count); | |
flag = 0; | |
digitalWrite(pinLed, ledStatus); | |
delay(500); | |
} | |
void hitISR(void) { | |
if(flag == 0) { | |
count++; | |
flag = 1; | |
ledStatus = !ledStatus; | |
} | |
} |
'드론' 카테고리의 다른 글
아두이노에 FreeRTOS 올리고 frBlink 예제 실행하기 (0) | 2019.07.09 |
---|---|
아두이노 clcd 문자열 입력받아서 띄우기 (0) | 2019.07.09 |
아두이노 Reed 자기 센서 제어 (0) | 2019.07.09 |
아두이노로 FND 6자리 출력하기 (0) | 2019.07.08 |
아두이노 스위치 입력 한번만 받기 (0) | 2019.07.08 |