This code is for automatically opening and closing a window using a motor with senors such as dust, humidity, temperature, and rain sensor with bluetooth module through phone. With this code if I press 1,2 it would close or open the window and 3 would let the window open or close regarding the measures that the sensor reads. Right now pressing 3 would only simulate this once and then turn off untill I press it again So I would want this part else if (cmd == '3') to repeat itself until I press another cmd button so the window keeps opening and closing.
//////////////////////////////////////////////////////////////////////////
#include <SoftwareSerial.h>
#include <Stepper.h>
#include <DHT.h>
#define DHTPIN A1
#define DHTTYPE DHT11
#define D_IN 3
#define A_IN A0
int Txpin = 7;
int Rxpin = 8;
int stepsmotor = 1024;
const int stepsPerRevolution = 64;
SoftwareSerial BTSerial(Txpin, Rxpin);
DHT dht(DHTPIN, DHTTYPE);
int measurePin = 0; // measurePin을 0으로 설정합니다.
int ledPower = 2; // ledPower를 2로 설정합니다.
int samplingTime = 280; // samplingTime을 280으로 설정합니다.
int deltaTime = 40; // deltaTime을 40으로 설정합니다.
int sleepTime = 9680; // sleepTime을 9690으로 설정합니다.
float voMeasured = 0; // voMeasured를 0으로 설정합니다.
float calcVoltage = 0; // calcVoltage를 0으로 설정합니다.
float dustDensity = 0; // dustDensity를 0으로 설정합니다.
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);
void setup()
{
Serial.begin(9600);
BTSerial.begin(9600);
myStepper.setSpeed(550);
pinMode(ledPower,OUTPUT);
pinMode(D_IN, INPUT);
}
void loop()
{
if (BTSerial.available())
{
char cmd = (char)BTSerial.read();
if(cmd == '1'){
for(int i=0; i<(32*3-6); i++){
myStepper.step(stepsPerRevolution);
}}
else if (cmd == '2')
for(int i=0; i<(32*3-6); i++){
myStepper.step(-stepsPerRevolution);
}
else if (cmd == '3'){
int dIn = digitalRead(D_IN);
double aIn = analogRead(A_IN);
int h = dht.readHumidity();
int t = dht.readTemperature();
digitalWrite(ledPower,LOW);
delayMicroseconds(samplingTime);
voMeasured = analogRead(measurePin);
delayMicroseconds(deltaTime);
digitalWrite(ledPower,HIGH);
delayMicroseconds(sleepTime);
calcVoltage = voMeasured * (5.0 / 1024.0);
dustDensity = (0.17 * calcVoltage - 0.1) * 1000;
Serial.print(dustDensity);
Serial.println(" ug/m3");
Serial.print("Digital Input = "); Serial.println(dIn);
Serial.print("Analog Input = "); Serial.println(aIn);
Serial.println();
Serial.print("humidity:");
Serial.println(h);
Serial.print("temperature:");
Serial.println(t);
delay(1000);
if (t >= 25 or h >= 70 or dustDensity >= 100 or aIn <= 50)
for(int i=0; i<(32*3-6); i++){
myStepper.step(stepsPerRevolution);
}
else
for(int i=0; i<(32*3-6); i++){
myStepper.step(-stepsPerRevolution);
}
delay(3000);
}
else if (cmd == '4'){
}
}
}