본문 바로가기

메이커 Maker

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

반응형

 

 

역시 매듭을 짓는 게 좋은 생각이라고 생각한다. 어려운 일도 아니라서, 꼭 필요한 건 아니라서, 제품을 만들기도 애매하다고 중단하고 포기하는 핑계를 댄다. 마무리는 언제든 해도 된다. 포스팅 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
    }
}

 

 

 

 

 

 

반응형

캐어랩 고객 지원

취업, 창업의 막막함, 외주 관리, 제품 부재!

당신의 고민은 무엇입니까? 현실과 동떨어진 교육, 실패만 반복하는 외주 계약, 아이디어는 있지만 구현할 기술이 없는 막막함.

우리는 알고 있습니다. 문제의 원인은 '명확한 학습, 실전 경험과 신뢰할 수 있는 기술력의 부재'에서 시작됩니다.

이제 고민을 멈추고, 캐어랩을 만나세요!

코딩(펌웨어), 전자부품과 디지털 회로설계, PCB 설계 제작, 고객(시장/수출) 발굴과 마케팅 전략으로 당신을 지원합니다.

제품 설계의 고수는 성공이 만든 게 아니라 실패가 만듭니다. 아이디어를 양산 가능한 제품으로!

귀사의 제품을 만드세요. 교육과 개발 실적으로 신뢰할 수 있는 파트너를 확보하세요.

지난 30년 여정, 캐어랩이 얻은 모든 것을 함께 나누고 싶습니다.

카카오 채널 추가하기

카톡 채팅방에서 무엇이든 물어보세요

귀사가 성공하기까지의 긴 고난의 시간을 캐어랩과 함께 하세요.

캐어랩 온라인 채널 바로가기

캐어랩