BASIC4MCU | 질문게시판 | 아두이노 시간 범위 설정
페이지 정보
작성자 moonlight 작성일2021-06-07 19:31 조회5,735회 댓글3건본문
1. RTC로 컴퓨터 시간 받아오기
2. LCD에 시간 출력
3. 사운드 레벨미터로 소음 측정
4. 06시~22시에 57dB 이상이면 피에조 부저 ON & LCD에 시간과 dB 크기 출력
22시~06시에 52dB 이상이면 피에조 부저 ON & LCD에 시간과 dB 크기 출력
1,2,3 번까지는 해결이 된 것 같은데
4번에서 막혔습니다.
06시를 저장하는 함수 1개 (morning)
22시를 저장하는 함수 1개 (night)
이렇게 선언해서
if (now > morning)
{ if (now<night)
{ if (dbValue >= 57)
{ tone (speakerpin,500,1000);
}
}
}
이런식으로 중복 if문을 사용하여 해결하려 했는데 시간을 저장하는 함수를 어떻게 사용해야 할지 모르겠습니다.
시간 범위 나누는 부분 좀 도와주세요
아래는 4번부분 빼고 제가 작성한 코드입니다.
==============================================================================================
#include <ThreeWire.h>
#include <RtcDS1302.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);ThreeWire myWire(4,5,2); // IO, SCLK, CE
#define SoundSensorPin A1 //this pin read the analog voltage from the sound level meter
#define VREF 5.0 //voltage on AREF pin,default:operating voltage
//RTC 라이브러리 생성
RtcDS1302<ThreeWire> Rtc(myWire);
int speakerpin = 12; // 피에조 부저가 연결된 디지털 핀번호
void setup ()
{
Serial.begin(57600);lcd.init();
lcd.backlight();//컴파일 시점의 날짜(__DATE__)와 시간(__TIME__)을 시리얼모니터에 표시
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);//RTC 모듈 라이브러리 시작
Rtc.Begin();//RTCDateTime 클래스 생성(컴파일된 시간으로 설정)
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
Serial.println();
//RTC모듈에 쓰기 금지 모드인지 확인
if (Rtc.GetIsWriteProtected())
{
Serial.println("RTC was write protected, enabling writing now");
//쓰기 금지 모드이면 해제
Rtc.SetIsWriteProtected(false);
}
//RTC 모듈이 동작중이 아니라면
if (!Rtc.GetIsRunning())
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}// RTC 모듈의 현재 시간 얻기
RtcDateTime now = Rtc.GetDateTime();//RTC 모듈하고 위에 만든 RTC클래스(컴파일된 시간을 가지고 있는)와 비교해서
//RTC 시간이 더 늦다면..
if (now < compiled)
{
//RTC가 컴파일 시간보다 느리다.
Serial.println("RTC is older than compile time! (Updating DateTime)");
//컴파일 시간을 RTC 모듈에 적용
Rtc.SetDateTime(compiled);
}
//RTC 모듈이 컴파일한 시간보다 빠르다면
else if (now > compiled)
{
//RTC가 캄파일 시간보다 더 새것이다
Serial.println("RTC is newer than compile time. (this is expected)");
}
//RTC와 컴파일한 시간이 같다면.
else if (now == compiled)
{
//RTC와 컴파일 시간이 같다.
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
}
}void loop ()
{
// 사운드 레벨미터로 소음 측정
float voltageValue,dbValue;
voltageValue = analogRead(SoundSensorPin) / 1024.0 * VREF;
dbValue = voltageValue * 50.0; //convert voltage to decibel value
Serial.print(dbValue,1);
Serial.println(" dBA");
delay(125);
//RTC 모듈의 현재 시간 얻기
RtcDateTime now = Rtc.GetDateTime();
//시리얼 모니터에 출력
printDateTime(now);
//줄 바꿈.
Serial.println();
//1초 대기...후 다시 loop 시작
delay(1000); // 1초
}#define countof(a) (sizeof(a) / sizeof(a[0]))
//시리얼 모니터에 날짜 시간 표시하는 함수
void printDateTime(const RtcDateTime& dt)
{
char datestring[20];snprintf_P(datestring,
countof(datestring),
PSTR("%02u:%02u:%02u"), dt.Hour(), dt.Minute(), dt.Second() );
Serial.print(datestring);
lcd.setCursor(0,0);
lcd.print(datestring);
}
댓글 3
조회수 5,735master님의 댓글
master 작성일
#include <ThreeWire.h>
#include <RtcDS1302.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
ThreeWire myWire(4,5,2); // IO,SCLK,CE
//
RtcDS1302<ThreeWire> Rtc(myWire);
#define countof(a) (sizeof(a)/sizeof(a[0]))
//
#define SoundSensorPin A1
//
int speakerpin=12;
//
char datestring[20];
int s,m,h;
//
void setup(){
Serial.begin(57600);
lcd.init(); lcd.backlight();
//
Serial.print("compiled: "); Serial.print(__DATE__); Serial.println(__TIME__);
//
Rtc.Begin();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
if (Rtc.GetIsWriteProtected()){ Serial.println("RTC was write protected, enabling writing now"); Rtc.SetIsWriteProtected(false); }
if (!Rtc.GetIsRunning()){ Serial.println("RTC was not actively running, starting now"); Rtc.SetIsRunning(true); }
RtcDateTime now = Rtc.GetDateTime();
if(now<compiled){ Serial.println("RTC is older than compile time! (Updating DateTime)"); Rtc.SetDateTime(compiled); }
else if(now> compiled)Serial.println("RTC is newer than compile time. (this is expected)");
else if(now==compiled)Serial.println("RTC is the same as compile time! (not expected but all is fine)");
}
//
void loop(){
RtcDateTime now=Rtc.GetDateTime();
h=dt.Hour(); m=dt.Minute(); s=dt.Second();
snprintf_P(datestring,countof(datestring),PSTR("%02u:%02u:%02u"),h,m,s);
Serial.println(datestring);
lcd.setCursor(0,0); lcd.print(datestring);
//
float voltageValue=analogRead(SoundSensorPin)/1023.0*5.0;
float dbValue=voltageValue*50.0;
Serial.print(dbValue,1); Serial.println(" dBA");
lcd.setCursor(0,1); lcd.print(datestring); lcd.print(" dBA ");
//
// 06시~22시에 57dB 이상이면 피에조 부저 ON
if((h>=06)&&(h>=22)){
if(dbValue>=57)tone(speakerpin,500,1000); else notone(speakerpin);
}
// 22시~06시에 52dB 이상이면 피에조 부저 ON
if((h>22)||(h<06)){
if(dbValue>=52)tone(speakerpin,500,1000); else notone(speakerpin);
}
//
delay(1000); // 1초
}
moonlight님의 댓글
moonlight
답변 감사합니다.
loop문 2번째줄 h=dt.Hour(); m=dt.Minute(); s=dt.Second(); 에서
'dt' was not declared in this scope 라는 오류메세지가 뜨는데 왜 그러는건가요?
master님의 댓글
master
dt 대신 now로 변경하면 됩니다.
아니면 now를 dt로 변경해도 되겠죠