Controlling Servo motor using ADXL335

Hi all,

I’m trying to control a servo motor (MG995) using (ADXL335) Accelerometer. They are both connected to the same board which is arduino UNO board. I’m using y and z axes only. I have a problem in my code. I’m trying to control the servo in the reverse direction of the accelerometer (right and left direction using y axis). z-axes is used for more accurate measurement. A buzzer is connected to pin (13) and (11) to operate after a certain range of y and z axis. The problem is after uploading the program, when moving the accelerometer to the right direction, the servo doesn’t move. For the reverse direction, the servo moves with vibrations not smoothly.

The code that I’ve been used is as follows:


#include <Servo.h>
int y=0;
int z=0;

Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(11, OUTPUT);
myservo.attach(9);
pinMode(13, OUTPUT);

}

void loop() {
// put your main code here, to run repeatedly:
y= analogRead(A0);
z= analogRead(A1);

if(y<278 && z<380 )
{

for(pos = 180; pos>=1; pos-=10)
{
myservo.write(pos);
delay(15);
}
}

if(y>367 && z<388 )
{

for(pos = 0; pos<180; pos+=10)
{
myservo.write(pos);
delay(15);
}
}

if((z>260 && z<280) && ( y>325 && y<355))
digitalWrite(13, HIGH);
if((z>400 && z<410) && ( y>320 && y<330))
{
digitalWrite(13, LOW);

}

delay(1500);

}


Please give me your ideas about it. I need your help.

Thanks in advance.

Please give me your ideas about it.

My idea is that you should post your code correctly. Go back and read the sticky at the top of the forum.

I have another idea, too.

  if((z>260 && z<280) && ( y>325 && y<355))
  digitalWrite(13, HIGH);
  if((z>400 && z<410) && ( y>320 && y<330))
 {
  digitalWrite(13, LOW);

}

Consistent use of { and } should prevail. Consistent indenting should prevail. If you can’t get it right as you are writing the code, use Tools + Auto Format before posting code.

I have another idea, too. Print the values read from the accelerometers. They may not be what you think they should be. Serial.begin() in setup() with no Serial.print() statements in loop() or other functions screams clueless. Use some, and get Serial.begin() to shut up and quit giving away what should be your secret.

I have one more idea. You should tell us how the servos are powered. Hopefully, you are not expecting smooth movement if the servos are powered by the Arduino. They must have a separate power supply.

the program that you have been uploaded is not working for me. not even a single response of servo by accelerometer..i need code for my final year project..pls help

i need code for my final year project

You've resurrected a two year+ old thread, and posted nothing of your own.

It's your project, how about showing some effort?

i need code for my final year project..pls help

Which year?

I am connecting 2 servo motor and adxl345 on a robot for making a stable platform. But when robot is running due to vibration the servo motors gets down and doesn't rotate. Without the motor it is working fine. I am using different arduino for robot and servo. Plzz help...

amankumarsingh32: I am connecting 2 servo motor and adxl345 on a robot for making a stable platform. But when robot is running due to vibration the servo motors gets down and doesn't rotate. Without the motor it is working fine. I am using different arduino for robot and servo. Plzz help...

Code? Wiring? Power supply?

All of these are missing from your post.

Plus what servos and what Arduinos?

We're not very good at guessing games.

Steve