# Stepper Motor not stopping at set degree

I'm trying to install some motors on a telescope so I can put a degree and it moves to said degree (relative to its starting point). I can get it to move back and forth a set amount using one code(doubleStepper code), but when I input a degree into another code (Telescope code)it just never stops rotating.

It's probably something wrong with my loop, but I can't figure out what, any advice?

Non working code:

``````int Distance1 = 0;
int Distance2 = 0;
int DistanceDeg = 0;
int DistanceHours= 0;
int x;
int y;

void setup() {
// put your setup code here, to run once:
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
x = DistanceDeg *(6400/360);

pinMode(12,OUTPUT);
pinMode(11,OUTPUT);
digitalWrite(12,LOW);
digitalWrite(11,LOW);
y = DistanceHours *(1/.06666666)*(6400/360);
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(9,HIGH);
digitalWrite(11,HIGH);
delayMicroseconds(200);
digitalWrite(9,LOW);
digitalWrite(11,LOW);
delayMicroseconds(200);
Distance1 = Distance1 + 1;
Distance2 = Distance2 + 1;

if (Distance1 == x)
{
{
digitalWrite(9,LOW);
}

Distance1 = 0;
delay(300);
}

if (Distance2 == y)
{
{
digitalWrite(11,LOW);
}

Distance2 = 0;
delay(300);

}

}
``````

Working "back and forth" code

``````int Distance1 = 0;
int Distance2 = 0;
int DistanceDeg = 720;
int DistanceHours= 4.5;
int x;
int y;

void setup() {
// put your setup code here, to run once:
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
x = DistanceDeg *(6400/360);

pinMode(12,OUTPUT);
pinMode(11,OUTPUT);
digitalWrite(12,LOW);
digitalWrite(11,LOW);
y = DistanceHours *(1/.06666666)*(6400/360);
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(9,HIGH);
digitalWrite(11,HIGH);
delayMicroseconds(100);
digitalWrite(9,LOW);
digitalWrite(11,LOW);
delayMicroseconds(100);
Distance1 = Distance1 + 1;
Distance2 = Distance2 + 1;

if (Distance1 == x)
{
{
digitalWrite(8,HIGH);
}
else
{
digitalWrite(8,LOW);
}
Distance1 = 0;

delay(500);

}

if (Distance2 == y)
{
{
digitalWrite(12,HIGH);
}
else
{
digitalWrite(12,LOW);
}
Distance2 = 0;

delay(500);

}

}
``````

Telescope_Code.ino (979 Bytes)

doubleStepperTest.ino (1.03 KB)

First, I see you doing arithmetic with decimal values, but the receiving field is an integer. So you will usually get zero as an answer.

For example:

y = DistanceHours (1/.06666666)(6400/360);

DistanceHours is set to zero, and 1/.066666 will always be less than 1 and can never go into an integer, so multiplying by 6400/360 will still be zero.

You need to separate out each of those operation and look at the result by using serial.Print() the answer, so you can begin to debug your program.

Integers have no decimal points!!!! If you need them, use "float".

Paul

1/.066666 will always be less than 1

Your calculator needs new batteries. 1/.066666 is 15.

What, EXACTLY, is connected to the anonymous pins? You are toggling pins 9 and 11 on every pass through loop(), and then reading the state of pin 9 (which you set to LOW). Makes no sense to me.