메이커 Maker

욕실, 창고의 내부 상태를 알려주는 Rabbit 2

지구빵집 2021. 3. 18. 10:20
반응형

 

 

역시 매듭을 짓는 게 좋은 생각이라고 생각한다. 어려운 일도 아니라서, 꼭 필요한 건 아니라서, 제품을 만들기도 애매하다고 중단하고 포기하는 핑계를 댄다. 마무리는 언제든 해도 된다. 포스팅 1편에 모든 내용이 들어 있으니 참고하고 보드를 Nano 33 IoT 보드 정도로 만들면 다양한 환경에서 사용할 수 있겠다. 1편 포스팅을 아래에 연결한다.

 

욕실, 창고의 내부 상태를 알려주는 래빗 Rabbit 1

 

전체 소스코드는 아래와 같다. 연결하지 않은 온도 습도 센서와 실내 소리를 센싱하는 사운드 센서는 차후 버전에서 구현하기로 한다. 다 귀찮다. 

 

//function
//Arduino nano
//3.3V 동작, 충전 배터리
//부저 외부로 빼기

#include <avr/sleep.h>
#include "pitches.h"

int buzzer = 7; //부저 연결 7번
int human = 8;
int light = A0;
int light_digital = 9;
int interruptPin = 2; //Pin we are going to use to wake up the Arduino

int inputValue;

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(buzzer,OUTPUT);
    pinMode(LED_BUILTIN, OUTPUT);
    pinMode(human,INPUT);
    pinMode(light,INPUT);

    //interrupt sleep mode
    pinMode(LED_BUILTIN,OUTPUT);//We use the led on pin 13 to indecate when Arduino is A sleep
    pinMode(interruptPin,INPUT_PULLUP);//Set pin d2 to input using the buildin pullup resistor
    digitalWrite(LED_BUILTIN,HIGH);//turning LED on
}

//다 귀찮음
//이렇게 하자. 사람이 없고, 불이 켜져 있으면 1초마다 소리를 내고
//사람이 없고 불이 겨져 있으면 소리 0.2초 소리, 3초 쉬고
//불이 꺼져 있으면 중지 sleep 등등
//불나면 소리 무지크게
//사람이 있으면 무조건 소리 중지
//기타 있는 조건 다 걸어~^^

void loop() {

    //test_light_analog();
    inputValue = analogRead(light);

    if(inputValue > 250) //if light is off, goto sleep mod
    {
        //Going_To_Sleep(); //나중에
        //전원이 꺼져 있으니 무조건 슬립모드로 들어간다.
        //전원이 켜지면 깬다. - 깨는 핀은 조도 센서 디지털로 한다.
        //사람을 감지하면 3분간 슬립모드로 가게 할까?
    }
    
    if(inputValue < 250) //if light is on, real start
    {
        Serial.println("light ON");
        delay(1000);      

        if(digitalRead(human))
        {
            Serial.println("Human Detect");
            digitalWrite(LED_BUILTIN, HIGH);
            noTone(buzzer);        
        }
        else
        {
            Serial.println("Human Not Detect");
            set_buzzer2();
            delay(10000);
        }
    }
}

void Going_To_Sleep()
{
    Serial.println("Going to Sleep Mode");
    sleep_enable();//Enabling sleep mode
    attachInterrupt(0, wakeUp, LOW);//attaching a interrupt to pin d2
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);//Setting the sleep mode, in our case full sleep
    digitalWrite(LED_BUILTIN,LOW);//turning LED off
    delay(1000); //wait a second to allow the led to be turned off before going to sleep
    sleep_cpu();//activating sleep mode

    ADCSRA |= (1<<ADEN);
    Serial.println("here2");
    Serial.println("just woke up!");//next line of code executed after the interrupt 
    digitalWrite(LED_BUILTIN,HIGH);//turning LED on
 }

void wakeUp()
{
    Serial.println("Interrrupt Fired");//Print message to serial monitor
    sleep_disable();//Disable sleep mode
    detachInterrupt(0); //Removes the interrupt from pin 2;
}
  

//구성품 test를 위해 준비
void test_all()
{   
    while(1)
    {
       test_buzzer();
    }
}

void set_buzzer1()
{
    tone(buzzer, 2000);
    delay(200);
    noTone(buzzer); 
    delay(3000);
}

void set_buzzer2()
{
    // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 8; thisNote++) {
    // to calculate the note duration, take one second divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(buzzer, melody[thisNote], noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:

    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    
    // stop the tone playing:
    noTone(buzzer);
  }
}

void test_buzzer()
{
    tone(buzzer, 2000);
    delay(200);
    noTone(buzzer); 
    delay(3000);
}

void test_humansensor()
{
    if(digitalRead(human))
    {
        Serial.println("Human Detect");
    }
    else
    {
        Serial.println("No human");
    }
}

void test_light_analog()
{
    while(1)
    {
        inputValue = analogRead(light);     /* read input value of A5 pin */
    
        Serial.println(inputValue);
        test_light_digital();
        delay(100);
        //analog light <250 - light on
        //analog light > 250 - light off
    }
}

void test_light_digital()
{
    if(digitalRead(light_digital))
    {
        Serial.println("light_digital HIGH"); //light OFF
    }
    else
    {
        Serial.println("light_digital LOW"); //light ON - 0~150~200
    }
}

 

 

 

 

 

 

반응형