Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 프로그래머스
- 풀스택프레임워크
- 프로그래밍
- 개발자
- 모의 주식
- 사물인터넷
- 백준
- 자바독학
- 자바정리
- Reflex
- 코테
- C++
- 빅데이터
- 주가예측
- 자바
- 코딩
- java
- backend
- java정리
- 코스닥
- Python
- 이웃한 칸
- SNS
- 코딩독학
- 프로그램
- Project
- 코딩테스트
- frontend
- 개발
- 개인프로젝트
Archives
- Today
- Total
Creative Code
13_PCINT_ultrasonic(timer1함수를 이용한 거리측정) 본문
#include <PinChangeInterrupt.h>
#include <TimerOne.h>
const int trig_pin = 9;
const int echo_pin = 10;
unsigned long echo_duration =0;
void echoIsr(void) {
static unsigned long echo_begin = 0;
static unsigned long echo_end = 0;
unsigned int echo_pin_state = digitalRead(echo_pin);
if (echo_pin_state == HIGH) {
echo_begin = micros();
} else {
echo_end = micros();
echo_duration = echo_end - echo_begin;
}
}
void timerIsr(void) {
if (echo_duration == 0) {
digitalWrite(trig_pin,LOW);
delayMicroseconds(2);
digitalWrite(trig_pin,HIGH);
delayMicroseconds(2);
digitalWrite(trig_pin,LOW);
} else {
unsigned long distance = echo_duration / 58;
Serial.print(distance);
Serial.println(" cm");
echo_duration = 0;
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(trig_pin,OUTPUT);
pinMode(echo_pin,INPUT);
Timer1.initialize(500000);
Timer1.attachInterrupt(timerIsr);
attachPCINT(digitalPinToPCINT(echo_pin),echoIsr,CHANGE);
}
void loop() {
// put your main code here, to run repeatedly:
}
'Arduino(C,C++)' 카테고리의 다른 글
15_ArrayFND (0) | 2023.08.14 |
---|---|
14_FND (0) | 2023.08.14 |
12_timer1_piezo (0) | 2023.08.14 |
11_timer1_led (0) | 2023.08.14 |
10_ultrasonic(초음파 거리재기) (0) | 2023.08.11 |