heres the code im using. i dont know if its right or not. i pasted it here for you guys to modify. i got it to sweep the angles i need but its extremely twitchy. breath on it and it twitches the servo. its a 2g 2axis accelerometer. i posted the link in my last response. i need to smooth it out and get it to read one axis. right now im wired to X axis only.
#define NUM_SERVO 1
int servoPin[NUM_SERVO] = { 2 }; // Control pins for servo motors
int minPulse = -300; // Minimum servo position
int maxPulse = 5000; // Maximum servo position
int pulseRange = maxPulse - minPulse; // Range of the servo position
int pulseWidth[NUM_SERVO] = { 0 }; // Pulse width for the servo motors
long lastPulse = 20; // Time in milliseconds of the last pulse
int refreshTime = 20; // Time needed in between pulses
int analogPin[NUM_SERVO] = { 0 }; // The analog pin that the sensor's on
int minSensorValue[NUM_SERVO] = { 0 }; // Minimum sensor value
int maxSensorValue[NUM_SERVO] = { 1000 }; // Maximum sensor value
int sensorRange[NUM_SERVO] = { maxSensorValue[0] - minSensorValue[0] }; // Range of the sensor values
int sensorValue[NUM_SERVO] = { 0 }; // The value returned from the analog sensor
int phase = 0; // variable to select the servo motor to drive
//------------------------------------------------------------------------------
// convert the pulse width to a range between minPulse and maxPulse
//------------------------------------------------------------------------------
int checkPulseWidth(int pulseWidth)
{
if (pulseWidth < minPulse) return minPulse;
if (pulseWidth > maxPulse) return maxPulse;
return pulseWidth / 25 * 25;
}
//------------------------------------------------------------------------------
// setup function
//------------------------------------------------------------------------------
void setup()
{
for (int i = 0; i < NUM_SERVO; i++) {
// Set servo pin as an output pin
pinMode(servoPin*, OUTPUT);*
- // Set the motor position value to the minimum*
_ pulseWidth = minPulse;_
* }*
* Serial.begin(9600); // Start serial communication*
}
//------------------------------------------------------------------------------
// loop function
//------------------------------------------------------------------------------
void loop()
{
* for (int i = 0; i < NUM_SERVO; i++) {
sensorValue _= analogRead(analogPin); // read the analog input*
pulseWidth = (int)((float)(sensorValue - minSensorValue*) /*
sensorRange * pulseRange) + minPulse;
pulseWidth = checkPulseWidth(pulseWidth*); // convert the pulse width*
* }*_
* // pulse the servo again if the refresh time (20ms) have passed:*
* if (millis() - lastPulse >= refreshTime) {*
* digitalWrite(servoPin[phase], HIGH); // Turn the motor on*
* delayMicroseconds(pulseWidth[phase]); // Length of the pulse sets the motor position*
* digitalWrite(servoPin[phase], LOW); // Turn the motor off*
* lastPulse = millis(); // Save the time of the last pulse*
* // display the data*
* if (phase == 1) {*
* for (int i = 0; i < NUM_SERVO; i++) {
_ Serial.print("SV");
Serial.print(i);
Serial.print(": ");
Serial.print(sensorValue);
Serial.print(" ");
Serial.print("PW");
Serial.print(i);
Serial.print(": ");
Serial.print(pulseWidth);
Serial.print(" ");
}
Serial.println();
}*_
* // update the phase variable*
* phase++;*
* if (phase > NUM_SERVO-1) phase = 0;
_ }
}*_