controle dc motor with acceleromtre ADXL335 +arduino

hello evreyone .. i need program arduino to control dc motor with acceleromtre ADXL335 thanks in advance :)

Are you able to read values from the accelerometer and print them to the serial monitor? Can you show us what you do have?

with this code for test const int xPin = 0; const int yPin = 1; const int zPin = 2;

//The minimum and maximum values that came from //the accelerometer while standing still //You very well may need to change these int minVal = 265; int maxVal = 402;

//to hold the caculated values double x; double y; double z;

void setup(){ Serial.begin(9600); }

void loop(){

//read the analog values from the accelerometer int xRead = analogRead(xPin); int yRead = analogRead(yPin); int zRead = analogRead(zPin);

//convert read values to degrees -90 to 90 - Needed for atan2 int xAng = map(xRead, minVal, maxVal, -90, 90); int yAng = map(yRead, minVal, maxVal, -90, 90); int zAng = map(zRead, minVal, maxVal, -90, 90);

//Caculate 360deg values like so: atan2(-yAng, -zAng) //atan2 outputs the value of -? to ? (radians) //We are then converting the radians to degrees x = RAD_TO_DEG * (atan2(-yAng, -zAng) + PI); y = RAD_TO_DEG * (atan2(-xAng, -zAng) + PI); z = RAD_TO_DEG * (atan2(-yAng, -xAng) + PI);

int ledPinX = 5; //whatever PWM digital pin you want int ledPinY = 9; //whatever PWM digital pin you want int ledPinZ = 10; //whatever PWM digital pin you want

pinMode(ledPinX, OUTPUT); pinMode(ledPinY, OUTPUT); pinMode(ledPinZ, OUTPUT);

int LEDx = map(x, 0, 360, 0, 255); int LEDy = map(y, 0, 360, 0, 255); int LEDz = map(z, 0, 360, 0, 255);

analogWrite(ledPinX, LEDx); analogWrite(ledPinY, LEDy); analogWrite(ledPinZ, LEDz);

//Output the caculations Serial.print("x: "); Serial.print(x); Serial.print(" | y: "); Serial.print(y); Serial.print(" | z: "); Serial.println(z);

delay(500);//just here to slow down the serial output - Easier to read }

.. i have this result

x: 357.04 | y: 360.00 | z: 270.00 x: 357.73 | y: 3.18 | z: 324.46 x: 357.06 | y: 358.04 | z: 236.31 x: 357.23 | y: 1.85 | z: 303.69 x: 357.27 | y: 1.36 | z: 296.57 x: 356.22 | y: 359.53 | z: 262.88

These are not needed in the loop, put the pinModes in setup() and the variables above the setup function.

int ledPinX = 5; //whatever PWM digital pin you want int ledPinY = 9; //whatever PWM digital pin you want int ledPinZ = 10; //whatever PWM digital pin you want

pinMode(ledPinX, OUTPUT); pinMode(ledPinY, OUTPUT); pinMode(ledPinZ, OUTPUT);

You have 3 LEDs as outputs, learn how they work and replace them with a motor. (USE A MOTOR DRIVER/SHIELD, DO NOT POWER MOTOR FROM ARDUINO)

I try to do two motor direction is not equivalent with the acceleration :/ can you help me please

What are your maximum and minimum values you get for each axis, and what values are you feeding to the motors?

i will use dc motor 5v … i want just reversing the direction of motor

i have this code but it doesn’t work … i use for it a servo motor …
#include <Servo.h>

// these constants describe the pins. They won’t change:
const int groundpin = 18; // analog input pin 4 – ground
const int powerpin = 19; // analog input pin 5 – voltage
const int xpin = A3; // x-axis of the accelerometer
const int ypin = A2; // y-axis
const int zpin = A1; // z-axis (only on 3-axis models)
// these constants won’t change:
const int xPin = 2; // X output of the accelerometer
const int yPin = 3; // Y output of the accelerometer
int val;
int valAverage = 0;
Servo myservo; // create servo object to control a servo

void setup()
{
// initialize the serial communications:
Serial.begin(9600);

// Provide ground and power by using the analog inputs as normal
// digital pins. This makes it possible to directly connect the
// breakout board to the Arduino. If you use the normal 5V and
// GND pins on the Arduino, you can remove these lines.
pinMode(groundpin, OUTPUT);
pinMode(powerpin, OUTPUT);
digitalWrite(groundpin, LOW);
digitalWrite(powerpin, HIGH);
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
myservo.attach(9); // attaches the servo on pin 9 to the servo object

}

void loop()
{
// print the sensor values:
Serial.print(analogRead(xpin));
// print a tab between values:
Serial.print("\t");
Serial.print(analogRead(ypin));
// print a tab between values:
Serial.print("\t");
Serial.print(analogRead(zpin));
Serial.println();
// delay before next reading:
delay(100);

// variables to read the pulse widths:
int pulseX, pulseY;
// variables to contain the resulting accelerations
int accelerationX, accelerationY;

// read pulse from x- and y-axes:
pulseX = pulseIn(xPin,HIGH);
pulseY = pulseIn(yPin,HIGH);

// convert the pulse width into acceleration
// accelerationX and accelerationY are in milli-g’s:
// earth’s gravity is 1000 milli-g’s, or 1g.
accelerationX = ((pulseX / 10) - 500) * 8;
accelerationY = ((pulseY / 10) - 500) * 8;

// print the acceleration
Serial.print(accelerationX);
// print a tab character:
Serial.print("\t");
Serial.print(accelerationY);
Serial.println();
//simple smoothening function
for(int i = 0; i < 5; i++){
val = map(accelerationY,-1200 , 1200, 0, 179); // scale it to use it with the servo (value between 0 and 180)
//can put a if stament here.
valAverage = valAverage + val;
}
// if value is too high change number you divide by
valAverage = valAverage / 5;
myservo.write(val); // sets the servo position according to the scaled value
// valAverage = 0;
delay(100);
}

const int groundpin = 18; // analog input pin 4 -- ground const int powerpin = 19; // analog input pin 5 -- voltage

NO, do not power the servo from the Arduino!, Especially not from the digital pins, you will burnout your Arduino. You need a proper 5-6V power supply, like a 9V battery pack with a 5V regulator.

pulseX = pulseIn(xPin,HIGH); pulseY = pulseIn(yPin,HIGH);

What accelerometer are you using? Can you provide a link or picture?

i use the accelereomter ADXL335 .. https://www.youtube.com/watch?v=hbcpv6tr21c .. this is a video in youtube like my project

Then you have the same module I have, in which case you should not be using pulseIn() to read the values from the accelerometer. There should be 3 pins labeled x, y and z, on that board. What you need to do is get the values using the analogRead() function on each pin. It should return a range from 200 - 500 or similar.

Once you are able to see those values, you then need to use the map() function, to convert the 200 - 500 range to a usable 0 - 180 range for your servo.

thanks .. :) please can you help me for the code :) or can you give me your example code .. and thanks in advance

This is for skid steering, and I know its not a great example, but it should give you some idea.

/*
 ADXL3xx
 
 Reads an Analog Devices ADXL3xx accelerometer and communicates the
 acceleration to the computer.  The pins used are designed to be easily
 compatible with the breakout boards from Sparkfun, available from:
 http://www.sparkfun.com/commerce/categories.php?c=80
 
 http://www.arduino.cc/en/Tutorial/ADXL3xx
 
 The circuit:
 analog 0: accelerometer self test
 analog 1: z-axis
 analog 2: y-axis
 analog 3: x-axis
 analog 4: ground
 analog 5: vcc
 
 created 2 Jul 2008
 by David A. Mellis
 modified 30 Aug 2011
 by Tom Igoe 
 
 This example code is in the public domain.
 
 */

// these constants describe the pins. They won't change:
const int powerpin = 53;              // analog input pin 5 -- voltage
const int xpin = A15;                  // x-axis of the accelerometer
const int ypin = A14;                  // y-axis
const int zpin = A13;                  // z-axis (only on 3-axis models)
int X,Y,Z;
int DRV1,DRV2,STRR,STRL;
int M1L,M1R,M2L,M2R;

void setup()
{
  // initialize the serial communications:
  Serial.begin(9600);

  // Provide ground and power by using the analog inputs as normal
  // digital pins.  This makes it possible to directly connect the
  // breakout board to the Arduino.  If you use the normal 5V and
  // GND pins on the Arduino, you can remove these lines.

  pinMode(powerpin, OUTPUT);

  digitalWrite(powerpin, HIGH);
}

void loop()
{
  X = constrain(map(analogRead(xpin),406,630,-90,90),-90,90);
  Y = constrain(map(analogRead(ypin),390,612,-90,90),-90,90);
  Z = constrain(map(analogRead(zpin),410,610,-90,90),-90,90);
  // print the sensor values:
  Serial.print(X);
  Serial.print("\t");
  Serial.print(Y);
  Serial.print("\t");
  Serial.println(Z);

  // print a tab between values:
  DRV2 = map(Z, -90, 0, 255, 0);
  DRV1 = map(Z, 0, 90, 0, 255);
  STRL = map(Y, -90, 0, 255, 0);
  STRR = map(Y, 0, 90, 0, 255);

  if((Z > 10 || Z < -10)&& X > 10 || X < -10)
  {
    Z = Z;
  }
  else 
  {
    Z=0;
  }
  if(Y > 10 || Y < -10)
  {
    Y=Y;
  }
  else 
  {
    Y=0;
  }

  if(Z > 0)//forwards               
  {
    M1L = abs(DRV1 - STRL); 
    M1R = abs(DRV1 - STRR);
    M2L = 0; 
    M2R = 0;    
  }
  else if(Z < 0)//backwards               
  {
    M1L = 0; 
    M1R = 0;
    M2L = abs(DRV2 - STRL); 
    M2R = abs(DRV2 - STRR);   
  }
  else if(Z==0 && Y >0)//Right               
  {
    M1L = STRL; 
    M1R = 0;   
    M2L = 0;   
    M2R = STRL;   
  }
  else if(Z == 0 && Y < 0)//Left              
  {
    M1L = 0;     
    M1R = STRR;   
    M2L = STRR; 
    M2R = 0;
  }
  else
  {
    M1L = 0;     
    M1R = 0;   
    M2L = 0; 
    M2R = 0;
  }
  //Serial.print("\t");
  Serial.println(temp);
  Serial.print("M1l: ");
  Serial.print(M1L); 
  Serial.print(" M1R: ");
  Serial.println(M1R);
  Serial.print("M2l: ");
  Serial.print(M2L); 
  Serial.print(" M2R: ");
  Serial.println(M2R);
  Serial.println();
  // delay before next reading:
  delay(100);
}