The problem with the stepper motor constantly rotating

Hello, I'm going to use Arduino and Stepper motors to calculate the temperature and the amount of light and make a curtain that goes up and down automatically.
I'm going to use two buttons to make it.
For one button, I want to distinguish between automatic and manual mode, and for the other, I want to implement the function of raising and lowering the curtain in manual mode.

These are the conditions

Bright light comes in, and when it's above 25 degrees, the curtain goes down
Bright light comes in and when it's below 25 degrees, the curtain goes up
It's dark and when it's over 25 degrees, the curtains go up
When it's dark and below 25 degrees, the curtain goes down
I want to implement these conditions

These codes have been written, but the function to distinguish between modes and the buttons to raise and lower the curtain in manual mode will work, but the motor will continue to rotate in one direction!
Can I get some help?

#define pushButton 2
#define wayButton 3
#include <Stepper.h>
#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT11


bool globalFlag = false;
bool toggleStatus = false;

bool globalFlag2 = false;
bool toggleStatus2 = false;

const int stepsPerRevolution = 2048;
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);
DHT dht(DHTPIN, DHTTYPE);


void detectwayButton() {
  int readPin = digitalRead(wayButton);
  if (readPin == LOW) {
    if (globalFlag2 == false)
      globalFlag2 = true;
  }

  else {
    if (globalFlag2 == true) {
      if (toggleStatus2 == true)
        toggleStatus2 = false;
      else
        toggleStatus2 = true;
      globalFlag2 = false;
    }
  }

}

void detectButton() {
  int readPin = digitalRead(pushButton);
  if (readPin == LOW) {
    if (globalFlag == false)
      globalFlag = true;
  }

  else {
    if (globalFlag == true) {
      if (toggleStatus == true)
        toggleStatus = false;
      else
        toggleStatus = true;
      globalFlag = false;
    }
  }
}

void setup()
{
  Serial.begin(9600);
  pinMode(pushButton, INPUT_PULLUP);
  pinMode(wayButton, INPUT_PULLUP);
  myStepper.setSpeed(14);
  dht.begin();
  Serial.println("DHT censor is on");
  myStepper.setSpeed(14);
}

void loop()
{
  detectButton();
  Serial.println(toggleStatus);
  Serial.println(toggleStatus2);
  delay(2000);



  //float h = dht.readHumidity();
  float t = dht.readTemperature();
  int lightvalue = analogRead(A0);
  int togglesave1 = 0;
  int togglesave2 = 0;


    if (toggleStatus== 0) {
      int lightValue = analogRead(A0);
      Serial.println("automode");
      Serial.println(lightValue);
      Serial.println(t);
      delay(200);

      int blindcondition = 0;
      int lightcalc = 0;
      int tempcalc = 0;

      if (lightValue <= 599) {
        lightcalc = -1;
      }
      else if (lightValue > 600) {
        lightcalc = 1;
      }
      if (t <= 24) {
        tempcalc = -1;
      }
      else if (t > 24) {
        tempcalc = 1;
      }

      blindcondition = lightcalc * tempcalc;

      switch (blindcondition)
      {
        case -1:
          for (int x = 1; x < 2; x++) {
            myStepper.step(-stepsPerRevolution);
            Serial.println("down");
          }
          break;
        case 1:
          for (int x = 1; x < 2; x++) {
            myStepper.step(stepsPerRevolution);
            Serial.println("up");
          }
          break;
      }
    }
    else if (toggleStatus == 1) {

      Serial.println("manual mode");
      detectwayButton();

      if ( toggleStatus2 == 0) {
        for (int x = 1; x < 2; x++) {
          Serial.println("up");
          myStepper.step(stepsPerRevolution);
        }
      }
      else if (toggleStatus2 == 1) {
        for (int x = 1; x < 2; x++) {
          Serial.println("down");
          myStepper.step(-stepsPerRevolution);
        }
      }
    }
}

When you decide that the curtain should go 'up', do you have some way of knowing that the curtain is already as 'up' as it can go? You need some way to know when the curtain is 'up' or 'down'.

For example, this:

Should be:

Bright light comes in, and when it's above 25 degrees, and the curtain is not all the way down, the curtain goes down until the curtain is all the way down, then stops.

I typed it wrong in the code I posted. I was going to test if the stepPerRevolution is working by setting the value to 1024. If it works, I will calculate the length of the curtain and the angle of rotation to set the value.

Also think about days when the power is out, the chain breaks, the drive slips, et c.

You will still have to remember if the curtain is up or down.

The most secure way to know if the curtain is fully "up" or fully "down" is to have a sensor that detects the end-position.

This could be a mirco-switch or a small light-barrier or a reed-relay with magnet or a hall-sensor with magnet.

If you post a picture of the mechanic of your curtain better suggestions can be given.

best regards Stefan

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