BASIC4MCU | 질문게시판 | 아두이노 모드(2)
페이지 정보
작성자 Winavr 작성일2019-05-14 16:20 조회4,579회 댓글1건본문
저번에 알려주신대로 모드를 설정해서 바꿔보았습니다.
#include <SoftwareSerial.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#define DHTPIN 2 // 습도센서 Signal 선 연결
#define DHTTYPE DHT22 // 습도센서 종류: DHT22
DHT dht(DHTPIN,DHTTYPE);
LiquidCrystal_I2C lcd(0x3F,16,2); // LCD주소: 0x27 또는 0x3F
//
int analog=analogRead(A1);
int relay1=10; // 열선 릴레이
int relay2=11;
int motorPin=6; // 모터 PWM
int R=3; // 워터펌프 릴레이
float hum,temp;// 온습도 설정
int fan=0; // 팬 속도 설정
char mode=0;
SoftwareSerial bluetooth(8,9);
//
void setup(){
Serial.begin(9600);
bluetooth.begin(9600);
dht.begin();
lcd.init(); lcd.backlight(); lcd.clear();
pinMode(relay1,OUTPUT); // relay를 output으로 설정
pinMode(relay2,OUTPUT);
pinMode(motorPin,OUTPUT);
pinMode(R,OUTPUT);
while(!Serial);
Serial.println("Speed 0 to 255");
}
//
void loop(){
int analog=analogRead(A1);
hum=dht.readHumidity();
temp=dht.readTemperature();
int speed=Serial.parseInt();
if(Serial.available()){
if(speed>=0&&speed<=255){ analogWrite(motorPin,speed); }
}
if(bluetooth.available()){
char val=bluetooth.read(); Serial.write(val);
if(val=='a'){mode==0;}
if(val=='b'){mode==1;}
if(mode==1){
if(val=='c'){ fan=2; analogWrite(6,0); }
if(val=='d'){ fan=1; analogWrite(6,130); }
if(val=='e'){ fan=0; analogWrite(6,255); }
}
}
if(mode==0){
if (temp>29){ fan=2; analogWrite(6,0);}
else if(temp>28){ fan=1; analogWrite(6,130);}
else{ fan=0; analogWrite(6,255); }
if (analog<300){ digitalWrite(R,LOW); } //
else if(analog>300){ digitalWrite(R,HIGH); } //
}
Serial.print("HUMIDITY: "); Serial.print(hum,0); Serial.print("%,");
Serial.print("SOIL: "); Serial.print(analog);
Serial.print("TEMPERATURE: "); Serial.print(temp,0); Serial.println(" C");
lcd.setCursor(0,0);
lcd.print("TEM: "); lcd.print(temp,0);
lcd.print("C SOIL: "); lcd.print(analog);
lcd.setCursor(0,1); lcd.print("HUM: "); lcd.print(hum,0);
lcd.print("% FAN: "); lcd.print(fan);
bluetooth.print("hum : "); // ‘시리얼 플로터’ 사용위해 이부분 주석 필요
bluetooth.print(hum); // 습도값 출력
bluetooth.println("%");
bluetooth.print("temp : "); // ‘시리얼 플로터’ 사용위해 이부분 주석 필요
bluetooth.print(temp); // 온도값 출력
bluetooth.println("C");
bluetooth.print("Soil hum : "); // ‘시리얼 플로터’ 사용위해 이부분 주석 필요
bluetooth.print(analogRead(2)); // 습도값 출력
bluetooth.println("");
}
코딩은 잘 되는데 역시 컨트롤이 안됩니다. 이것도 모드를 설정하는거에 문제가 있나요?
댓글 1
조회수 4,579master님의 댓글
master 작성일
c언어 공부한지 일주일도 안됬나요?
if(val=='a'){mode==0;}
if(val=='b'){mode==1;}
조건문은 맞지만 치환문이 엉터리입니다.
if(val=='a'){mode=0;}
if(val=='b'){mode=1;}
기초문법 부터 막히면 설명을 해드릴 사람이 없을겁니다.