MaxSonar EZ0 for servo speed control

At the moment you have two servos that move alternately. I assume you want them to keep doing that, rather than both moving together. On that basis, I've come up with the following:

#include <Servo.h>

const int X_SERVO_PIN = 9;
const int Y_SERVO_PIN = 10;
const int MIN_DELAY = 130; // ms
const int MAX_DELAY = 350; // ms
const int MIN_STEP_INTERVAL = 100; // microseconds per step
const int MAX_STEP_INTERVAL = 1000; // microseconds per step

const int SONAR_PIN = A1;
int sonarVal = 0;

Servo xServo;
Servo yServo;

int xPos = 0;
int yPos = 0;

void setup()
{
  xServo.attach(X_SERVO_PIN);
  yServo.attach(Y_SERVO_PIN);
  Serial.begin (9600);
}

void loop ()
{
  sonarVal = 4 * analogRead(SONAR_PIN);
  sonarVal = constrain(sonarVal, 100, 300);    
  Serial.println(sonarVal);

  int stepInterval = map(sonarVal, 300, 100, MAX_STEP_INTERVAL, MIN_STEP_INTERVAL);

  int newXPos = random(0,181);
  moveServo(xServo, xPos, newXPos, stepInterval);
  xPos = newXPos;
  delay(random(MIN_DELAY, MAX_DELAY));

  int newYPos = random(0,181);
  moveServo(yServo, yPos, newYPos, stepInterval);
  yPos = newYPos;
  delay(random(MIN_DELAY, MAX_DELAY));
}

void moveServo(Servo &servo, int from, int to, int stepInterval)
{
  int direction = 1;
  if(to < from)
  {
    direction = -1;
  }
  for(int position = from; position != to; position += direction)
  {
    xServo.write(position);
    delayMicroseconds(stepInterval);
  }
}

I don't know if this does what you want, but it does what I think you want. I haven't tested it, but it does compile. If it is along the right lines, you will probably want to play around with the delays and step intervals to get the sonar distance / speed relationship you want.

The sketch runs fine for a second or two, then stops. The serial monitor is not printing sensor values. Here is a not so clean photo http://www.flickr.com/photos/50454200@N06/8453876230/ of the armature, none of which will be visible when the piece is complete. Thanks again.

I don't see any separate power supplies for the servos. You aren't powering them directly from the Arduino, are you?

Yes. Do I need a regulated 5 or 6v to power the servos? I have a wall wart with min 7v.

I used a 7806 to regulate the voltage. Looks better?http://www.flickr.com/photos/50454200@N06/8454294456/

I have tried to power the servos several times from external supplies but without success, with both a wall wart and 9v battery, Arduino ground connected.

I have tried to power the servos several times from external supplies but without success, with both a wall wart and 9v battery, Arduino ground connected.

A schematic would be useful. A 9V battery would not.

ifugaopapercraft:
Yes. Do I need a regulated 5 or 6v to power the servos? I have a wall wart with min 7v.

Yes, you need an external supply. The Arduino is very marginal for powering a single small servo that is not under load and very unlikely to support two loaded servos. Check the rating for your servo to see what the maximum current draw is but typically people allow about 1 Amp per servo. The little PP3 9V batteries are completely useless for this, they cannot provide anywhere near enough current to power a servo. A pack of four AA cells in series would usually be enough. Most places that sell radio control systems will also stock the little plastic battery holders which are ideal for the job, and electronic hobby suppliers often stock them.

It also should be noted that a servo's max supply voltage is 6 volts.

I bought a 1.5 amp/5v wall wart that seems to resolve power, uploading, and serial monitor problems. This sketch is working minimally, but is a little jerky and subject to quivering but it is close to the goal. The map/constrain values are the result of guesswork.

#include <Servo.h>

#define PIN_SERVO1 9
#define PIN_SERVO2 10
#define MIN_DELAY1 230
#define MAX_DELAY1 550
#define MIN_DELAY2 230
#define MAX_DELAY2 550

int sonarPin = A0;
int sonarVal =0;

Servo xservo;
Servo yservo;


void setup() {
  xservo.attach(9);
  yservo.attach(10);
  Serial.begin (9600);
  delay(50);//50
}

void loop () {
  
    sonarVal = analogRead(sonarPin);
    sonarVal = sonarVal *5;
    sonarVal = constrain(sonarVal, 200, 500);    
    sonarVal = map(sonarVal, 200, 800, 200, 1500);//  1500
   Serial.println(sonarVal);
   delay(50);
   
  if ((sonarVal < 800) && (sonarVal > 300)) 
  {
    long msec1 = random(MIN_DELAY1,MAX_DELAY1);
    int pos1 = random(0,181);
    xservo.write(pos1);
    delay(sonarVal);
    
    long msec2 = random(MIN_DELAY2,MAX_DELAY2);
    int pos2 = random(0,181);
    yservo.write(pos2);
    delay(sonarVal);
  }
  
}

I thought you wanted to slow the servos down? That sketch slams each servo to the next position as fast as it can move.

Yes, I would like to smooth these jerky movements, but given my limited experience, I was pleased to get at least this far (after many, many hours of trial and error - mostly error). Hopefully, this sketch can be tweaked to resolve the tremors?

Would this approach help?

void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}

You didn't fancy trying the approach implemented in reply #20, then?

The previous anemic power supply wouldn't run your sketch beyond some spastic trembling.The 1.5 amp is turning one motor correctly but the other trembles in one position. Neither are responding to sensor input but that could be just a matter of adjusting the values.

Unfortunately, comparing methods is beyond my current programming skill. When over my head, search, cut, paste, and a little tweaking has been successful but I've never worked with servos.

If you disconnect the 'working' servo, does the other one stop mucking about and start working normally?

When I pull the pin on the working motor, both stop. When the bad motor pin is pulled, the good motor runs.

That suggests either a software bug, or a hardware fault. If the identical hardware works OK with a different sketch, that proves the hardware is OK and implies a software bug. Did I get the pin assignments right?

The same motor on the "smooth sketch" run without a hitch. Your pins are correct. Wish I could help debug but, frankly, I haven't a clue. I will try manipulating some values.

Should have mentioned that initially, both motors run well but after several minutes, the twitch infects the bad motor, no longer moving at random. The twitch only happens at one end of the cycle, not both, as though it is caught in one position and trying to free itself.