Skip to content

Instantly share code, notes, and snippets.

@gitaeks
Created December 22, 2020 09:22
Show Gist options
  • Select an option

  • Save gitaeks/1c61e100c0616ccbc87f320d24cb3dd1 to your computer and use it in GitHub Desktop.

Select an option

Save gitaeks/1c61e100c0616ccbc87f320d24cb3dd1 to your computer and use it in GitHub Desktop.
chkMask
#include "pitches.h" //include zip library
#include "MsTimer2.h"
#include <Servo.h>
int LED = 13; // LED
int PIEZO = 8; //부저
int MOTION = 7; //모션 센서
int trigPin = 2; //초음파 트리거 핀
int echoPin = 3; //초음파 에코 핀
Servo myservo;
boolean isOpen = false;
int motionState = LOW;
// notes in the melody:
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4
};
void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
pinMode(MOTION, INPUT);
pinMode(trigPin,OUTPUT);
pinMode(echoPin, INPUT);
MsTimer2::set(1000,openBox);//마스크 요청 감지.2초마다.
MsTimer2::start();
myservo.attach(9);
}
void loop() {
//1.모션 감지
motionState = digitalRead(MOTION);
Serial.println( motionState );
if(motionState == HIGH ) {
Serial.println("motion on");
digitalWrite(LED, HIGH);
RingBuzzer();
//1.1 '괜찮아요~' 음성 출력 코드
//1.2 네오픽셀 작동 코드
//1.3 마스크 필요시 보관박스에 손을 내밀었을때 박스 하단 열기
delay(30*1000);//60초 대기
}else {
Serial.println("motion off");
digitalWrite(LED, LOW);
//noTone(PIEZO);
//2.1
}
}
/*
* 마스크 필요시 마스크 박스쪽으로 손을 가까이 하면 박스 하단 문 열림
*/
void openBox(){
digitalWrite(trigPin,HIGH);
delay(10);
digitalWrite(trigPin,LOW);
float duration = pulseIn(echoPin,HIGH);
float distance = ((float)(340*duration) /10000 ) /2 ;
if(motionState == HIGH) {
Serial.println("request to open box..");
if(distance <= 20) {
if(isOpen == false ) {
Serial.println("open mask box...");
myservo.write(0);
isOpen = true;
}
}else {
if(isOpen == true ) {
Serial.println("close mask box...");
myservo.write(170);
isOpen = false;
}
} //end distance
} //end motionState
}
/*
* RingBuzzer
* 모션 감지시 소리내기
*/
void RingBuzzer(){
for (int thisNote = 0; thisNote < 8; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(PIEZO, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(PIEZO);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment