#include <SoftwareSerial.h>
#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
SoftwareSerial Genotronex(10, 11); //RX, TX
int ledpin = 13;
int BluetoothData;
String inData;
int C_Speed = 0;
int Mode = 1, M_Mode = 1;
#define RxPin 10
#define TxPin 11
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
String Speed = "";
char buf;
boolean stringComplete = false;
const int No1 = 5;
const int No2 = 6;
const int No3 = 7;
void setup() {
Serial.begin(9600);
Genotronex.begin(9600);
dht.begin();
pinMode(No1, OUTPUT);
pinMode(No2, OUTPUT);
pinMode(No3, OUTPUT);
delay(100);
digitalWrite(No1, LOW);
digitalWrite(No2, LOW);
digitalWrite(No3, LOW);
lcd.begin();
}
void loop() {
delay(500);
int t = dht.readTemperature();
String myString = String(t);
Genotronex.print("T");
Genotronex.print(myString);
if (Mode = 1)
Genotronex.print("A");
if (Mode == 0)
Genotronex.print("M");
if (C_Speed == 1)
Genotronex.print("S=1");
else if (C_Speed == 2)
Genotronex.print("S=2");
else if (C_Speed == 3)
Genotronex.print("S=3");
lcd.setCursor(0, 0);
if (Mode = 0)
lcd.print("Manual Mode ");
else if (Mode = 1)
lcd.print("Auto Mode ");
lcd.setCursor(0, 1);
lcd.print("T= ");
lcd.print(t);
lcd.print(" S= ");
lcd.print(C_Speed);
while (Genotronex.available() > 0)
Speed = Genotronex.read();
{
if (Speed == "1") {
C_Speed = 1;
}
else if (Speed == "2") {
C_Speed = 2;
}
else if (Speed == "3") {
C_Speed = 3;
}
else if (Speed == "0") {
C_Speed = 0;
}
else if (Speed == "4") {
M_Mode = 3;
}
else if (Speed == "5") {
M_Mode = 1;
}
Speed = "";
}
//Manual Mode
if (analogRead(0) > 300) {
C_Speed == 1;
Mode = 0;
}
else if (analogRead(1) > 300) {
C_Speed == 2;
Mode = 0;
}
else if (analogRead(2) > 300) {
C_Speed == 3;
Mode = 0;
}
else if ((analogRead(0) < 500) && (analogRead(1) < 500) && (analogRead(2) < 500) && (M_Mode == 1)) {
Mode = 1;
}
else if ((analogRead(0) > 500) || (analogRead(1) > 500) || (analogRead(2) > 500) || (M_Mode == 3)) {
Mode = 0;
}
//Auto Mode
if (Mode = 1) {
if (t >= 32) {
C_Speed = 3;
}
else if ((t <= 31) && (t > 28)) {
C_Speed = 2;
}
else if ((t <= 28) && (t >= 25)) {
C_Speed = 1;
}
else if (t < 25) {
C_Speed = 0;
}
if (C_Speed == 1) {
digitalWrite(No1, LOW);
digitalWrite(No2, HIGH);
digitalWrite(No3, HIGH);
}
else if (C_Speed == 2) {
digitalWrite(No1, HIGH);
digitalWrite(No2, LOW);
digitalWrite(No3, HIGH);
}
else if (C_Speed == 3) {
digitalWrite(No1, HIGH);
digitalWrite(No2, HIGH);
digitalWrite(No3, LOW);
}
else if (C_Speed == 0) {
digitalWrite(No1, HIGH);
digitalWrite(No2, HIGH);
digitalWrite(No3, HIGH);
}
}
}
ถ้าต้องการควบคุมผ่านโทรศัพท์ ใช้ ESP8266 ติดต่อทาง Wifi
หรือจะใช้ bluetooth เช่น HC-05
ติดต่อแบบ bluetooth สื่อสารแบบ SerialPort ตามโคดที่สอบถามมา
การใช้งาน ESP8266 มีแนะนำในคอร์สเรียน Arduino ESP8266 IoT ที่หน้าเว็บ
ทั้ง 2 วิธีสามารถออกแบบโปรแกรมให้สั่งงานตามที่ต้องการได้ครับ
ใช้ Hc-06ก็ได้ใช่ไหมครับ เเล้วในโค็ดมีอะไรผิดพลาดไหมครับ มือใหม่
ใช้ hc-06 ได้ครับ สำหรับโคดมีสอนพื้นฐานในคอร์สเรียน Arduino Starter ที่หน้าเว็บ ลองเล่นดูครับ