Coding problem with controlling stepper motor and humidity sensor

Hi pips,

im trying to get my steeper motor (NEMA 17) to turn 180 degrees in one direction (lets say "open') and stay there when my humidity sensor (DHT11 sensor) reads higher then 64%. If the sensor reads anything below 64% the steeper motor turns 180 degrees in the opposite direction (lets say 'closed') and stays there. The original position of the stepper motor should be 'closed'.

The problem with my code is that when the humidity sensor reads above 64% the stepper motor turns 180 degrees (which is what i want), but keeps turning another 180 degrees, and another, and another...you get the picture.

here is my code:

#include <dht.h>

dht DHT;

#define DHT11_sensor 2

const int stepPin = 3;
const int dirPin = 4;

void setup() {
  // Sets the two pins as Outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  Serial.begin(9600);
}
void loop() {

  int val = DHT.read11(DHT11_sensor); //Read DHT11 sensor temp and humid and store it as a value

  Serial.print("Temp: ");
  Serial.print(DHT.temperature);
  Serial.print((char)223);
  Serial.println("C");
  Serial.print("Humidity: ");
  Serial.print(DHT.humidity);
  Serial.println("%");
  delay(1000);


  if (DHT.humidity > 64) {
    digitalWrite(dirPin, HIGH);
    for (int x = 0; x < 100; x++) {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(2000);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(2000);
    }
  
    delay(1000);
  }
  }

im a novice and i would appreciate it if someone could help me out. Thank you for taking the time to read this post.

Look at the state change detection example. You want to move the stepper when the humidity goes above, or goes below, 64 %, NOT when the humidity IS above, or IS below 64 %.

what paul said,
you have no if condition if it goes below your value, you need a variable to store the current state of the reading ie above or below. And a function to change the direction pin and step the stepper based on the condition of that variable.
ie if above value, drive dir_pin low - step stepper
if below value, drive dir_pin high - step stepper.

if you are just learning you need to read Robyns post on how to do multiple things at once.

https://forum.arduino.cc/index.php?topic=223286.0

The very first thing you should do is learn how not to use delay, because once you learn that it leads on to proper use of functions, not just the trap of using delay. good luck