Hello, I'm going to use Arduino and Stepper motors to calculate the temperature and the amount of light and make a curtain that goes up and down automatically.
I'm going to use two buttons to make it.
For one button, I want to distinguish between automatic and manual mode, and for the other, I want to implement the function of raising and lowering the curtain in manual mode.
These are the conditions
Bright light comes in, and when it's above 25 degrees, the curtain goes down
Bright light comes in and when it's below 25 degrees, the curtain goes up
It's dark and when it's over 25 degrees, the curtains go up
When it's dark and below 25 degrees, the curtain goes down
I want to implement these conditions
These codes have been written, but the function to distinguish between modes and the buttons to raise and lower the curtain in manual mode will work, but the motor will continue to rotate in one direction!
Can I get some help?
#define pushButton 2
#define wayButton 3
#include <Stepper.h>
#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT11
bool globalFlag = false;
bool toggleStatus = false;
bool globalFlag2 = false;
bool toggleStatus2 = false;
const int stepsPerRevolution = 2048;
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);
DHT dht(DHTPIN, DHTTYPE);
void detectwayButton() {
int readPin = digitalRead(wayButton);
if (readPin == LOW) {
if (globalFlag2 == false)
globalFlag2 = true;
}
else {
if (globalFlag2 == true) {
if (toggleStatus2 == true)
toggleStatus2 = false;
else
toggleStatus2 = true;
globalFlag2 = false;
}
}
}
void detectButton() {
int readPin = digitalRead(pushButton);
if (readPin == LOW) {
if (globalFlag == false)
globalFlag = true;
}
else {
if (globalFlag == true) {
if (toggleStatus == true)
toggleStatus = false;
else
toggleStatus = true;
globalFlag = false;
}
}
}
void setup()
{
Serial.begin(9600);
pinMode(pushButton, INPUT_PULLUP);
pinMode(wayButton, INPUT_PULLUP);
myStepper.setSpeed(14);
dht.begin();
Serial.println("DHT censor is on");
myStepper.setSpeed(14);
}
void loop()
{
detectButton();
Serial.println(toggleStatus);
Serial.println(toggleStatus2);
delay(2000);
//float h = dht.readHumidity();
float t = dht.readTemperature();
int lightvalue = analogRead(A0);
int togglesave1 = 0;
int togglesave2 = 0;
if (toggleStatus== 0) {
int lightValue = analogRead(A0);
Serial.println("automode");
Serial.println(lightValue);
Serial.println(t);
delay(200);
int blindcondition = 0;
int lightcalc = 0;
int tempcalc = 0;
if (lightValue <= 599) {
lightcalc = -1;
}
else if (lightValue > 600) {
lightcalc = 1;
}
if (t <= 24) {
tempcalc = -1;
}
else if (t > 24) {
tempcalc = 1;
}
blindcondition = lightcalc * tempcalc;
switch (blindcondition)
{
case -1:
for (int x = 1; x < 2; x++) {
myStepper.step(-stepsPerRevolution);
Serial.println("down");
}
break;
case 1:
for (int x = 1; x < 2; x++) {
myStepper.step(stepsPerRevolution);
Serial.println("up");
}
break;
}
}
else if (toggleStatus == 1) {
Serial.println("manual mode");
detectwayButton();
if ( toggleStatus2 == 0) {
for (int x = 1; x < 2; x++) {
Serial.println("up");
myStepper.step(stepsPerRevolution);
}
}
else if (toggleStatus2 == 1) {
for (int x = 1; x < 2; x++) {
Serial.println("down");
myStepper.step(-stepsPerRevolution);
}
}
}
}