1. 초음파 센서
- 초음파를 이용하여 벽이나 사물의 거리를 측정하는 센서
2. 초음파 센서 동작 원리
- Trig에 HIGH 전압 인가시 초음파 발사
- Echo에서 초음파 수신(처음에는 LOW)
- 시간 차이를 측정하여 거리 계산
3. 초음파 센서 거리 측정
#include <MiniCom.h>
MiniCom com;
int echoPin = 5;
int triggerPin = 6;
void check() {
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
int distance = pulseIn(echoPin, HIGH) / 58;
com.print(1, "Dist.(cm)=", distance);
}
void setup() {
com.init();
com.setInterval(1000, check);
com.print(0, "[[Distance]]");
pinMode(echoPin, INPUT);
pinMode(triggerPin, OUTPUT);
}
void loop() {
com.run();
}
4. Ultra class
#pragma once
#include <Arduino.h>
class Ultra{
protected:
int echo;
int trig;
public:
Ultra(int echo, int trig);
int getDistance();
};
#include "Ultra.h"
Ultra::Ultra(int echo, int trig) : echo(echo), trig(trig){
pinMode(echo, INPUT);
pinMode(trig, OUTPUT);
}
int Ultra::getDistance(){
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig,LOW);
int distance = pulseIn(echo, HIGH) / 58;
return distance;
}
#include <MiniCom.h>
#include <Ultra.h>
MiniCom com;
Ultra ultra(5, 6);
void check(){
int distance = ultra.getDistance();
com.print(1,"Dist.(cm)=", distance);
}
void setup(){
com.init();
com.setInterval(1000, check);
com.print(0, "[[Distance]]");
}
void loop(){
com.run();
}
5. Buzzer.cpp 자동차 후방 감지기 만들기
#pragma once
#include <Arduino.h>
class Buzzer {
protected:
int pin;
int state;
int on_time;
int off_time;
unsigned long old_time;
bool bstop;
public:
Buzzer(int pin);
void beep();
void stop() {
bstop = true;
digitalWrite(pin, LOW);
}
void setOffTime(int off_time);
void run();
};
#include "Buzzer.h"
Buzzer::Buzzer(int pin): pin(pin){
state = LOW;
off_time = 0;
on_time = 100;
pinMode(pin, OUTPUT);
bstop = true;
}
void Buzzer::beep(){
digitalWrite(pin,HIGH);
delay(50);
digitalWrite(pin, LOW);
}
void Buzzer::setOffTime(int off_time){
this -> off_time = off_time;
bstop = false;
}
void Buzzer::run() {
unsigned long cur, diff;
if(bstop) return;
cur = millis();
diff = cur - old_time;
if(state) {
if(diff > on_time) {
digitalWrite(pin, LOW);
state = LOW;
old_time = cur;
}
} else {
if(diff > off_time ) {
digitalWrite(pin, HIGH);
state = HIGH;
old_time = cur;
}
}
}
#include <MiniCom.h>
#include <Ultra.h>
#include <Buzzer.h>
MiniCom com;
Ultra ultra(5, 6);
Buzzer buzzer(7);
void check() {
int distance = ultra.getDistance();
com.print(1, "Dist.(cm)=", distance);
if(distance < 50 && distance >= 30) {
buzzer.setOffTime(1000);
} else if(distance < 30 && distance >= 20) {
buzzer.setOffTime(500);
} else if(distance < 20 && distance >= 10) {
buzzer.setOffTime(200);
} else if(distance < 10) {
buzzer.setOffTime(100);
} else {
buzzer.stop();
}
}
void setup() {
com.init();
com.setInterval(100, check);
com.print(0, "[[Distance]]");
buzzer.stop();
}
void loop() {
com.run();
buzzer.run();
}
6. 물체 감지기
#pragma once
#include <Arduino.h>
typedef void (*ultra_callback_t)();
class Ultra {
protected:
int echo;
int trig;
int threshold;
ultra_callback_t on_f;
ultra_callback_t off_f;
int state;
public:
Ultra(int echo, int trig);
int getDistance();
void setThreshold(int t, ultra_callback_t on_f, ultra_callback_t off_f);
int run();
};
#include "Ultra.h"
Ultra::Ultra(int echo, int trig) : echo(echo), trig(trig) {
pinMode(echo, INPUT);
pinMode(trig, OUTPUT);
on_f = NULL;
off_f = NULL;
state = false;
}
:
void Ultra::setThreshold(int t, ultra_callback_t on_f, ultra_callback_t off_f)
{
threshold = t;
this->on_f = on_f;
this->off_f = off_f;
}
int Ultra::run() {
int distance = getDistance();
if(!state && distance <= threshold) {
state = true;
if(on_f != NULL) {
on_f();
}
} else if(state && (distance == 0 || distance > (threshold+2))){
state = false;
if(off_f != NULL) {
off_f();
}
}
if(distance == 0 || distance > threshold ) {
return 0;
}
return distance;
}
#include <MiniCom.h>
#include <Ultra.h>
MiniCom com;
Ultra ultra(5, 6);
void notice_in() {
com.print(1, "NOTICE IN");
}
void notice_out() {
com.print(1, "NOTICE OUT");
}
void setup() {
com.init();
com.print(0, "[[Distance]]");
ultra.setThreshold(40, notice_in, notice_out);
}
void loop() {
com.run();
ultra.run();
}