I've been struggling with code. Project works, but there are two issues I would like to sort out. First one is that whenever I read temp and humid from DHT22 (once in 3s) it interrupts stepper if it is in motion. If I disable reading of temp and humid, then lcd interrups stepper but it isn't noticeable which is ok for me.
Second issue is that if DHT22 reported temp is within range of 22.70-23.30 celsius (setTemp 23 +-0.3) and I press switch "opened" stepper starts moving to one direction and if press switch "closed" it moves to other direction, but it shouldn't move while in that state no matter which button is pressed.
This project is for regulating room temperature with window positioning (how wide it is opened or closed). Buttons go HIGH when pressed. If switch "closed" isn't pressed and reported temp is 0.3 celsius lower than setTemp, window starts closing. If switch "opened" isn't pressed and reported temp is 0.3 celsius higher than setTemp, window starts opening.
I'm really beginner at coding even tho I started coding atmel chips back in mid 2000, still a n00b
I implemented part of a code for controlling stepper with millis() from here A simple sketch to drive Stepper Motor 28BYJ-48 - Exhibition / Gallery - Arduino Forum
My code:
#include <Stepper.h>
#include "DHT.h"
#include "LiquidCrystal.h"
#define DHTPIN 3
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(1, 2, 4, 5, 6, 7);
const int Opened = 12;
const int Closed = 13;
const int stepsPerRevolution = 2048;
unsigned long goTime;
unsigned long goTime2;
int OpenedState = 0 ;
int ClosedState = 0 ;
int interval = 96; // initial delay for stepper motor rotation
int nextTime = 3000;
float Hotter;
float Colder;
float SetTemp = 23.00;
float t;
float h;
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);
void setup() {
dht.begin();
lcd.begin(16, 2);
pinMode(Opened, INPUT_PULLUP);
pinMode(Closed, INPUT_PULLUP);
goTime = millis();
goTime2 = millis();
myStepper.setSpeed( 0xEFFFFFFFL ); // Just to set the step_delay value to 0, to bypass the delay routine in the Stepper class.
}
/////////////////////////////////////////////////////
void CheckTemperature() {
// Read temperature as Celsius (the default)
t = dht.readTemperature();
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
h = dht.readHumidity();
lcd.setCursor(0, 0);
lcd.print("T: ");
lcd.print(t);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("H: ");
lcd.print(h);
lcd.print(" %");
lcd.setCursor(10, 1);
lcd.print(goTime);
Hotter = t + 0.30;
Colder = t - 0.30;
goTime2 += nextTime;
}
/////////////////////////////////////////////
void loop() {
OpenedState = digitalRead(Opened) ;
ClosedState = digitalRead(Closed) ;
int stepsPerRevolution = 0;
if (millis() >= goTime2)
CheckTemperature();
if (OpenedState == LOW && Hotter >= SetTemp)
stepsPerRevolution--;
if (ClosedState == LOW && Colder <= SetTemp)
stepsPerRevolution++;
if ( stepsPerRevolution ) {
myStepper.step(stepsPerRevolution);
interval /= 1.5;
if(interval < 2)
interval = 2;
delay(interval);
goTime = millis();
}
else {
interval = 96;
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
}
}
Breadboard: