help using sensor to control both sound and servo

i’ve been looking high and low but can’t find sample code for controlling a servo with a sensor.
at this point i’ve got an ultra sound (ez0) hooked up to an arduino (2009) that plays different sounds at different sensor reading settings. i’d like to add a servo into the mix and get the sensor reading to not only play sound but rotate the servo as well.

we’re using the lady ada wave shield with this project.

any suggestions?
(i’m new to coding but have been working with a friend who’s traveling at present)

here is the code we’ve got going for the ultrasonic reading in tiers:

#include <ctype.h>
#include <FatReader.h>
#include <SdCard.h>
#include <avr/pgmspace.h>
#include “WaveUtil.h”
#include “WaveHC.h”

#define bit9600Delay 84
#define halfBit9600Delay 42

// Sonar global variables:
byte an = 0; // sonar input pin
int numsonarreads = 6;
int numsonartiers = 20;

// Wave global variables:
SdCard card;
FatVolume vol;
FatReader root;
WaveHC wave;

void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.print("\nBeat box!\n");
delayMicroseconds(500);

// Wave setup
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);

if (!card.init()) {
putstring_nl(“Card init. failed!”);
return;
}

// Enable optimize read – some cards may timeout
card.partialBlockRead(true);

if (!vol.init(card)) {
putstring_nl(“No partition!”);
return;
}
if (!root.openRoot(vol)) {
putstring_nl(“Couldn’t open dir”);
return;
}
}

int sonardistance() {
// returns a number from 1 to numsonartiers representing the sonar reading.
static int readings[5];
static int thisreading = 0;
int totalread = 0;
int maxread = 0;
int minread = 999999;
int averageread;
int tiergap;
int i;

readings[thisreading % numsonarreads] = analogRead(an);

if (thisreading < numsonarreads) {
// We don’t have enough readings to average yet.
// Wait 50ms, then take another reading.
thisreading++;
delay(5);
return(sonardistance());
}

// Now we know we have enough data; drop the largest and smallest values of the
// last 5 and return the average.
for (i=0; i<numsonarreads; i++) {
if (readings > maxread) {
_ maxread = readings*;_
_
}_
_ if (readings < minread) {
minread = readings;
}
totalread += readings;
}
totalread = totalread - maxread - minread;
averageread = totalread / (numsonarreads-2);*_

* thisreading = ((thisreading + 1) % numsonarreads) + numsonarreads;*
* tiergap = 170/(numsonartiers-1);*
* for (i=1; i<=numsonartiers; i++) {*
_ if (averageread < 10 + itiergap) {
return(i);
}
}
return(numsonartiers);
}
void playfile(char *name) {
FatReader file;
if (wave.isplaying) {
// a wave is already playing — let it finish.
} else {
Serial.println(“Starting wave.”);
if (!file.open(root, name)) {
putstring(“Couldn’t open file “);
Serial.println(name);
return;
}
if (!wave.create(file)) {_

putstring_nl(“Not a valid WAV”);
_ return;
}
// ok time to play!
wave.play();
while(wave.isplaying) {
sonardistance();
Serial.print(”.”);
delay(50);
wave.stop();
}
}
}
void loop() {
int sdist = 0;
delay(50);
sdist = sonardistance();
Serial.println(sdist);
if (sdist < 5) {
// This is ugly. Replace with itoa!
if (sdist == 1) {
playfile(“1.WAV”);
}
if (sdist == 2) {
playfile(“2.WAV”);
}
if (sdist == 3) {
playfile(“3.WAV”);
}
if (sdist == 4) {
playfile(“4.WAV”);
}
}
}*_

but can't find sample code for controlling a servo with a sensor

You're kidding?

no, i'm new at this, worked with a friend to write this and am just trying to patch in servo control on my own.

i'm still way on the learning curve here. (he's a great coder but i've only been at it a couple of weeks.)

this is the stuff i'm trying to patch in: (from tom igoe)

int servoPin = 2; // Control pin for servo motor int minPulse = 500; // Minimum servo position int maxPulse = 2500; // Maximum servo position int pulse = 0; // Amount to pulse the servo

long lastPulse = 0; // the time in milliseconds of the last pulse int refreshTime = 20; // the time needed in between pulses

int analogValue = 0; // the value returned from the analog sensor int analogPin = 0; // the analog pin that the sensor's on

void setup() { pinMode(servoPin, OUTPUT); // Set servo pin as an output pin pulse = minPulse; // Set the motor position value to the minimum Serial.begin(9600); }

void loop() { analogValue = analogRead(analogPin); // read the analog input Serial.println(analogValue); pulse = map(analogValue,0,1023,minPulse,maxPulse); // convert the analog value // to a range between minPulse // and maxPulse.

// pulse the servo again if rhe refresh time (20 ms) have passed: if (millis() - lastPulse >= refreshTime) { digitalWrite(servoPin, HIGH); // Turn the motor on delayMicroseconds(pulse); // Length of the pulse sets the motor position digitalWrite(servoPin, LOW); // Turn the motor off lastPulse = millis(); // save the time of the last pulse } }

also: i wonder whether the arduino will be able to control both the sound and servo simultaneously.

thanks

Go to the playground and look for MegaServo library. There's no need to drive the servo yourself - that's what the hardware's for.

Untested:

#define  servoPin 2
#define analogPin 0
MegaServo servo;

void setup() {
  servo.attach (servoPin);
}

void loop() {
servo.write (map(analogRead (analogPin),0,1023,0, 180));
}

yes, i see your point now: perhaps i misspoke.

i'm trying to integrate sensor control for both sound and servo simultaneously.

thanks.