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");
}
}
}*_