///////// BILLY MOUTH MOVE ///////////////
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
int calibrationTime = 0;
long unsigned int lowIn;
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
/////////////////////////////////////////////////////////////////////////
int pirPin = 11; ///////////////////// pir sensor pin
/////////////////////////////////////////setup
void setup()
{
myservo.attach(16); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
pinMode(pirPin, INPUT);
// Set the output pins for the DAC control. This pins are defined in the library
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
Serial.println("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(calibrationTime - i);
Serial.print("-");
delay(1000);
}
Serial.println();
Serial.println("done");
while (digitalRead(pirPin) == HIGH) {
delay(500);
Serial.print(".");
}
Serial.print("SENSOR ACTIVE");
delay(1000);
}
//////////////////////////////////////////////// loop
void loop() {
if(digitalRead(pirPin) == HIGH){
for(pos = 0; pos < 90; pos += 1) // goes from 0 degrees to 90 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(3); // waits 4ms for the servo to reach the position
}
for(pos = 90; pos>=1; pos-=1) // goes from 90 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(3); // waits 4ms for the servo to reach the position
}
if(lockLow){
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}
if(digitalRead(pirPin) == LOW){
if(takeLowTime){
lowIn = millis();
takeLowTime = false;
}
if(!lockLow && millis() - lowIn > pause){
lockLow = true;
Serial.print("motion ended at ");
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
}
}
}
could someone tell me how to add a sound file which plays at the same time as the servo movement!
Tell us more about your system. What kind of Arduino are you using. What DAC are you using? Where do you plan to store the Wave files? If you are using an UNO and will be getting files from an SD card, you will first need to move your PIR sensor to a different pin. Pin 11 is needed for SPI communication with the card.
OK. As I mentioned in my previous post, you will need to move the PIR to a different pin. All of the analog pins are free, so one of those would be a good choice. You will also have to use a different servo library, because the standard servo library conflicts with the WaveHC library: http://forums.adafruit.com/viewtopic.ph ... 0&p=198665
Have you tested your wave shield with any of the WaveHC example sketches to make sure it works?
Hi I appreciate the reply and have moved my pir sensor to pin no 8. I have tested the wave shield and servo together in a sketch where the volume moves the mouth to the sound file played unfortunately its not enough movement for the mouth to move. I will post the sketch below
//////////////////////////////////////////////////////////////////// libraries
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include <WaveHC.h>
#include <WaveUtil.h>
///////////////////////////////////////////////////////// constant pins, values
#define swPin 14
SdReader card; // This object holds the information for the card
FatVolume vol; // This holds the information for the partition on the card
FatReader root; // This holds the information for the filesystem on the card
FatReader f; // This holds the information for the file we're play
WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time
// this handy function will return the number of bytes currently free in RAM, great for debugging!
int freeRam(void)
{
extern int __bss_end;
extern int *__brkval;
int free_memory;
if((int)__brkval == 0) {
free_memory = ((int)&free_memory) - ((int)&__bss_end);
}
else {
free_memory = ((int)&free_memory) - ((int)__brkval);
}
return free_memory;
}
void sdErrorCheck(void)
{
if (!card.errorCode()) return;
putstring("\n\rSD I/O error: ");
Serial.print(card.errorCode(), HEX);
putstring(", ");
Serial.println(card.errorData(), HEX);
while(1);
}
///////////////////////////////////////////////////////////////////////////////////////////
int inputPin = 8; // choose the input pin (for PIR sensor)
int servoPin = 16; // choose the input pin (for Servo)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status for motion sensor
int minPulse = 600; // minimum servo position
int maxPulse = 2200; // maximum servo position
int BANNED = 1800; // servo turn rate increment (larger value, faster rate)
int refreshTime = 20; // time (ms) between pulses (50Hz)
int mouthchange = 6; //checks to see if mouth position needs to be changed
/** The Arduino will calculate these values for you **/
int centerServo; // center servo position
int pulseWidth; // servo pulse width
long lastPulse = 0; // recorded time (ms) of the last pulse
/////////////////////////////////////////////////////////////////////////// setup
void setup() {
pinMode(inputPin, INPUT); // declare sensor as input for PIR
// set up servo pin
pinMode(servoPin, OUTPUT); // Set servo pin 18 (analog 4) as an output pin
centerServo = maxPulse - ((maxPulse - minPulse)/2);
pulseWidth = centerServo; // Give the servo a starting point (or it floats)
// set up serial port
Serial.begin(9600);
putstring("Free RAM: "); // This can help with debugging, running out of RAM is bad
Serial.println(freeRam()); // if this is under 150 bytes it may spell trouble!
// Set the output pins for the DAC control. This pins are defined in the library
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
// if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you
if (!card.init()) { //play with 8 MHz spi (default faster!)
putstring_nl("Card init. failed!"); // Something went wrong, lets print out why
sdErrorCheck();
while(1); // then 'halt' - do nothing!
}
// enable optimize read - some cards may timeout. Disable if you're having problems
card.partialBlockRead(true);
// Now we will look for a FAT partition!
uint8_t part;
for (part = 0; part < 5; part++) { // we have up to 5 slots to look in
if (vol.init(card, part))
break; // we found one, lets bail
}
if (part == 5) { // if we ended up not finding one :(
putstring_nl("No valid FAT partition!");
sdErrorCheck(); // Something went wrong, lets print out why
while(1); // then 'halt' - do nothing!
}
// Lets tell the user about what we found
putstring("Using partition ");
Serial.print(part, DEC);
putstring(", type is FAT");
Serial.println(vol.fatType(),DEC); // FAT16 or FAT32?
// Try to open the root directory
if (!root.openRoot(vol)) {
putstring_nl("Can't open root dir!"); // Something went wrong,
while(1); // then 'halt' - do nothing!
}
// Whew! We got past the tough parts.
putstring_nl("Ready!");
}
//////////////////////////////////////////////////////////// loop
void loop() {
val = digitalRead(inputPin); // read input value
if (val == HIGH) // check if the input is LOW
{
if (pirState == LOW)
{
Serial.println("Motion Detected!");
playcomplete("PUPPET~1.WAV");
playcomplete("PUPPET~1.WAV");
pirState = HIGH;
}
}
else
{
if (pirState == HIGH)
{
Serial.println("Motion ended!");
pirState = LOW;
}
}
}
// Plays a full file from beginning to end with no pause.
void playcomplete(char *name) {
char i;
uint8_t volume;
int v2;
playfile(name);
while (wave.isplaying)
{
volume = 0;
v2 = 0;
for (i=0; i<100; i++)
{
volume = analogRead(1);
// Remember this value if it's the biggest so far
if (volume > v2) {
v2 = volume;
}
delay(1);
}
if (v2 > 440)
{
pulseWidth = 600;
mouthchange = 1;
}
else
{
pulseWidth = 600;
mouthchange = 1;
}
digitalWrite(servoPin, HIGH); // start the pulse
delayMicroseconds(pulseWidth); // pulse width
digitalWrite(servoPin, LOW); // stop the pulse
}
}
void playfile(char *name)
{
//////// stop any file already playing//////////////////////////
if (wave.isplaying) {// already playing something, so stop it!
wave.stop(); // stop it
}
// look in the root directory and open the file
if (!f.open(root, name)) {
putstring("Couldn't open file ");
Serial.print(name);
return;
}
// OK read the file and turn it into a wave object
if (!wave.create(f)) {
putstring_nl("Not a valid WAV");
return;
}
// ok time to play! start playback
wave.play();
}
Ha Ha Thanks CircuitBurner its not as annoying as a fish its the scary puppet from film saw called Billy funny enough lol.
Meanwhile I have been trying to use my first sketch but instead of using the servo.h library use timer2 but it just keeps saying "ServoTimer2" does not name a type
//////// BILLY MOUTH MOVE ///////////////
ServoTimer2 myservo;
int servoPin = 16;
int pos = 0; // variable to store the servo position
int calibrationTime = 0;
long unsigned int lowIn;
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
/////////////////////////////////////////////////////////////////////////
int pirPin = 8; ///////////////////// pir sensor pin
/////////////////////////////////////////setup
void setup()
{
Serial.begin(9600);
pinMode(pirPin, INPUT);
pinMode(servoPin, OUTPUT);
myservo.attach(servoPin);
// Set the output pins for the DAC control. This pins are defined in the library
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
Serial.println("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(calibrationTime - i);
Serial.print("-");
delay(1000);
}
Serial.println();
Serial.println("done");
while (digitalRead(pirPin) == HIGH) {
delay(500);
Serial.print(".");
}
Serial.print("SENSOR ACTIVE");
delay(1000);
}
//////////////////////////////////////////////// loop
void loop() {
if(digitalRead(pirPin) == HIGH){
for(pos = 0; pos < 70; pos += 1) // goes from 0 degrees to 90 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(3); // waits 4ms for the servo to reach the position
}
for(pos = 70; pos>=1; pos-=1) // goes from 90 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(3); // waits 4ms for the servo to reach the position
}
if(lockLow){
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}
if(digitalRead(pirPin) == LOW){
if(takeLowTime){
lowIn = millis();
takeLowTime = false;
}
if(!lockLow && millis() - lowIn > pause){
lockLow = true;
Serial.print("motion ended at ");
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
}
}
}
Last edited by adafruit_support_bill on Wed May 29, 2013 9:02 am, edited 1 time in total.
Reason:Please use the 'code' button when submitting code
for (i=0; i<100; i++)
{
volume = analogRead(1);
// Remember this value if it's the biggest so far
if (volume > v2) {
v2 = volume;
}
delay(1);
}
This loops 100 times and the "delay(1)" causes a 1 ms delay on each iteration of the loop. If you change the 100 to 50, it will change the total delay to about 50 ms.
That will at least give you the proper refresh rate so that the servo will hold a position.
The other problem I see is that all your pulses are the same 600 microseconds. If this never varies, the mouth will never move.
600 microseconds is also too short for a servo positioning pulse. It should be between 1000 and 2000 microseconds.
for (i=0; i<50; i++) // sample for 50 ms
{
volume = analogRead(1);
// Remember this value if it's the biggest so far
if (volume > v2) {
v2 = volume;
}
delay(1);
}
///////// BILLY MOUTH MOVE ///////////////
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include <WaveHC.h>
#include <WaveUtil.h>
SdReader card; // This object holds the information for the card
FatVolume vol; // This holds the information for the partition on the card
FatReader root; // This holds the information for the filesystem on the card
FatReader f; // This holds the information for the file we're play
WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time
// this handy function will return the number of bytes currently free in RAM, great for debugging!
int freeRam(void)
{
extern int __bss_end;
extern int *__brkval;
int free_memory;
if((int)__brkval == 0) {
free_memory = ((int)&free_memory) - ((int)&__bss_end);
}
else {
free_memory = ((int)&free_memory) - ((int)__brkval);
}
return free_memory;
}
void sdErrorCheck(void)
{
if (!card.errorCode()) return;
putstring("\n\rSD I/O error: ");
Serial.print(card.errorCode(), HEX);
putstring(", ");
Serial.println(card.errorData(), HEX);
while(1);
}
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
int calibrationTime = 0;
long unsigned int lowIn;
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
/////////////////////////////////////////////////////////////////////////
int pirPin = 8; ///////////////////// pir sensor pin
/////////////////////////////////////////setup
void setup()
{
myservo.attach(16); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
pinMode(pirPin, INPUT);
putstring("Free RAM: "); // This can help with debugging, running out of RAM is bad
Serial.println(freeRam()); // if this is under 150 bytes it may spell trouble!
// Set the output pins for the DAC control. This pins are defined in the library
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
// if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you
if (!card.init()) { //play with 8 MHz spi (default faster!)
putstring_nl("Card init. failed!"); // Something went wrong, lets print out why
sdErrorCheck();
while(1); // then 'halt' - do nothing!
}
// enable optimize read - some cards may timeout. Disable if you're having problems
card.partialBlockRead(true);
// Now we will look for a FAT partition!
uint8_t part;
for (part = 0; part < 5; part++) { // we have up to 5 slots to look in
if (vol.init(card, part))
break; // we found one, lets bail
}
if (part == 5) { // if we ended up not finding one :(
putstring_nl("No valid FAT partition!");
sdErrorCheck(); // Something went wrong, lets print out why
while(1); // then 'halt' - do nothing!
}
// Lets tell the user about what we found
putstring("Using partition ");
Serial.print(part, DEC);
putstring(", type is FAT");
Serial.println(vol.fatType(),DEC); // FAT16 or FAT32?
// Try to open the root directory
if (!root.openRoot(vol)) {
putstring_nl("Can't open root dir!"); // Something went wrong,
while(1); // then 'halt' - do nothing!
}
// Whew! We got past the tough parts.
putstring_nl("Ready!");
Serial.println("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(calibrationTime - i);
Serial.print("-");
delay(1000);
}
Serial.println();
Serial.println("done");
while (digitalRead(pirPin) == HIGH) {
delay(500);
Serial.print(".");
}
Serial.print("SENSOR ACTIVE");
delay(1000);
}
//////////////////////////////////////////////// loop
void loop() {
if(digitalRead(pirPin) == HIGH){
playcomplete("PUPPET~1.WAV");
[quote]for(pos = 0; pos < 70; pos += 1) // goes from 0 degrees to 90 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(3); // waits 4ms for the servo to reach the position
}
for(pos = 70; pos>=1; pos-=1) // goes from 90 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(3); // waits 4ms for the servo to reach the position
}
if(lockLow){
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}
if(digitalRead(pirPin) == LOW){
if(takeLowTime){
lowIn = millis();
takeLowTime = false;
}
if(!lockLow && millis() - lowIn > pause){
lockLow = true;
Serial.print("motion ended at ");
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
}
}
}
// Plays a full file from beginning to end with no pause.
void playcomplete(char *name) {
// call our helper to find and play this name
playfile(name);
while (wave.isplaying) {
// Do nothing while its playing
}
// Now it's done playing
}
void playfile(char *name) {
// see if the wave object is currently doing something
if (wave.isplaying) { // Already playing something, so stop it!
wave.stop(); // Stop it
}
// Look in the root directory and open the file
if (!f.open(root, name)) {
putstring("Couldn't open file ");
Serial.print(name);
return;
}
// OK read the file and turn it into a wave object
if (!wave.create(f)) {
putstring_nl("Not a valid WAV");
return;
}
// OK time to play! Start playback
wave.play();
}