BASIC4MCU | 질문게시판 | 아두이노 블루투스 연결방법
페이지 정보
작성자 wait 작성일2019-06-04 22:56 조회4,119회 댓글0건본문
안녕하세요 전에 동작이 안되서 질문을 한 번드렸었습니다.
그 후로 그것을 기초로 하다보니 이제 스테핑모터가 조건에 따라 잘 작동하였습니다.
이제 마지막으로 블루투스를 연결하여 조건 상관없이 누르면 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();
}
댓글 0
조회수 4,119등록된 댓글이 없습니다.