Hi I'm still a beginner, I need help for my final project, I'm trying created an application that can set temperature in arduino to open and close the window, but unfortunately the number sent to arduino is not accurate, and when the Bluetooth disconnect the set temperature gets reset.
any sugestion?
Here's the code
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <SoftwareSerial.h>
SoftwareSerial BTserial(12, 13); // RX , TX
#define DHTPIN 2
#define DHTTYPE DHT11 // DHT 11
DHT dht = DHT(DHTPIN, DHTTYPE);
const int ENA_PIN = 7; // the Arduino pin connected to the EN1 pin L298N
const int IN1_PIN = 6; // the Arduino pin connected to the IN1 pin L298N
const int IN2_PIN = 5; // the Arduino pin connected to the IN2 pin L298N
const int BUTTON_PIN8 = 8; // the number of the pushbutton pin
const int BUTTON_PIN9 = 9;
const int BUTTON_PIN10 = 10; // the number of the pushbutton pin
const int BUTTON_PIN11 = 11;
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0; // variable for reading the pushbutton status
int buttonState4 = 0;
int data = 0;
void setup() {
dht.begin();
Serial.begin(9600);
pinMode(4, OUTPUT);
pinMode(2, OUTPUT);
digitalWrite(ENA_PIN, HIGH);
BTserial.begin(9600);
// initialize the pushbutton pin as an pull-up input:
// the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed.
pinMode(BUTTON_PIN8, INPUT_PULLUP); //to manual
pinMode(BUTTON_PIN9, INPUT_PULLUP); //to automation
pinMode(BUTTON_PIN10, INPUT_PULLUP);
pinMode(BUTTON_PIN11, INPUT_PULLUP);
}
void loop() {
if (BTserial.available() > 0)
{
int data = (int) BTserial.read();
BTserial.print(data);
}
// read the state of the pushbutton value:
buttonState1 = digitalRead(BUTTON_PIN8);
buttonState2 = digitalRead(BUTTON_PIN9);
// control LED according to the state of button
if(buttonState1 == LOW) { // If button is pressing
Manual();
}
if (buttonState2 == LOW) { // If button is pressing
Automation();
}
}
void Manual(){
buttonState3 = digitalRead(BUTTON_PIN10);
buttonState4 = digitalRead(BUTTON_PIN11);
if(buttonState3 == LOW) { // If button is pressing
digitalWrite(IN1_PIN, LOW);
digitalWrite(IN2_PIN, HIGH);
int l = analogRead(A1);
int rain = analogRead(A0);
float t = dht.readTemperature();
float h = dht.readHumidity();
BTserial.print("MANUAL");
BTserial.print("-");
BTserial.print(h); //ito yung index(1) na ilalagay sa mit
BTserial.print("-");
BTserial.print(t);
BTserial.print("-"); //ito naman ay yung pang split
//index(2)
if (l < 10) {
BTserial.print("VERY LIGHT");
} else if (l < 200) {
BTserial.print("LIGHT");
} else if (l < 500) {
BTserial.print("BRIGHT");
} else if (l < 800) {
BTserial.print("DARK");
} else {
BTserial.print("DIM");
}
BTserial.print("-");
BTserial.print("OPEN");
}
if(buttonState4 == LOW) {
digitalWrite(IN1_PIN, HIGH);
digitalWrite(IN2_PIN, LOW);
int l = analogRead(A1);
int rain = analogRead(A0);
float t = dht.readTemperature();
float h = dht.readHumidity();
BTserial.print("MANUAL");
BTserial.print("-");
Serial.print(h); //ito yung index(1) na ilalagay sa mit
Serial.print("-");
Serial.print(t);
Serial.print("-"); //ito naman ay yung pang split
//index(2)
if (l < 10) {
BTserial.print("VERY LIGHT");
} else if (l < 200) {
BTserial.print("LIGHT");
} else if (l < 500) {
BTserial.print("BRIGHT");
} else if (l < 800) {
BTserial.print("DARK");
} else {
BTserial.print("DIM");
}
BTserial.print("-");
BTserial.print("CLOSE");
}
delay(1000);
}
void Automation(){
int data = (int) BTserial.read();
int l = analogRead(A1);
int rain = analogRead(A0);
float t = dht.readTemperature();
float h = dht.readHumidity();
BTserial.print("AUTOMATIC");
BTserial.print("-");
BTserial.print(h); //ito yung index(1) na ilalagay sa mit
BTserial.print("-");
BTserial.print(t);
BTserial.print("-"); //ito naman ay yung pang split
//index(2)
if (l < 10) {
BTserial.print("VERY LIGHT");
} else if (l < 200) {
BTserial.print("LIGHT");
} else if (l < 500) {
BTserial.print("BRIGHT");
} else if (l < 800) {
BTserial.print("DARK");
} else {
BTserial.print("DIM");
}
if (t < data) {
BTserial.print("-");
BTserial.print("CLOSE");
digitalWrite(IN1_PIN, HIGH);
digitalWrite(IN2_PIN, LOW);
delay(1000);
}
else if (rain < 500) {
//rain but high temp
//output in linear
BTserial.print("-");
BTserial.print("CLOSE");
digitalWrite(IN1_PIN, HIGH);
digitalWrite(IN2_PIN, LOW);
delay(1000);
}
else if (rain > 500) {
//sunny high temp
//output in linear
BTserial.print("-");
BTserial.print("OPEN");
digitalWrite(IN1_PIN, LOW);
digitalWrite(IN2_PIN, HIGH);
delay(1000);
} else {
digitalWrite(IN1_PIN, LOW);
digitalWrite(IN2_PIN, HIGH);
delay(1000);
}
}