I am using thumb USB Arduino to move puppet jaw servo. Using the code below but the issue here is that servo moves at every tiny sound change, ..Is there a way to filter the signal that servo moves only when significant characters are said ? or should I deal with it using hardware adding certain filters (capacitors and resistors) before the input pin 1 ?
/*
Make a Servo Move to Sound.
This example code is in the public domain.
2012 by Cenk Özdemir
*/
// for servo stuff we include the servo library
#include <Servo.h>
// creating a servo object
Servo myservo;
// Some Variables we need
int ServoPin = 1;
int SoundInPin = 5;
// the setup routine runs once when you press reset:
void setup() {
// initialize
myservo.attach(ServoPin);
pinMode(SoundInPin, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(SoundInPin);
// We Map it here down to the possible range of servo moovment.
sensorValue = map(sensorValue,0,512,0,180);
// note normaly the 512 is 1023 becouse of analog reading shud go so far, but i changed that to get better readings.
myservo.write(90);
// setting the servo into standard position
int MoveDelayValue = map(sensorValue,0,255,0,sensorValue);
// maping the same reading a little bit more down to calculate the time your servo gets to make the one Move
if (sensorValue > 33) { // to cut off some static readings only if the reading gets higher then 33 it begings to work
delay(1); // a static delay to smooth things out...
// now move the servo to our mapped reading
myservo.write(sensorValue);
}
}
Is there a way to filter the signal that servo moves only when significant characters are said ?
Yes.
You can filter either in software or hardware. In hardware you need an envelope follower before the A/D converter. Envelope detector - Wikipedia
Or you can do the same thing in software by following the values and making note of the biggest one. After a certain time use the biggest sample you have to set the jaw servo value.
thanks, I found the envelope detector circuit . I will try it tonight and see. One thing I found from working on this digispark usb arduino is that many libraries do not function with it. therefore one may not benefit from some very neat servo libraries and stick to basics.
The Digispark is an Attiny85 based microcontroller development board , as the name implies it is a tiny processor. Not much memory, not much in the way of peripherals like timers and UARTs.
In fact I think is is false advertising to call it a Uno. It is nothing like a Uno.
Yes that was the mistake, Many thanks. But since I ran it on digispark USB stump, I need to make sure both input and output are PWM pins. wish me luck.
I advise that you describe what you mean by "not working". What happened? What did you expect to happen? I also advise to never use the words "not working' on this forum without an explanation.
But since I ran it on digispark USB stump, I need to make sure both input and output are PWM pins.
Why is this?
Try reducing the threshold values you have. A value of 500 implies you have a 5V peak to peak audio signal, and that is not very normal. You need it at least 1.4V peak to peak to overcome the diode's forward voltage.
Why are you using the serial port? The ATtiny does not have any UART and so can not print anything.
The documentation says:-
he Digispark supports all features found in the IDE with the exception of the serial monitor and the burn bootloader functionality.
Not working meant servo not moving although applying audio to analog input.
I removed the serialPrint, yes was meant to show whats happening, but then as GM pointed attiny cannot do it...
I tried removing the 2 threshold limits the 500 , and removed the if statement, all of it..then I named a variable called mover at the beginning of code, and used mapping code, but still servo not responding to audio.
mover = map(audioVal ,0 , 1023, 20, 150);//mapping audio value into servo limits
servo1.write (mover);