Hi all,
I’m using an accelerometer mma7361 reading an angle of rotation and using this angle to make my stepper motor rotate a set number of rotations.
Here is my code:
#include <Stepper.h>
int analog_x;
float vol_x;
float add_x;
float g_x;
float degree_x;
int switchPin1 = 2;
int switchPin2 = 4;
int switchPin3 = 7;
const int stepsPerRevolution = 20; // 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);
int rotate(int num_rot);
void setup()
{
Serial.begin(9600);
pinMode(switchPin1, INPUT);
pinMode(switchPin2, INPUT);
pinMode(switchPin3, INPUT);
myStepper.setSpeed(1000);
}
void loop()
{
if(digitalRead(switchPin1)==0){
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.0/PI;//calculate the degree value
if (digitalRead(switchPin1)==1) {
degree_x=0;
}
}
//fix the overflow condition
if(g_x>1)
degree_x=90;
if(g_x<-1)
degree_x=-90;
if(digitalRead(switchPin1)==1 && digitalRead(switchPin2)==0 && digitalRead(switchPin3)==0 && 0 <= degree_x < 5)
{
rotate(3);
}
if(digitalRead(switchPin1)==1 && digitalRead(switchPin2)==0 && digitalRead(switchPin3)==0 && 25 <= degree_x < 30)
{
rotate(5);
}
//#########################
//print
Serial.print("Read switch input1: ");
Serial.print(digitalRead(switchPin1));
Serial.print("\t");
Serial.print("\t");
Serial.print("Read switch input2: ");
Serial.print(digitalRead(switchPin2));
Serial.print("\t");
Serial.print("\t");
Serial.print("Read switch input3: ");
Serial.print(digitalRead(switchPin3));
Serial.print("\t");
Serial.print("\t");
Serial.print("x1:");
Serial.print(degree_x);
delay(200);
}
//////////////////////////////////////////////
int rotate(int num_rot)
{
for(int i = 0 ; i < 10 ; i++)
{
for(int i = 0 ; i < num_rot ; i++)
{
myStepper.step(20);
}
}
return 1;
}
now I’m certain that:
- I can read an angle and save it in a variable (degree_x)
- the function called int rotate(num_rot) works. if input is 5 the motor rotates 5 or maybe 6 but this is not a problem.
3)switch buttons work prefectly - motor and mma7361 connections are correct(checked each individually)
5)I’ve checked certain parts of the code alone and they seem to work properly
now the problem is the following:
in my general if statement condition
if(digitalRead(switchPin1)==1 && digitalRead(switchPin2)==0 && digitalRead(switchPin3)==0 && ?? <= degree_x < ??)
the degree_x is the problem!
first if i have the same combination of inputs for the switch buttons the angle (degree_x)doesn’t make a difference, in other words in my previous provided code the motor first rotates 3 times then 5 times regardless of degree_x. The moment I press the button the motor starts rotating, completes the rotations then stops!
I have even tried this with no switch buttons but the same happens! the motor rotates every time i call the function rotate, regardless if the angle degree_x in the condition is met or not!
second to that the moment the motor starts rotating the value of degree_x changes unbelievably, for example if it was
5± .5 once the motor rotates it becomes 40± .5 and does not go down at all until I re-upload the sketch!
does anybody have an explanation or better a solution!
pls I need help!