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.