BASIC4MCU | 질문게시판 | 아두이노 홀센서 코딩 측정 값 오류
페이지 정보
작성자 공대생입니다 작성일2020-10-19 20:51 조회4,757회 댓글2건본문
#define DHALL 2
unsigned long time_=0, time_previous=0;
double w=0;
double w_previous=0;
double a=0;
double t=0;
double p=0;
int i = 162;
int Dvalue = 0;
void setup() {
Serial.begin(9600);
pinMode(DHALL,INPUT);
attachInterrupt(digitalPinToInterrupt(2), interrupt, FALLING);
// put your setup code here, to run once:
}
void loop() {
w = 6.28/(time_ - time_previous);
a = (w- w_previous)/(time_ - time_previous);
t = i*a;
p = w*t;
w_previous = w;
Serial.print("Time(millis):");
Serial.println(time_ - time_previous);
Serial.print("w =");
Serial.println(w);
Serial.print("w_previous =");
Serial.println(w_previous);
delay(1000); // put your main code here, to run repeatedly:
}
void interrupt () {
time_previous = time_;
time_ = millis();
}
위 처럼 코딩을 하면 w , w_previous 의 값이 동일 하게 나옵니다. 그로 인해 a 값 t 값 p 값 이 0 으로 나오는 것 같습니다.
코딩에 어떤 부분을 수정해야 할지 도움 주시면 감사하겠습니다.
댓글 2
조회수 4,757master님의 댓글
master 작성일
bool flag=0; // 셋업함수 윗쪽에서 전역변수 추가하시고
void interrupt () {
time_previous = time_;
time_ = millis();
flag=1; // 인터럽트가 걸린 것을 알려주기 위한 변수
}
인터럽트 함수는 위처럼 수정하세요
이제 루프문을 수정해보죠
delay(1000); // put your main code here, to run repeatedly:
제일 아래 딜레이는 삭제합니다.
인터럽트든 메인이든 불필요한 딜레이는 없는 것이 좋죠
void loop() {
if(flag){ flag=0;
w = 6.28/(time_ - time_previous);
a = (w- w_previous)/(time_ - time_previous);
t = i*a;
p = w*t;
w_previous = w;
Serial.print("Time(millis):"); Serial.println(time_ - time_previous);
Serial.print("w ="); Serial.println(w);
Serial.print("w_previous ="); Serial.println(w_previous);
}
}
루프문에서는 flag 변수 때문에 인터럽트가 걸린 후에 1회만 처리됩니다.
공대생입니다님의 댓글
공대생입니다 작성일답변 감사합니다.