Wednesday, October 21, 2009

Process

The code below didn't originally work when I placed my sensor code into it. Nothing did, I couldn't figure out why. I tested and experimented everything I could find. I realized after doing a processing/arduino communication text the way the serial port reads information being sent to it. I thought to check at that point if if was reading the wav files on the sd card the same way I saved them to the disk. It didn't. That was the problem all along. This is what I used for the final code. It's a little more complicated than it needs to be in some parts. I could now simplify it down, but this is what was used for the final project and it works fine.



#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