BASIC4MCU | 질문게시판 | 아두이노 스피커 출력관련해서 질문드립니다
페이지 정보
작성자 민석 작성일2022-11-21 19:32 조회1,121회 댓글1건본문
ino 파일 남겨드립니다
아두이노 우노, 와이파이 쉴드 정품, PIR센서 사용중입니다
오픈웨더 맵에서 날씨정보를 받아 LED로 출력하는 코드인데,
LED를 출력할 때 음성코드가 나오게 만들고 싶습니다.
아두이노를 접한지 얼마 안된 초보입니다..
선배님들이 작은 도움이라도 주시면 감사하겠습니다!
아래 음성코드가 type2 때 출력되게 만들고 싶습니다
const uint8_t spDANGER[] PROGMEM = {0x82, 0xc3, 0x4f, 0xc5, 0x5a, 0xa9, 0x16, 0x91, 0x3e, 0x67, 0x6b, 0x37, 0x36, 0x24, 0x76, 0x83, 0xad, 0xdc, 0xe8, 0x92, 0xd9, 0x28, 0x8e, 0x36, 0x23, 0x4d, 0x1, 0xb5, 0x52, 0xa3, 0x2, 0x17, 0x9, 0xcc, 0x6a, 0xc9, 0x14, 0x7c, 0x44, 0x56, 0xca, 0xab, 0xcc, 0xc8, 0x11, 0x79, 0xa, 0xaf, 0x36, 0x23, 0x4d, 0x24, 0xc9, 0x23, 0x2b, 0x8c, 0x1c, 0x81, 0xe7, 0xc8, 0x2c, 0x17, 0x72, 0x4, 0x9e, 0x2a, 0x2a, 0xdd, 0xc8, 0xe5, 0x97, 0xc0, 0x72, 0x65, 0x86, 0x9, 0x6d, 0x2, 0xa8, 0xbd, 0xec, 0x6e, 0xaa, 0x89, 0xb0, 0xf4, 0x8a, 0x85, 0xa9, 0xa6, 0xc0, 0xd4, 0x23, 0x1a, 0xa1, 0x8b, 0x9, 0x73, 0xd, 0x4b, 0x98, 0xe, 0xe, 0xd8, 0xd5, 0x64, 0x21, 0x5a, 0x79, 0xe0, 0x7b, 0x60, 0xb8, 0x54, 0xe4, 0x83, 0x1f, 0xa6, 0xe2, 0x66, 0x1, 0x85, 0x58, 0x7a, 0x78, 0x84, 0xe3, 0xc4, 0x15, 0x22, 0x16, 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0xb8, 0x94, 0xa5, 0xdc, 0x91, 0xe0, 0x52, 0x19, 0x26, 0x54, 0x8f, 0x24, 0x5d, 0x55, 0x85, 0xa, 0x39, 0x86, 0x71, 0x55, 0x9d, 0x66, 0xf8, 0x3a, 0xc7, 0xd5, 0xa1, 0x32, 0xd3, 0x5, 0x1f, 0xd7, 0x66, 0xb7, 0x2c, 0x33, 0xb2, 0x5c, 0x5b, 0x43, 0x62, 0xcc, 0xe8, 0x72, 0x4d, 0xb, 0xb6, 0x9, 0x23, 0xd3, 0x69, 0xa6, 0x9e, 0x41, 0xb6, 0x2e, 0xb4, 0xdb, 0xc5, 0x55, 0xcb, 0xd0, 0x57, 0xc5, 0x6, 0x57, 0x1c, 0xa2, 0x1a, 0x92, 0xa6, 0x7c, 0x7a, 0x40, 0xa7, 0x48, 0x1e, 0x49, 0xc0, 0x62, 0xb2, 0xe, 0x33, 0x0, 0x0, 0x0, 0x0, 0x70};
----------------------------------------------------------------------------------------------------------------
아래 코드에는 led를 출력하는데 문제없는 코드입니다!
-----------------------------------------------------------------------------------------------------------------
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include <WiFiUdp.h>
#include "SPI.h"
#include "WiFi.h"
#include <Adafruit_NeoPixel.h>
#include "talkie.h"// 헤더만 작성
Talkie voice;
char ssid[] = ".."; //이파이 SSID
char pass[] = "0000001297"; //와이파이 password
#define VARID "a03439e4b83b60633b757b88a0d629a5"
//인스턴스 변수 초기화
WiFiServer server(80);
WiFiClient client;
const unsigned long requestInterval = 60000; // 요구 시간 딜레이(1 min)
IPAddress hostIp;
uint8_t ret;
unsigned long lastAttemptTime = 0; // 마지막으로 서버에서 데이터를 전송받은 시간
String currentLine = "";
String weatherString = "";
String timeString = "";
String location = "Uijeongbu-si";
boolean readingWeather = false;
boolean readingTime = false;
boolean stringComplete = false;
int weather;
int temp = 0;
int pin = 9;
int lednum = 11;
int val=0;
bool flag=false;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(lednum, pin, NEO_GRB + NEO_KHZ800);
int sw = 5;
const uint8_t spDANGER[] PROGMEM = {0x2D, 0xBF, 0x21, 0x92, 0x59, 0xB4, 0x9F, 0xA2, 0x87, 0x10, 0x8E, 0xDC, 0x72, 0xAB, 0x5B, 0x9D, 0x62, 0xA6, 0x42, 0x9E, 0x9C, 0xB8, 0xB3, 0x95, 0x0D, 0xAF, 0x14, 0x15, 0xA5, 0x47, 0xDE, 0x1D, 0x7A, 0x78, 0x3A, 0x49, 0x65, 0x55, 0xD0, 0x5E, 0xAE, 0x3A, 0xB5, 0x53, 0x93, 0x88, 0x65, 0xE2, 0x00, 0xEC, 0x9A, 0xEA, 0x80, 0x65, 0x82, 0xC7, 0xD8, 0x63, 0x0A, 0x9A, 0x65, 0x5D, 0x53, 0xC9, 0x49, 0x5C, 0xE1, 0x7D, 0x2F, 0x73, 0x2F, 0x47, 0x59, 0xC2, 0xDE, 0x9A, 0x27, 0x5F, 0xF1, 0x8B, 0xDF, 0xFF, 0x03};
void setup() {
pinMode(6, INPUT);
//각 변수에 정해진 공간 할당
Serial.begin(9600);
strip.begin();
strip.show();
delay(10);
//WiFi연결 시도
Serial.println("Connecting to WiFi....");
WiFi.begin(ssid, pass); //WiFi가 패스워드를 사용한다면 매개변수에 password도 작성
server.begin();
Serial.println("Connect success!");
Serial.println("Waiting for DHCP address");
//DHCP주소를 기다린다
while(WiFi.localIP() == INADDR_NONE) {
Serial.print(".");
delay(300);
}
Serial.println("\n");
printWifiData();
connectToServer();
}
void loop()
{
int button=digitalRead(sw);
//if(button == 0){
if (client.connected()) {
//Serial.print("check");
while (client.available()) {
//전송된 데이터가 있을 경우 데이터를 읽어들인다.
char inChar = client.read();
// 읽어온 데이터를 inChar에 저장한다.
currentLine += inChar;
//inChar에 저장된 Char변수는 currentLine이라는 String변수에 쌓이게 된다.
//라인피드(줄바꿈)문자열이 전송되면 데이터를 보내지 않는다.
if (inChar == '\n') {
//Serial.print("clientReadLine = ");
//Serial.println(currentLine);
currentLine = "";
}
if ( currentLine.endsWith("<weather number=")) {
// 현재 스트링이 "<pressure value="로 끝났다면 기압 데이터를 받을 준비를 한다.
readingWeather = true;
weatherString = "";
}
if (readingWeather) {
if (inChar != 'v') { //다음 전송될 문자가 'v'가 아니라면 계속 기압데이터를 받는다.
weatherString += inChar;
}
else { //다음 전송된 문자가 'u'라면 기압데이터를 출력한다.
readingWeather = false;
weather = getInt(weatherString);
Serial.print("weather code: ");
Serial.println(weather);
weatherpixel();
Serial.print("weather code: ");
Serial.println(weather);
delay(10000);
if (true){
Serial.println("cheak");
strip.clear();
strip.show();
layfu();
}
}
}
if ( currentLine.endsWith("</current>")) { //현재 스트링이 </current>로 끝났다면 연결을 끊고 다시 서버와 연결을 준비한다.
delay(100); //10초뒤에 서버와 연결을 끊고 재연결을 시도한다.
client.stop();
// serialEvent();
// if (stringComplete) {
// Serial.println(location);
// // clear the string:
// stringComplete = false;
// }
connectToServer();
//Serial.println("Disconnected from Server.\n");
}
}
}
else if (millis() - lastAttemptTime > requestInterval) {
//연결을 실패했다면 requestInterval(60초)이후에 다시 연결을 시도한다.
connectToServer();
}
flag =false;
}
// }
void layfu(){
int pp=0;
while (pp==0){
delay(1000);
if (digitalRead(6) == HIGH){
pp=1;
Serial.println("pir high");
}
}
setup();
loop();
}
//서버와 연결
void connectToServer() {
Serial.println("");
Serial.println("connecting to server...");
client.flush();
client.stop();
String content = "";
if (client.connect(hostIp, 80)) {
Serial.println("Connected! Making HTTP request to api.openweathermap.org for "+location+"...");
//Serial.println("GET /data/2.5/weather?q="+location+"&mode=xml");
client.print("GET /data/2.5/weather?q=Uijeongbu-si&mode=xml&appid=");
client.print(VARID);
client.println(" HTTP/1.1");
client.println("Host: api.openweathermap.org");
client.println("Connection: close");
client.println();
Serial.println("Weather information for "+location);
}
//마지막으로 연결에 성공한 시간을 기록
lastAttemptTime = millis();
}
void printHex(int num, int precision) {
char tmp[16];
char format[128];
sprintf(format, "%%.%dX", precision);
sprintf(tmp, format, num);
Serial.print(tmp);
}
void printWifiData() {
// Wifi쉴드의 IP주소를 출력
Serial.println();
Serial.println("IP Address Information:");
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
//MAC address출력
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
printHex(mac[5], 2);
Serial.print(":");
printHex(mac[4], 2);
Serial.print(":");
printHex(mac[3], 2);
Serial.print(":");
printHex(mac[2], 2);
Serial.print(":");
printHex(mac[1], 2);
Serial.print(":");
printHex(mac[0], 2);
Serial.println();
//서브넷 마스크 출력
IPAddress subnet = WiFi.subnetMask();
Serial.print("NetMask: ");
Serial.println(subnet);
//게이트웨이 주소 출력
IPAddress gateway = WiFi.gatewayIP();
Serial.print("Gateway: ");
Serial.println(gateway);
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
ret = WiFi.hostByName("api.openweathermap.org", hostIp);
Serial.print("ret: ");
Serial.println(ret);
Serial.print("Host IP: ");
Serial.println(hostIp);
Serial.println("");
}
int getInt(String input){ //String데이터를 intger형으로 변환하는 함수
int i = 2;
while(input[i] != '"'){
i++;
}
input = input.substring(2,i);
char carray[20];
//Serial.println(input);
input.toCharArray(carray, sizeof(carray));
//Serial.println(carray);
temp = atoi(carray);
//Serial.println(temp);
return temp;
}
void weatherpixel(){
float rantime = random(20,50);
//비
if(weather>199 && weather<623){
type3();
}
else ;
//기타주의
if(weather>699 && weather <782){
type4();
}
else ;
//구름
if(weather>800 && weather<805){
type2();
}
else ;
//맑음
if(weather>799 && weather<801){
type1();
}
else ;
}
void type1(){
rainbow(15);
}
void type2(){
for(int i=0; i<strip.numPixels(); i++) { // For each pixel...
strip.setPixelColor(0,150,150,0);
strip.show();
delay(10);
}
}
void type3(){
for(int i=0; i<strip.numPixels(); i++) { // For each pixel...
strip.setPixelColor(0,0,150,150);
strip.show();
delay(10);
}
}
void type4(){
for(int i=0; i<strip.numPixels(); i++) { // For each pixel...
strip.setPixelColor(150,0,0,150);
strip.show();
delay(10);
}
}
void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
strip.show();
delay(wait);
}
}
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
댓글 1
조회수 1,121master님의 댓글
master 작성일
spDANGER[] 음성코드는 PWM으로 출력하나요?
8KHz(125us주기) 샘플링으로 만든 코드라면 125us딜레이를 가지고 출력하면 됩니다만
아두이노 기본 PWM주파수가 이보다 훨씬 느리므로 짧은 주기의 PWM을 설정하는 법을 먼저 공부하셔야겠습니다.
1. 음성데이터의 샘플링 주파수가 얼마인지 알아본다.
2. 상기 샘플링 주파수 이상의 PWM 주기를 설정할 수 있도록 공부한다. 주기가 동일하거나 1/n배 이면 더 좋다.
3. 음성데이터의 출력을 공부 한 후
4. 합쳐서 완성시킨다.
spDANGER[] 음성코드는 PWM으로 출력하나요?
8KHz(125us주기) 샘플링으로 만든 코드라면 125us딜레이를 가지고 출력하면 됩니다만
아두이노 기본 PWM주파수가 이보다 훨씬 느리므로 짧은 주기의 PWM을 설정하는 법을 먼저 공부하셔야겠습니다.
1. 음성데이터의 샘플링 주파수가 얼마인지 알아본다.
2. 상기 샘플링 주파수 이상의 PWM 주기를 설정할 수 있도록 공부한다. 주기가 동일하거나 1/n배 이면 더 좋다.
3. 음성데이터의 출력을 공부 한 후
4. 합쳐서 완성시킨다.
음성 데이터를 어느 사이트에서 가져온 것이라면 해당 사이트의 설명글과 예제를 공부하세요