Wednesday, October 21, 2009

Process work

The sensor code was easy to make, playing with the wav shield code was the most difficult.

I found this site to be the most helpful: http://www.ladyada.net/make/waveshield/

It pretty much shows you how to install, use the chip, modify files, and what beginning code to get started with.



I had a lot of trouble and looked at countless amounts of code to get this part of of the code to function. I ended up using this code and modified it for my project to function with a force sensor:

#include
#include
#include "util.h"
#include "wave.h"

AF_Wave card;
File f;
Wavefile wave; // only one!

#define playcomplete(x) ROM_playcomplete(PSTR(x)) // save RAM by using program memory strings

#define servo 7
#define redled 9
#define eyeleds 18
#define mouthleds 17
#define midmouthleds 16
#define outermouthleds 19

void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Wave test!");

pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(redled, OUTPUT);
pinMode(servo, OUTPUT);
pinMode(eyeleds, OUTPUT);
pinMode(outermouthleds, OUTPUT);
pinMode(midmouthleds, OUTPUT);
pinMode(mouthleds, OUTPUT);

randomSeed(analogRead(0));


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;
}

putstring_nl("Files found:");
ls();
}

void ls() {
char name[13];
int ret;

card.reset_dir();
putstring_nl("Files found:");
while (1) {
ret = card.get_next_name_in_dir(name);

if (!ret) {
card.reset_dir();
return;
}
Serial.println(name);
}
}


void pulseServo(uint8_t servopin, uint16_t p) {

digitalWrite(servopin, HIGH);
delayMicroseconds(600);
while (p--) {
delayMicroseconds(4);
}
digitalWrite(servopin, LOW);
delay(18);
}

uint8_t pumpkinstate = 0;

void loop() {
int distsensor, i;
long time;
/*
for (i=0; i<50; i++) {
pulseServo(servo,0);
}
for (i=0; i<50; i++) {
pulseServo(servo,400);
}
return;
*/
distsensor = 0;
for (i=0; i<8; i++) {
distsensor += analogRead(0);
delay(50);
}
distsensor /= 4;

putstring("Sensor = "); Serial.println(distsensor);


if (distsensor > 30 && distsensor <=150) {
playcomplete("SARAH1.WAV");
} else if(distsensor > 150 && distsensor <=250)
{
playcomplete("SARAH2.WAV");
}else if(distsensor > 250&& distsensor <= 400){
playcomplete("SARAH3.WAV");
}else if(distsensor > 400 && distsensor <=650){
playcomplete("SARAH4.WAV");
}else if(distsensor > 650 && distsensor <=900){
playcomplete("SARAH5.WAV");
}else if(distsensor >900 && distsensor <=1250){
playcomplete("SARAH6.WAV");
}else {
Serial.println("no signal");
}}





void ROM_playcomplete(const char *romname) {
char name[13], i;
uint8_t volume;
int v2;

for (i=0; i<13; i++) {
name[i] = pgm_read_byte(&romname[i]);
}
name[12] = 0;
Serial.println(name);
playfile(name);
while (wave.isplaying) {

delay(5);
}

card.close_file(f);
}

void playfile(char *name) {
f = card.open_file(name);
if (!f) {
putstring_nl(" Couldn't open file"); return;
}
if (!wave.create(f)) {
putstring_nl(" Not a valid WAV"); return;
}
// ok time to play!
wave.play();
}

No comments:

Post a Comment