Sorry for the confusion its actually not an analogread(0) > 5 in my code. I actually do some calculations to find degree rotations and use it in the if statement.

this is actually the full code:

#include <Stepper.h>

int analog_x;

float vol_x, add_x, g_x, degree_x;

int switchPin1 = 2;

const int stepsPerRevolution = 10; // change this to fit the number of steps per revolution

// for your motor

// initialize the stepper library on pins 8 through 11:

Stepper myStepper(stepsPerRevolution, 8,9,10,11);

void setup()

{

Serial.begin(57600);

pinMode(switchPin1, INPUT);

myStepper.setSpeed(1000);

}

void loop()

{

if(digitalRead(switchPin1)==0 || digitalRead(switchPin1)==1 ){

analog_x=analogRead(0);

}

vol_x=analog_x*5.0/1024;//convert analog_x-->voltage value(v)

pinMode(switchPin1, INPUT);

add_x=vol_x-1.62;//calculate the added x axis voltage value

g_x=add_x/0.8;//calculate the gram value

if(g_x<=1&&g_x>=-1) //We use this condition to prevent the overflow of asin(x).( If x>1 or x<-1, asin(x)=0)

{

degree_x=asin(g_x)*180/PI;//calculate the degree value

}

//fix the overflow condition

if(g_x>1)

degree_x=90;

if(g_x<-1)

degree_x=-90;

//#########################

//######################################################################

while(digitalRead(switchPin1)==1)

{

if(degree_x == 0){

rotate(0,0);

}

////////////////////////////////////////////////

if(degree_x > 15){

rotate(17,10);

}

if(degree_x < 10){

rotate(17,-10);

}

}

////####################################################################

//######################################################################

//print

Serial.print("degree_x:");

Serial.print((degree_x));

Serial.print("\t");

Serial.print("\t");

Serial.print("g_x:");

Serial.println(g_x);

delay(200);

}

/////////////////////////////

int rotate(int num_rot, int steps){

for(int i = 0; i < 25; i++)

{

//rotate 5 times in clockwise direction

for(int i = 0; i < num_rot; i++)

{

myStepper.step(steps);

}

}

return 1;

}

////////////////////////////

now again my problem is as follows:

based on my provided code:

as long as Im pressing the switch sensor: imagine that I place my sensor on a table and it reads zero angle, the motor does not rotate.

now if i rotate the sensor until it gives an angle above 15 degrees(say 20) once the motor rotates, the angle that was once read from the sensor as 20, shift directly to for example 40 once the motor rotates. at this position the sensor should read only 20 but before the motor rotates it was 20 and once it rotates it becomes a higher value!!!

not only this but after the 20 degrees turned into 40, if i try placing the sensor horizontally on a flat surface to give a zero angle reading again, it gives rather 40 degrees on the position that it should give zero on! and if i rotate it in the other direction(counter clockwise) it starts decreasing below 40 until it reaches zero!!!!

do u have any explanation??