BASIC4MCU | 질문게시판 | 아두이노 블루투스 연결 관련
페이지 정보
작성자 wait 작성일2019-06-10 15:07 조회8,853회 댓글3건본문
안녕하세요 전에 동작이 안되서 질문을 한 번드렸었습니다.
그 후로 그것을 기초로 하다보니 이제 스테핑모터가 조건에 따라 잘 작동하였습니다.
이제 마지막으로 블루투스를 연결하여 조건 상관없이 OPEN이나 CLOSE를 누르면 열리거나 닫히는 것이 목표입니다.
그래서 앱인벤터를 만들었고, 이제 블루투스 소스코드만 연결을 하면되는데 이것이 맞는지 잘 모르겠습니다.
위에 블루투스 연결 핀을 확인하고 맨마지막 부분에 정지 오픈 클로우즈로 생각을 해보았는데 소스코드를 잘 못 짠 것같습니다. 시간이 되실때 한 번 검토 부탁드리겠습니다!
#include <SoftwareSerial.h>
int blueTx=6;
int blueRx=7;
SoftwareSerial mySerail(blueTx,blueRx);#include <Stepper.h>
const int TMP36_pin = A1; //온도
const int stepsPerRevolution = 200; //회전수 200/한바퀴
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
int ENA=2;
int ENB=3;
int ondo1 = 37;
int ondo2 = 35;
int gas1 = 250;
int gas2 = 200;
boolean window = false; //false -> 닫혀있는상태
//true -> 열려있는상태
const int gasPin = A0; // 가스
void setup()
{
myStepper.setSpeed(60);
Serial.begin(9600);
pinMode(ENA,OUTPUT); //
pinMode(ENB,OUTPUT);//
pinMode(2, INPUT);pinMode(3, INPUT);
digitalWrite(ENA,HIGH);//
digitalWrite(ENB,HIGH);//
}
void loop()
{
int sensorValue = analogRead(TMP36_pin);
float mVoltage = sensorValue * 5000.0/1024.0;
float temperatureC = (mVoltage - 500) / 10.0;
int value = digitalRead(5);
Serial.print("Now : ");
Serial.print(temperatureC);
Serial.println(" C");
Serial.println();
Serial.print("current gas : ");
Serial.println(analogRead(gasPin));
Serial.println();
Serial.print("Rain : ");
Serial.println(value);
Serial.print("Window : ");
if(window==false) Serial.println("Close");
else if(window==true) Serial.println("Open");
Serial.println("=============================================== ");
Serial.println();
if(window == false && value==1) //창문이 닫혀있고 비가 안오는 상태
{
if(temperatureC<=ondo2 && analogRead(gasPin)>=gas1)
{
sensor_open();
}
else if(temperatureC>=ondo1 && analogRead(gasPin)>=gas1)
{
sensor_open();
}
else if(temperatureC<=ondo2 && analogRead(gasPin)<=gas2)
{
// 동작안함
}
else if(temperatureC>=ondo1 && analogRead(gasPin)<=gas2)
{
sensor_open();
}
}
else if(window == false && value==0) //창문이 닫혀있고 비가 오는 상태
{
if(temperatureC<=ondo2 && analogRead(gasPin)>=gas1)
{
sensor_open();
}
else if(temperatureC>=ondo1 && analogRead(gasPin)>=gas1)
{
sensor_open();
}
else if(temperatureC<=ondo2 && analogRead(gasPin)<=gas2)
{
// 동작안함
}
else if(temperatureC>=ondo1 && analogRead(gasPin)<=gas2)
{
//동작안함
}
else
{
//동작안함
}
}
else if(window == true && value==1) //창문이열려있고 비가 안오는 상태
{
if(temperatureC<=ondo2 && analogRead(gasPin)>=gas1)
{
//동작안함
}
else if(temperatureC>=ondo1 && analogRead(gasPin)>=gas1)
{
//동작안함
}
else if(temperatureC<=ondo2 && analogRead(gasPin)<=gas2)
{
sensor_close();
}
else if(temperatureC>=ondo1 && analogRead(gasPin)<=gas2)
{
//동작안함
}
}
else if(window == true && value==0) //창문이열려있고 비가 오는 상태
{
if(temperatureC<=ondo2 && analogRead(gasPin)>=gas1)
{
//동작안함
}
else if(temperatureC>=ondo1 && analogRead(gasPin)>=gas1)
{
//동작안함
}
else if(temperatureC<=ondo2 && analogRead(gasPin)<=gas2)
{
sensor_close();
}
else if(temperatureC>=ondo1 && analogRead(gasPin)<=gas2)
{
sensor_close();
}
else
{
sensor_close();
}
}
delay(5000);
}
void loop(){
if(Serial.available()){
char in_data=Serial.read();
btSerial.write(in_data);
if(in_data=='0'){
digitalWrite(in1,LOW); digitalWrite(in2,LOW);
digitalWrite(in3,LOW); digitalWrite(in4,LOW);
}
if(in_data=='1'){
digitalWrite(in1,HIGH); digitalWrite(in2,LOW);
digitalWrite(in3,HIGH); digitalWrite(in4,LOW);
}
if(in_data=='2'){
digitalWrite(in1,LOW); digitalWrite(in2,HIGH);
digitalWrite(in3,LOW); digitalWrite(in4,HIGH);
}
}
void sensor_open(){
Serial.println("open");
myStepper.step(stepsPerRevolution);
Serial.println("stop");
window = true;
Serial.print("Window Status : ");
Serial.println(window);
Serial.println();
}
void sensor_close()
{
Serial.println("Close");
myStepper.step(-stepsPerRevolution);
window = false;
Serial.print("Window Status : ");
Serial.println(window);
Serial.println();
}
댓글 3
조회수 8,853master님의 댓글
master 작성일
답변을 드린 것 같은데 같은 질문이 계속 올라오네요?
이 질문까지 3개를 본 것 같습니다.
master님의 댓글
master 작성일
루프에서 반복적으로
센서 값을 읽어서 OPEN CLOSE를 한다고 해보죠
이 상태에서 블루투스로 OPEN CLOSE를 하면 단 1회만 실행합니다.
루프의 마지막을 지난 후 다음번 루프에서는 센서 값으로 OPEN CLOSE를 하기 때문에 블루투스 제어는 별 효과를 얻지 못합니다.
//
블루투스 명령이 제대로 실행되기 위해서는 자동(센서값에 의한) 과 수동(블루투스제어) 두 가지로 구분할 필요가 있습니다.
블루투스 제어를 받으면 수동으로 모드를 변경해서 센서값을 더이상 읽지 않도록 만들면 되고
자동(센서값에 의한)으로 다시 돌리기 위해서는 블루투스 명령으로 모드를 자동으로 변경해주면 됩니다.
master님의 댓글
master 작성일
int mide=0; // 자동모드
void loop(){
if(mode==0){ // 자동모드이면
// 센서값을 읽어서 OPEN CLOSE를 제어
}
//
if (mySerial.available()) { //블루투스에서 넘어온 데이터가 있다면
char r=mySerial.read();
if(r=='O'){ mode=1; } // OPEN 제어
if(r=='C'){ mode=1; } // CLOSE 제어
if(r=='A'){ mode=0; } // AUTO
}
}
이런식이 되는 것이죠