Tuesday, 26 May 2009

tuesday, 1 day til move, 3 days til deadline

lots and lots and lots of worry.

had to buy a last minute addition, a PIR sensor from Active-robots.co.uk to go with project two, the proverb machine. the buttons were just too clumsy and wanted something a little less touchy. the PIR sensor should be really quick to connect to 5V, GND & an14 so that's good.


Sketch will be as follows,



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

AF_Wave card;
File f;
Wavefile wave;

#define playcomplete(x) ROM_playcomplete(PSTR(x))

#define DEBOUNCE 100

#define snsr 14 //sensor connected to analogue0

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(snsr, OUTPUT);

// enable pull-up resistors on
// switch pins (analog inputs)
digitalWrite(14, HIGH);


// open memory card

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 sensorstate = 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 /= 8;

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

if (distsensor <= 10) {
digitalWrite(snsr, HIGH);
}
if (distsensor > 10) {
digitalWrite(snsr, LOW);
sensorstate = 1;
// nobody there. one out of 200 times play one of the sounds (once every few minutes)
i = random(200);
//Serial.println(i);
if (i == 0) {
i = random(3);
if (i == 0) {
playcomplete("rise so high.WAV");
} else if (i == 1) {
playcomplete("smoothseas.WAV");
} else if (i == 2) {
playcomplete("to climb steep hills.WAV");
} else {
playcomplete("exhalted throne.WAV");
}
}
} else if ((distsensor > 30) && (distsensor < 40)) {
if (sensorstate <= 1) { // play "smoothseas"
playcomplete("smoothseas.WAV");
} else {
i = random(60); // more often
//Serial.println(i);
if (i == 0) {
i = random(3);
if (i == 0) {
playcomplete("exhalted throne.WAV");
} else if (i == 1) {
playcomplete("to climb steep hills.WAV");
} else {
playcomplete("rise so high.WAV");
}
}
}

}
}




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) {
volume = 0;
for (i=0; i<8; i++) {
v2 = analogRead(1) - 512;
if (v2 < 0)
v2 *= -1;
if (v2 > volume)
volume = v2;
delay(5);
}

}
//putstring("vol = "); Serial.println(volume, DEC);
}



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





hope it's right!!

No comments: