BASIC4MCU | 질문게시판 | 아두이노 레오나르도와 tone함수 잘문드립니다!!
페이지 정보
작성자 아야어여 작성일2019-02-27 11:15 조회1,111회 댓글0건본문
현재 아두이노 레오나르도와 블루투스를 연결했고, 해드폰과 연동을 해서 정보를 주고 받는 것 까지는 만들었습니다.
여기서 핸드폰과 연결 시에 tone함수를 이용해서 소리를 내게 하고 싶은데
setup문에 들어가게되면 처음 연결되지 않았을 때만 소리가 나고 연결이 된 이후에는 소리가 나지 않으며
loop문에 들어가게되면 처음 연결되지 않았을 때만 무한정 소리가 나게 됩니다.
원하는 방법은 처음에는 연결이 되지 않은 상태에서는 소리가 나징 않았다가 연결이 된 후 소리가 나게 하고
다시 연결이 해제 하면 소리가 나는 것을 구현하고 싶은데 잘 되지 않아 질문 드립니다..
#include <SoftwareSerial.h>
SoftwareSerial voiceSerial(8, 7);
SoftwareSerial BTSerial(0, 1);
int speakerpin = 12; //스피커가 연결된 디지털핀 설정
int note[] = {2093,2349,2637,2793,3136,3520,3951,4186}; //도레미파솔라시도
int note1[] = {4186,3951,3520,3136,2793,2637,2349,2093}; //도레미파솔라시도
void setup(){
voiceSerial.begin(9600);
voiceSerial.listen();
BTSerial.begin(9600);
}
char id, accuracy;
void loop()
{
int elementCount = sizeof(note) / sizeof(int);
int elementCount1 = sizeof(note1) / sizeof(int);
if(BTSerial.available()){for (int i=0; i < elementCount; i++) //note를 play
{tone(speakerpin,note[i],500); delay(3000);}}///연결 되었을 때
else{for(int i=0; i < elementCount1; i++) //note를 play
{tone(speakerpin,note1[i],500);delay(500);}}/// 연결 되지 않았을 때
////////////////////////소리 나게 하는 함수
if (voiceSerial.available()) {
id = voiceSerial.read();
accuracy = voiceSerial.read();
switch (id) {
case '0': // STOP
BTSerial.println("0");
break;
case '1': // RED
BTSerial.println("1");
break;
case '2': // GREEN
BTSerial.println("2");
break;
case '3': // BLUE
BTSerial.println("3");
break;
}
}
}
댓글 0
조회수 1,111등록된 댓글이 없습니다.