Hello! I am working on a project where audio levels activate the servo motor. I am using the sparkfun MEMS mic breakoutboard (SparkFun Analog MEMS Microphone Breakout - ICS-40180 - BOB-18011 - SparkFun Electronics) and a servo similar to the Hitec HS-55. When reading the data without the servo motor I get the range I need but when the servo is wired in the data just continually rises and then maxes out. I am very lost at what the problem is. If anybody could help me that would be great.
#include <Servo.h>
Servo myservo;
int pins = 0; // pin counter
int pos = 1;
const int middleValue = 512; //the middle of the range of analog values
const int numberOfSamples = 128; //how many readings will be taken each time
int sample; //the value read from microphone each time
long signal; //the reading once you have removed DC offset
long averageReading; //the average of that loop of readings
long runningAverage=0; //the running average of calculated values
const int averagedOver= 22; //how quickly new values affect running average
//bigger numbers mean slower
const int threshold=512; //at what level the light turns on
void setup() {
myservo.attach(8);
˜
Serial.begin(9600);
}
void loop() {
// Thank you Elio for the help with audio sampling code below
long sumOfSquares = 0;
for (int i=0; i<32; i++) { //take many readings and average them
sample = analogRead(0); //take a reading
signal = (sample - middleValue); //work out its offset from the center
signal *= signal; //square it to make all values positive
sumOfSquares += signal; //add to the total
}
averageReading = sumOfSquares/numberOfSamples; //calculate running average
runningAverage=(((averagedOver-1)*runningAverage)+averageReading)/averagedOver;
int range;
range = map(runningAverage, 0, 6500, 0, 45);
Serial.print("Running: \t");
Serial.println(runningAverage);
Serial.print("Servo: \t");
Serial.println(range);
delay(1000);
for(pos = 0; pos < range; pos = pos + 10) // 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 = range; pos>=1; pos-=10) // 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
}
}
ex of data
Running: 69
Servo: 0
Running: 65
Servo: 0
Running: 62
Servo: 0
Running: 59
Servo: 0
Running: 56
Servo: 0
Running: 53
Servo: 0
Running: 50
Servo: 0
Running: 48
Servo: 0
Running: 2647
Servo: 18
Running: 5394
Servo: 37
Running: 8102
Servo: 56
Running: 10120
Servo: 70
Running: 12314
Servo: 85
Running: 13860
Servo: 95
Running: 13794
Servo: 95
Running: 14555
Servo: 100
Running: 14515
Servo: 100
Running: 16124
Servo: 111
Running: 18010
Servo: 124
Running: 18762
Servo: 129
Would appreaciate any help! thanks, Levi