Urgent help(stepmotor and DHT11)

I am trying to solve a problem with this code about the stepmotor. What I want it to do is that depending on the temperature it turns to one side or the other (simulating a blind). But this one does it in a loop and doesn't stop.
I leave it below in case someone can help me. ; )

#include <DHT.h>
#include <Stepper.h>
#include <LiquidCrystal.h>
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

LiquidCrystal lcd(8, 9, 4, 5, 6, 10);

Stepper myStepper(40, 30, 32, 34, 36);

int ventiladorPin = 12;

double fnc_ultrasonic_distance(int _t, int _e){
	unsigned long dur=0;
	digitalWrite(_t, LOW);
	delayMicroseconds(5);
	digitalWrite(_t, HIGH);
	delayMicroseconds(10);
	digitalWrite(_t, LOW);
	dur = pulseIn(_e, HIGH, 18000);
	if(dur==0)return 999.0;
	return (dur/57);
}

void setup() {
  pinMode(ventiladorPin, OUTPUT);

  myStepper.setSpeed(15);
  dht.begin();

  lcd.begin(16, 2);
  lcd.print("Temperatura:");
  lcd.setCursor(0, 1);
  lcd.print("Luz: ");

{
  	pinMode(13, OUTPUT);
	pinMode(11, INPUT);
	pinMode(7, OUTPUT);

}

}

void loop() {
  float temperatura = dht.readTemperature();

  if (temperatura < 24) {
    myStepper.step(20);
  } else {
    myStepper.step(-20);
  }

  if (temperatura < 24) {
    digitalWrite(ventiladorPin, HIGH);
  } 
  else {
    digitalWrite(ventiladorPin, LOW);
  }

  {

  	

  lcd.setCursor(12, 0);
  lcd.print(temperatura, 1);
  lcd.setCursor(5, 1);
  
  delay(1000);


}

if ((fnc_ultrasonic_distance(13,11) < 20)) {
  		digitalWrite(7, HIGH);
  		delay(3000);
      digitalWrite(7, LOW);
  	}
  	else {
  		digitalWrite(7, LOW);
  	}

}

Do you want the motor to step 20 a single time when the temperature is one way and then turn 20 the other way once when the temperature gets over the set point (24)? The way your code runs now, the 20 steps happens every single time the loop runs and that is many times per second.

You need a global variable to store the motor position and only change the motor position once per temperature flip. When the motor position and required position don’t match, then command the motor 20 steps.

Would love to give a more detailed answer, but don’t have time. Will check back later though.

Thank you very much for responding, in the meantime I will try to make the variable that you recommended.
If you could look at it today it would be great for me since I need this job for tomorrow and I'm a little lost. But thanks anyway

This ould be right now??¿

#include <DHT.h>
#include <Stepper.h>
#include <LiquidCrystal.h>

#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

LiquidCrystal lcd(8, 9, 4, 5, 6, 10);

Stepper myStepper(40, 30, 32, 34, 36);

int ventiladorPin = 12;

double fnc_ultrasonic_distance(int _t, int _e) {
  unsigned long dur = 0;
  digitalWrite(_t, LOW);
  delayMicroseconds(5);
  digitalWrite(_t, HIGH);
  delayMicroseconds(10);
  digitalWrite(_t, LOW);
  dur = pulseIn(_e, HIGH, 18000);
  if (dur == 0) return 999.0;
  return (dur / 57);
}

int posicionMotorActual = 0; // Variable global para almacenar la posición actual del motor

void setup() {
  pinMode(ventiladorPin, OUTPUT);
  myStepper.setSpeed(15);
  dht.begin();
  lcd.begin(16, 2);
  lcd.print("Temperatura:");
  lcd.setCursor(0, 1);
  lcd.print("Luz: ");
  pinMode(13, OUTPUT);
  pinMode(11, INPUT);
  pinMode(7, OUTPUT);
}

void loop() {
  float temperatura = dht.readTemperature();

  // Comparar la temperatura actual con el umbral (24)
  if (temperatura < 24) {
    if (posicionMotorActual != 20) {
      myStepper.step(20);
      posicionMotorActual = 20;
    }
    digitalWrite(ventiladorPin, HIGH);
  } else {
    if (posicionMotorActual != 0) {
      myStepper.step(-20);
      posicionMotorActual = 0;
    }
    digitalWrite(ventiladorPin, LOW);
  }

  lcd.setCursor(12, 0);
  lcd.print(temperatura, 1);
  lcd.setCursor(5, 1);
  delay(1000);

  // Control de un dispositivo basado en la distancia medida por un sensor ultrasónico
  if (fnc_ultrasonic_distance(13, 11) < 20) {
    digitalWrite(7, HIGH);
    delay(3000);
    digitalWrite(7, LOW);
  } else {
    digitalWrite(7, LOW);
  }
}

this seems to be the same topic as Maqueta de habitación con sensores

Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum. It will help you get the best out of the forum in the future.

Thank you.


➜ seems this was solved in the Spanish speaking section, you could have had the decency to to come here and close this thread....

I'm locking it. don't cross post ever again please.

2 Likes