아두이노 슬레이브 코드 질문 합니다.
페이지 정보
작성자 쥬스 작성일19-05-27 13:28 조회3,320회 댓글0건본문
<슬레이브코드>
#include "pitches.h"
#include <LiquidCrystal.h>
#include<SoftwareSerial.h>
#include "pitches.h"
#include<SoftwareSerial.h>
#include <swRTC.h>
int counter=0;
char incomingByte ; // 수신 데이터를 저장하는 변수
SoftwareSerial mySerial(9,10); //블루투스의 Tx, Rx핀을 2번 3번핀으로 설정
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4};
LiquidCrystal lcd(12,11,5,4,3,2); //RS 핀, E핀, 데이터 핀 4개
String lcdString = ""; //객체 선언 : 출력 할 글자 저장
swRTC rtc;
int piezo = 8;
int switchPin = 9;
int temp;
//AM PM을 구분해 주는 함수
void Set_AMPM(int hour) {
if(hour >=12)
lcd.print("PM");
else
lcd.print("AM");
lcd.print(hour%12, DEC); //시간 출력
}
//10보다 작은수를 출력할때 앞에 0을 출력하게 하는 함수
void Set_lowThanTen(int time) {
if(time < 10) {
lcd.print("0");
lcd.print(time%10);
}
else
lcd.print(time);
}
//유효한 알람시간인지 체크하는 함수
int checkTheAlarmClock(int time) {
if(time/100 < 24 && time %100 < 60) {
Serial.println("Success");
return time;
}
else {
Serial.println("Failed");
return 0;
}
}
//알람이 울릴시간인지 체크하는 함수
void checkTheAlarmTime(int alarmHour, int alarmMinute) {
if(alarmHour == rtc.getHours() && alarmMinute == rtc.getMinutes()) {
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(8, 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(8);
}
}
}
void setup() {
pinMode(8, OUTPUT);
Serial.begin(9600); // 시리얼 통신의 속도를 9600으로 설정
while (!Serial) {
; //시리얼통신이 연결되지 않았다면 코드 실행을 멈추고 무한 반복
}
Serial.println("Hello World!");
mySerial.begin(9600); //블루투스와 아두이노의 통신속도를 9600으로 설정
lcd.begin(16,2); //LCD 크기 지정, 2줄 16칸
lcd.clear(); //화면 초기화
rtc.stopRTC(); //정지
rtc.setTime(10,05,30); //시간, 분, 초 초기화
rtc.setDate(27, 5, 2019); //일, 월, 년 초기화
rtc.startRTC(); //시작
pinMode(piezo, OUTPUT);
pinMode(switchPin, INPUT_PULLUP);
Serial.begin(9600); //시리얼 포트 초기화
Serial.begin(9600); //시리얼 통신 초기화
}
void loop() {
int day;
lcd.setCursor(0,0); //커서를 0,0에 지정
//1초 단위로 갱신하며 현재시간을 LCD에 출력
Set_AMPM(rtc.getHours());
lcd.print(":");
Set_lowThanTen(rtc.getMinutes());
lcd.print(":");
Set_lowThanTen(rtc.getSeconds());
//날짜를 LCD에 출력
lcd.print("[");
Set_lowThanTen(rtc.getMonth());
lcd.print("/");
Set_lowThanTen(rtc.getDay());
lcd.print("]");
//세팅된 알람시간을 LCD에 출력
lcd.setCursor(0,1);
lcd.print("Alarm ");
Set_AMPM(1000/100);
lcd.print(":");
Set_lowThanTen(0006%100);
//1초마다 LCD갱신
lcdString = ""; //문자열 초기화
lcd.print(" "); //전 글씨 삭제
delay(1000);
//알람이 울릴 시간인지 체크
checkTheAlarmTime(temp/100, temp%100);
//시리얼 통신을 통해 알람시간을 입력받고 시리얼 모니터에 출력
char theDay[4];
int i = 0;
if(Serial.available()) {
while(Serial.available()) {
theDay[i] = Serial.read();
i++;
}
day = atoi(theDay);
if(day/100 >= 12) {
Serial.print("PM");
Serial.print((day/100)-12);
}
else {
Serial.print("AM");
Serial.print(day/100);
}
Serial.print(":");
if(day%100 < 10)
Serial.print("0");
Serial.println(day%100);
temp = checkTheAlarmClock(day);
}
//스위치버튼이 눌렸을 경우 피에조센서의 소리를 0으로 하고 알람시간을 초기화 한다
if (mySerial.available()) { //블루투스에서 넘어온 데이터가 있다면
incomingByte = mySerial.read(); //날라온 데이터를 읽기
Serial.print(incomingByte);
//날라온 데이터를 읽기
if(incomingByte == '1')
counter++;}
if(counter==4)
while(free);
}
제가 일정수치 이상의 근전도 수치를 받으면 소리가 꺼지게 하고 싶은데
while(free);이 부분을
disital.write(8,LOW)
noTone(8)로 바꿔봣는데 스피커가 안꺼져서
loop가 완전 끝나는 코드를 넣엇습니다. 그랫더니 LCD의 현재시간 또한 꺼지는데 혹시 어떻게 해야 스피커만 꺼지게 할 수 있는지 알 수 있을까요? ㅠㅠㅠㅠ
댓글 : 0
조회수 : 3,320
등록된 댓글이 없습니다.