Coding Problem btw Servo and accelerometer

I qm trying to build a code to control a memsic 2125 and Hs 325 HB servo through ardunio on a breadboard. I tried working on the current code but the problem being only one pulse could be transfered, not mulitple. I would like to get some help regarding the same.

I am not able to insert the image on the forums. Someone please tell me how to load the image so that u can have a look at my schematic and help me if I am going wrong somewhere.

any help regarding the coding would be immensely helpful. :-[

Once you've posted the first time, you should be able to post links. I don't know whether the restriction on posting links applies to posting images, too.

If you are getting one input from the accelerometer, you should be able to get more. Post your code, so we can help.

#define NUMREADINGS 10

int readings[NUMREADINGS];
int index = 0;
int total = 0;
int average = 0;
const int xPin = 2; // X output of the accelerometer
const int yPin = 3; // Y output of the accelerometer
int servoPin = 4;
int minPulse = 0;
int maxPulse = 180;
int pulse = 0;

long lastPulse = 0;
int refreshTime = 20;

void setup() {
for (int i = 0; i < NUMREADINGS; i++)
readings *= 0; *

  • pinMode(accelPin, INPUT); *
  • pinMode(servoPin, OUTPUT); *
    *pulse = minPulse; *
    }
    void loop() {
    *total -= readings[index]; *
  • readings[index] = digitalRead(accelPin);*
  • total += readings[index]; *
  • index = (index + 1); *
  • if (index >= NUMREADINGS) *
  • index = 0; *
  • average = total / NUMREADINGS; *
  • int average = pulseIn(accelPin, HIGH); *
    accelerometer input
    *pulse = map(average, 90, 110, minPulse, maxPulse); *
    if (millis() - lastPulse >= refreshTime) {
  • digitalWrite(servoPin, HIGH); *
  • delayMicroseconds(pulse); *
    motor position
  • digitalWrite(servoPin, LOW); *
    *(); *
    }
    }

Thanks Paul for help me out with this. could u send me a test mail to rrrr2222in@yahoo.com so that i can attach my schmatic and send it back to you. it would great if u can look at the schematic adn let me know if something is wrong with that.

thanks

Your servo code needs work. You are not resetting lastPulse and your servo pulses are not within a valid range (if pulsing in microseconds, you want something around 1000 to 2000). It may be easier to get this going using the arduino servo library.

Also I suggest you add some serial.print statements for debugging so you can see the values you get from the accelerometer and what you send to the servo to make make sure they are as expected.

In loop, you do a digitalRead on the accelerometer pin. That will return a value of LOW (0) or HIGH (1). You store the value in an array, and add it to total.

Then, you compute the average.

Then, you declare a local variable called average, and call pulseIn on the digital pin. The value in average will then be 0 or 1.

Next, you map this value. I'm not sure what you get back from this function, but I suspect it will be a negative number.

Then, you set the servo pin HIGH, wait a probably negative amount of time, and then turn the pin LOW again.

What is actually attached to the servo pin? Why is the time for the servo pin to be HIGH based on the output of an accelerometer that seems to be connected to a digital pin instead of an analog pin?

I have tried attaching the servo library and change the values of the pulses it still doesn't help.

I am using an ATMEGA 168 btw servo and accelerometer and programming the board ona duemilanue. I used the accelerometer pin to digital because I think the accelerometer send pulses so only digital input would accept this, as when i tried on analog input it doesn yield any result.

is ther any sample code anyone has regarding the same. i would like to have a look adn make the changes according to my program.

thank you

I have tried attaching the servo library and change the values of the pulses it still doesn't help.

I am using an ATMEGA 168 btw servo and accelerometer and programming the board ona duemilanue. I used the accelerometer pin to digital because I think the accelerometer send pulses so only digital input would accept this, as when i tried on analog input it doesn yield any result.

is ther any sample code anyone has regarding the same. i would like to have a look adn make the changes according to my program.

thank you

as suggested previously, try adding some serial.print statements for debugging so you can see the values you get from the accelerometer and what you send to the servo to make make sure they are as expected.