Issue with arduino 28byj-84 stepper motor

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");
  }
}

Have you considered a limit switch or position sensor?

NO I dint't actually I want if the temperature is > than (for eg) 30 degrees the motor to run for 20 secons and stop and when the temperature goes down ,I want th motor to rotate for the same amount of time but in the other direction

It's like opening and closing a curtain

Your problem/project does not seem to involve LEDs or multiplexers. I am curious why you chose this forum section and I can move it to a more appropriate section if that would help.

If you lose power how do you know where you are? I would suspect it is for a brood house, green house or some other are that needs control over humidity and temperature.

thank you very much for your help

Thank me after I do something helpful!

Actually I'm designing a small prototype for a green how ,regaring the power cut off,I'll something I'll try to figurout but I wanted first to hav a working mechanism wich goes with my logic

Once you know your position you can go to any other with time if there is no slippage. If you do not know where you do a home maneuver to the sensor then time back to the original position.

I totaly understand,but the serious issue that I'm facing is the time of the full resolution ,the logic is good but then activating th motor it's only turning for like 5 seconds,I would like it to do like 20 seconds

Your problem appears to me that the delays are locking your code. The way the delay() works is it does nothing until it counts a given amount of ticks in a closed loop. You should look at blink without delay for a example sketch on how to delay without locking the processor up.

thank you..let me try it out

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.