Skip to content

Instantly share code, notes, and snippets.

@gitaeks
Last active December 25, 2020 11:31
Show Gist options
  • Select an option

  • Save gitaeks/3bf93943151ebab974add15571f7257a to your computer and use it in GitHub Desktop.

Select an option

Save gitaeks/3bf93943151ebab974add15571f7257a to your computer and use it in GitHub Desktop.
마스크 없이 괜찮아요?
#include <Servo.h> //서보모터 라이브러리를 불러옵니다.
#include <Adafruit_NeoPixel.h> //네오픽셀 라이브러리를 불러옵니다.
#include <SoftwareSerial.h>
#include <DFPlayer_Mini_Mp3.h>
int motionPin = 7; // 인체감지센서를 7번핀으로 설정합니다.
int motionState = LOW;
int trigPin = 2;
int echoPin = 3;
int pixelPin1 = 13;
int pixelPin2 = 12;
int pixelCount = 16;
int drawerStart = 30; //마스크 수납함이 열리는 기준거리(cm)
SoftwareSerial mySerial(10,11);
Servo drawer; // 서보를 제어할 객체를 만듭니다.
Adafruit_NeoPixel ring1(pixelCount, pixelPin1, NEO_GRB + NEO_KHZ800); // 네오픽셀을 제어할 객체1를 만듭니다
Adafruit_NeoPixel ring2(pixelCount, pixelPin2, NEO_GRB + NEO_KHZ800); // 네오픽셀을 제어할 객체2를 만듭니다
void setup() {
Serial.begin(9600); // 시리얼 통신을 시작
mySerial.begin(9600); // 시리얼 통신을 시작
pinMode(motionPin, INPUT); // 인체감지센서의 핀을 INPUT으로 설정합니다.
drawer.attach(9); // 핀 9의 서보를 서보 오브젝트에 연결합니다.
drawer.write(90); // 서모보터 각도 초기화
// trig를 출력모드로 설정, echo를 입력모드로 설정
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
ring1.begin(); //네오픽셀을 초기화하기 위해 모든LED를 off시킨다
ring1.show();
ring2.begin(); //네오픽셀을 초기화하기 위해 모든LED를 off시킨다
ring2.show();
mp3_set_serial(mySerial); // DFPlayer-mini mp3 module 시리얼 세팅
delay(1); // 볼륨값 적용을 위한 delay
mp3_set_volume(30); // 볼륨조절 값 0~30mp3_set_serial (Serial);
}
void loop() {
float duration, distance;
// 적외선 인체감지 센서에서 값을 읽는다
motionState = digitalRead(motionPin);
//네오픽셀 켜기
for(int i=0; i<pixelCount; i++) {
ring1.setPixelColor(i, random(0, 256), random(0, 256), random(0,256));
}
for(int i=0; i<pixelCount; i++) {
ring2.setPixelColor(i, random(0, 256), random(0, 256), random(0,256));
}
ring1.show();
ring2.show();
if (motionState == HIGH) {
// 스피커 음성출력
boolean play_state = digitalRead(4);
if( play_state == HIGH) {
mp3_play(1);
delay(6000);
mp3_stop();
}
} else {
//Serial.println("none");
//네오픽셀 끄기
for(int i=0; i<pixelCount; i++) {
ring1.setPixelColor(i, 0, 0, 0);
ring2.setPixelColor(i, 0, 0, 0);
}
ring1.show();
ring2.show();
}
//마스크 필요시 보관박스에 손을 내밀었을때 박스 하단 열기
// 초음파를 보낸다. 다 보내면 echo가 HIGH 상태로 대기하게 된다.
digitalWrite(trigPin, HIGH);
delay(10);
digitalWrite(trigPin, LOW);
// echoPin 이 HIGH를 유지한 시간을 저장 한다.
duration = pulseIn(echoPin, HIGH);
// HIGH 였을 때 시간(초음파가 보냈다가 다시 들어온 시간)을 가지고 거리를 계산 한다.
distance = ((float)(340 * duration) / 10000) / 2;
//Serial.print(distance);
//Serial.println("cm");
if (distance <= drawerStart) {
Serial.println("drawer open");
mp3_play(2);
delay(5000);
mp3_stop();
drawer.write(0); //마스크수납 서랍 열기
} else {
delay(5000);
drawer.write(90); //마스크수납 서랍 닫기
Serial.println("drawer close");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment