BASIC4MCU | 질문게시판 | 블루투스와 소리센서를 이용한 무드등 질문있습니다!
페이지 정보
작성자 엽빵이 작성일2019-06-12 18:04 조회3,875회 댓글0건본문
소리센서로 RGB색상 변화
블루투스로 on/off및 타이머
로 하려고 해서 제가 만들어봤었는데 이 코드가 아에 작동이 안합니다.
수정해주시면 정말 이 고마운 마음을 가지고 다음에 다른이를 도와주는 사람이 되도록하겠습니다!! 부탁드립니다!!
//핀맵설정
const int LED_R_PIN = 10;
const int LED_G_PIN = 11;
const int LED_B_PIN = 9;
const int SOUND_PIN = A2;
// 전역변수
boolean button_is_activate = true; // 버튼 활성화 상태, 한번 길게 터치로 여러번 실행되는것을 막는다.
int button_step = 0; // 버튼 정지(0)
int SOUND_THRESHOLD = 600;
float red = 0; //0~255의 밝기 값
float blue = 170;
float green = 85;
float bright_control = 0.1;
float red_bright_control = 0.1;
float blue_bright_control = 0.1;
float green_bright_control = 0.1;
float x = 0;
#include <SoftwareSerial.h> //소프트웨어 시리얼 통신 함수가 정의됨
SoftwareSerial BTS(2,3); // (rx통신pin, tx통신pin) 로 정의됨
void setup() {
{
pinMode(A2,OUTPUT);
Serial.begin(9600);//통신속도 9600bps
BTS.begin(9600); //통신속도 설정 (핸드폰연결)
}
Serial.begin(9600);
pinMode(SOUND_PIN, INPUT);
pinMode(LED_R_PIN, OUTPUT);
pinMode(LED_G_PIN, OUTPUT);
pinMode(LED_B_PIN, OUTPUT);
}
void button_pressed() {
if (button_step == 0) { //흰색 켜짐
analogWrite(LED_R_PIN, 0);
analogWrite(LED_G_PIN, 0);
analogWrite(LED_B_PIN, 0);
}
else if (button_step == 1) { //흰색 밝기변화
analogWrite(LED_R_PIN, x);
analogWrite(LED_G_PIN, x);
analogWrite(LED_B_PIN, x);
x += bright_control;
if (x <= 0 || x > 255) bright_control = -bright_control;
// 0,255 값이 됬을때 bright_control를 반전시켜 반대로 동작하게 함.
delay(1);
}
else if (button_step == 2) { //3색숨쉬는LED
analogWrite(LED_R_PIN, red);
analogWrite(LED_G_PIN, blue);
analogWrite(LED_B_PIN, green);
red += red_bright_control;
blue += blue_bright_control;
green += green_bright_control;
if (red < 0 || red > 255) red_bright_control = -red_bright_control;
if (blue < 0 || blue > 255) blue_bright_control = -blue_bright_control;
if (green < 0 || green > 255) green_bright_control = -green_bright_control;
delay(1);
}
}
char cmd;
void loop() {
if( BTS.available() ) { //블루투스통신 가능한지 여부 체크
cmd = BTS.read(); //핸드폰에서 보내준 명령을 cmd로 설정
Serial.print("Command = ");
Serial.println(cmd);
switch(cmd){
case '1' : Serial.println("SOUND ON"); digitalWrite(A2,HIGH); break;
case '2' : Serial.println("SOUND OFF"); digitalWrite(A2,LOW); break;
default : Serial.println("default");
}
button_pressed();
Serial.println(analogRead(SOUND_PIN)); // 감도확인 사운드센서의 가변저항 돌리면서 감도를 맞춰줘야함
if (analogRead(SOUND_PIN) < SOUND_THRESHOLD) {
if (button_is_activate) {
button_step++;
if (button_step >= 3) {
button_step = 0;
}
button_is_activate = false; // 한번만 실행될 수 있게 버튼 비활성화
}
delay(2000);
}
else {
button_is_activate = true;
}
}
}
댓글 0
조회수 3,875등록된 댓글이 없습니다.