Pages: [1]   Go Down
Author Topic: Accelerometer changes reading alone!  (Read 327 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,

I have an triple axis accelerometer mma7361 and Im using it to find a rotaton about the x-axis and then use the angle of rotations in an if statement to make my stepper motor rotate. In other words if the analog reading is bigger than a value rotate the motor a set number of rotations and steps if its bigger than another value do another thing and so on.

Code:
int switchPin1 = 2;               ///button that gives either 1 or 0



void loop()
{
   if(digitalRead(switchPin1)==1 && analogRead(0) > 5 )
{
stepper.step(20);
}

   if(digitalRead(switchPin1)==1 && analogRead(0) > 10 )
{
stepper.step(-20);
}
}

the accelerometer is connected to analog pin 0, supplied with 3v from arduino
the motor is unipolar and has 4 wires connected to PWM 8,9,10,11 and supplied with external voltage source

now my problem is the following:
if i place my sensor horizontally and it reads 0+- once i press the button(digital) and the motor rotates, I find out that the reading of the sensor is shifted from 0 to 10 degrees automatically alone without even moving the sensor from the position it was on before the motor starts!

if it was for example 10 before the motor rotates once the motor stops rotating the angle shifts to another bigger value. if i try placing the sensor after that at the zero position it reads 10 instead of zero at the zero position!!! 10 becomes it new zero!!!

(given that the sensor is always stationary and stable during the motor rotations, I only move the sensor after the motor stops rotating)

does anybody really have an explanation to why is this happening?

I suspect that the motor creates electrical noise when it rotates and thus this affects the analog reading of the sensor, since analog is susceptible to noise the value changes! this is my only explanation but I need a proof therefore i need your help!

thanks!
Logged

Oregon, USA
Offline Offline
Edison Member
*
Karma: 65
Posts: 2123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I can't make much sense out of your description of the problem or your code. You should make sure that you understand how the accelerometer works, using the simplest possible program, before moving on to more complicated tasks.

For example, what do you think the value "5" means in the following line? Hint: it is not degrees rotation.
Code:
if(digitalRead(switchPin1)==1 && analogRead(0) > 5 )
Logged

"It seems to run on some form of electricity"

Offline Offline
Newbie
*
Karma: 1
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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??
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

try  to put a "restPosition" value  in the setup function and then in the loop put, current position minus the restPosition, this will give your absolute position. also try to put a 10k resistor between the sensor pin and the sensor to minimize your variation reading. Also what your serial print says?
Logged

Narvik, Norway
Offline Offline
God Member
*****
Karma: 22
Posts: 962
electronics engineer, teacher.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

.. this cannot work very good. ?
The accelerometer gives values based om movement in three directions. Turning(spin) around its own axis will give you non meaningful readings.
As a tilt sensor it must be stable (and still) when read
« Last Edit: June 14, 2014, 05:22:50 pm by knut_ny » Logged

Ny

Pages: [1]   Go Up
Jump to: