Hello,I'm having an issue with my program,I'm using a DHT11 sensor to measure temperature and humidity but I want the stepper motor to run full rotation(revolution) when the temperature is at a certain level but for 20 seconds and stop,until the temperature becomes normal to go back in the other direction....the code is working but having issue on the delays for the motors to keeep running and stop.Here is the code
/*
*Mod by: cerubala christian wanny
* wanny@mediabox.bi
* +25776545762
Stepper Motor 28BYJ-48
Shaft of the stepper motor rotates one full revolution clockwise, then CCW.
Connections (ULN2003 - ARDUINO):
* IN1 --> 11
* IN2 --> 10
* IN3 --> 9
* IN4 --> 8
* VCC --> 5V
* GND --> GND
*/
#include <Arduino.h>
#include <Stepper.h>
#include "DHT.h"
#define DHTTYPE DHT11
#define DHTPIN 4
DHT dht(DHTPIN, DHTTYPE);
int pos = 1;
int rotationCW = 0;
int rotationCCW = 0;
float Temperature, Humidity; //variables to read temperature and humidity
const int stepsPerRevolution = 2048; // 32 teeth on cogged wheel * 64 gear ratio = 2048
// initialize the stepper library on pins 8 through 11 (mind the order!)
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);
void setup()
{
pinMode(DHTPIN, INPUT);
dht.begin();
Serial.begin(9600);
// set the speed; minimum delay between steps is around 2ms
myStepper.setSpeed(15);// goes up to 18 for 28BYJ-48 + ULN2003
}
void loop() {
getvalues ();
steppr();
}
void getvalues (){
//void to read data from dht11
Humidity = dht.readHumidity();
Temperature = dht.readTemperature();
if (isnan(Temperature) || isnan(Humidity)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
Serial.print(F("Humidity: "));
Serial.println(Humidity);
Serial.print(F("Temperature: "));
Serial.println(Temperature);
delay(500);
}
void steppr(){
//void to rotate the motor
if (Temperature <= 34 && pos == 1){
// step one revolution in one direction:
rotationCW = (stepsPerRevolution);
Serial.println("clockwise");
myStepper.step(rotationCW);
rotationCCW++;
delay(5000);
pos = (2);
Serial.println("at location2");
}
else if (Temperature > 34 && pos == 2) {
// step one revolution in the other direction:
rotationCCW = (-stepsPerRevolution);
Serial.println("counterclockwise");
myStepper.step(rotationCCW);
rotationCCW++;
delay(1000);
pos = (1);
Serial.println("at location 1");
}
}