Step 8Hook up the arduino
If the code isn't visible below, I included a txt file of it so you can just download it that way.
-----------------------------------------------------------------------------------------------------------------------------------
#include
#include
#include "util.h"
#include "wave.h"
#define DEBOUNCE 100
#define swPin 14
#define eyeleds 18
AF_Wave card;
File f;
Wavefile wave;
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 turnRate = 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
int randNumber; //random number variable to allow the choosing of which wav will be played
/** 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
void setup() {
// set up serial port
Serial.begin(9600);
pinMode(inputPin, INPUT); // declare sensor as input for PIR
pinMode(eyeleds, OUTPUT); // declare sensor as output for eyes
// 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 waveshield pins
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
// open memory card
if (!card.init_card()) {
putstring_nl("Card init failed!"); return;
}
if (!card.open_partition()) {
putstring_nl("No partition!"); return;
}
if (!card.open_filesys()) {
putstring_nl("Couldn't open filesys"); return;
}
if (!card.open_rootdir()) {
putstring_nl("Couldn't open dir"); return;
}
}
void loop()
{
val = digitalRead(inputPin);
if (val == HIGH)
{
if (pirState == LOW)
{
// we have just turned on
Serial.println("Motion!");
//Turn eyes on
digitalWrite(eyeleds, HIGH);
// Play a sound:
randNumber = 0;
randNumber = random(3);
if (randNumber >= 1)
{
randNumber = random(3);
randNumber = randNumber + 1;
Serial.println(randNumber);
if (randNumber == 1)
{
playcomplete("11.WAV");
}
else if (randNumber == 2)
{
playcomplete("2.WAV");
}
else if (randNumber == 3)
{
playcomplete("13.WAV");
}
else if (randNumber == 4)
{
playcomplete("1.WAV");
}
}
pirState = HIGH;
}
}
else
{
if (pirState == HIGH)
{
digitalWrite(eyeleds, LOW);
// we have just turned off
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
void playcomplete(char *name)
{
char i;
uint8_t volume;
int v2;
playfile(name);
while (wave.isplaying)
{
volume = 0;
for (i=0; i<8; i++)
{
v2 = analogRead(1);
delay(5);
}
if (v2 > 440)
{
pulseWidth = 1800;
mouthchange = 1;
}
else
{
pulseWidth = 800;
mouthchange = 1;
}
digitalWrite(servoPin, HIGH); // start the pulse
delayMicroseconds(pulseWidth); // pulse width
digitalWrite(servoPin, LOW); // stop the pulse
}
card.close_file(f);
digitalWrite(eyeleds, LOW);
// we have just turned off
}
void playfile(char *name)
{
// stop any file already playing
if (wave.isplaying)
{
wave.stop();
card.close_file(f);
}
f = card.open_file(name);
if (f && wave.create(f))
{
wave.play();
}
}
| « Previous Step | Download PDFView All Steps | Next Step » |












































sorry my key boards broke so some keys dont work, but yeah i compile this and get that error message is there anything i have to add, i need this by halloween.
Also missing in the code here, there are 2 Include statements that are blank right at the top of the code. "Instructables doesnt like the "<>" symbols posted in the code and it dropped the information after the #include statement. I typed out the names of the symbols here before and after the information that is required.
The first one should be #include (less than symbol) AF_Wave.h (greater than symbol)
The second one should be "#include (less than symbol)avr/pgmspace.h
(greater than symbol)
It took me a bit to figure out (i'm new to arduino and not a programmer) looking at other examples of code lead me to change those two include statements
I loaded the code last night (without a servo hooked up yet) and the wav portion works no problem.
Cheers