Controlling range of stepper motion

Hello,

I am trying to use a stepper and a T sensor to control a window opening mechanism.

So far I have got stepper to respond by moving a set number of steps when the temperature is above or below set temperature thresholds using if statements.

The challenge now is to set a limit to the stepper motion so that it will stop turning when the window is fully closed/open... What are my options to do this?

  1. Can I set a home position and then read the motor position relative to this and block movement beyond this point with another if statement?

  2. should I use a limit switch?

  3. can I set the limit the range of motion to one full rotation and then define the steps per rotation to match the range of movement that I need?

Any suggestions would be great...

My (painfully simple!) code is below.. Thanks!

#include <Stepper.h>

int in1Pin = 3;
int in2Pin = 5;
int in3Pin = 6;
int in4Pin = 9;

Stepper motor(200, in1Pin, in2Pin, in3Pin, in4Pin);

int Tpin = A1;

void setup()
{
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(in3Pin, OUTPUT);
pinMode(in4Pin, OUTPUT);

motor.setSpeed(30);

pinMode(Tpin, INPUT);

Serial.begin(9600);
}

void loop()
{

int reading = analogRead(Tpin);

float voltage = reading * 5.0;
voltage /= 1024.0;

float temperatureC = (voltage - 0.5)*100;

Serial.print (temperatureC); Serial.println("degrees C");

if (temperatureC >= 24) {motor.step(20);}

if (temperatureC <= 20) {motor.step(-20);}}

Limit switches would be the best method but involve more wiring. Moving the motor by a fixed number of steps would seem to be a workable solution that involves no more wiring.

You might also want to consider what to do if the blinds don't move. Burning out a stepper motor driver trying to make them move anyway, while the cat is fighting tooth and nail to prevent that, is not a good thing.