Wednesday, October 12, 2011

And Just About Ready to Set Up At Bus Stop - Natalie, Netalia, Dailey

Finished circuit set up:
Finished Code:
/*
NOTES:
 contact1 to digital 6 and 5V
 contact2 to digital 7 and 5V
 contact3 to digital 8 and 5V
 speaker to digital 9
 
 do note: requires waveHC library
 
 !!all audio needs to be 22KHz 16-bit .wavs at the most!!
 */
int contact1 = 6;
int contact2 = 7;
int contact3 = 8;
#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 volumes root directory
FatReader file;   // This object represent the WAV file 
WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time

// time to play each tone in milliseconds
#define PLAY_TIME 61000

// Define macro to put error messages in flash memory
#define error(msg) error_P(PSTR(msg))
/*for test melody
 #include "pitches.h"  //this is just for testing noise
 int melody[] = {
 NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
 int noteDurations[] = {
 4, 8, 8, 4,4,4,4,4 };  // note durations: 4 = quarter note, 8 = eighth note, etc.:
 */
void setup() {
  Serial.begin(9600);
  pinMode(contact1, OUTPUT);
  pinMode(contact2, OUTPUT);
  pinMode(contact3, OUTPUT);
  pinMode(9, OUTPUT);
  if (!card.init()) error("card.init");
  // enable optimized read - some cards may timeout
  card.partialBlockRead(true);
  if (!vol.init(card)) error("vol.init");
  if (!root.openRoot(vol)) error("openRoot");
}

void loop() {
  int val1 = digitalRead(contact1);     //this is technically unnecessary, but seems to work better, so whatever
  int val2 = digitalRead(contact2);  
  int val3 = digitalRead(contact3);
  if (val1 == HIGH | val2 == HIGH | val3 == HIGH){
    Serial.print("contact1:  "); //so we can see what's going on, but only if contact is made, otherwise it'd be a madhouse
    Serial.println(val1);  
    Serial.print("contact2:  ");
    Serial.println(val2);
    Serial.print("contact3:  ");
    Serial.println(val3);
  }
  delay(20);  //slight delay between contact and play
  if (val1 == HIGH | val2 == HIGH | val3 == HIGH){ 
    /*PgmPrintln("Index files");
    indexFiles();

    PgmPrintln("Play files by index");
    playByIndex();
*/
PgmPrintln("Play files by name");
playByName();

    /*test melody
     for (int thisNote = 0; thisNote < 8; thisNote++) {
     int noteDuration = 1000/noteDurations[thisNote];
     tone(8, melody[thisNote],noteDuration);
     int pauseBetweenNotes = noteDuration * 1.30;
     delay(pauseBetweenNotes);
     }
     */
    delay(500);   //this will make it so if two people sit down almost at the same time, only one sound will play, but who knows 
  }
  else {
  }
}
/*
 * print error message and halt
 */
void error_P(const char *str) {
  PgmPrint("Error: ");
  SerialPrint_P(str);
  sdErrorCheck();
  while(1);
}
/*
 * print error message and halt if SD I/O error, great for debugging!
 */
void sdErrorCheck(void) {
  if (!card.errorCode()) return;
  PgmPrint("\r\nSD I/O error: ");
  Serial.print(card.errorCode(), HEX);
  PgmPrint(", ");
  Serial.println(card.errorData(), HEX);
  while(1);
}
/*
char fileLetter[] =  {
  '0', '1', '2', '3', '4', '5', '6', 
  '7', '8', '9', 'A', 'B', 'C', 'D', 'P', 'S'};

// index of DTMF files in the root directory
uint16_t fileIndex[FILE_COUNT];
*/
/*
 * Find files and save file index.  A file's index is is the
 * index of it's directory entry in it's directory file. 
 */
/*void indexFiles(void) {
  char name[10];
   
   // copy flash string to RAM
   strcpy_P(name, PSTR("DTMFx.WAV"));
   
   for (uint8_t i = 0; i < FILE_COUNT; i++) {
   
   // Make file name
   name[4] = fileLetter[i];
   
   // Open file by name
   if (!file.open(root, name)) error("open by name");
   
   // Save file's index (byte offset of directory entry divided by entry size)
   // Current position is just after entry so subtract one.
   fileIndex[i] = root.readPosition()/32 - 1;   
   }
  PgmPrintln("Done");
}
 // Play file by index and print latency in ms
void playByIndex(void) {
  for (uint8_t i = 0; i < FILE_COUNT; i++) {
    // start time
    uint32_t t = millis();
    // open by index
    if (!file.open(root, fileIndex[i])) {
      error("open by index");
    }
    // create and play Wave
    if (!wave.create(file)) error("wave.create");
    wave.play();
    // print time to open file and start play
    Serial.println(millis() - t);
    // stop after PLAY_TIME ms
    while((millis() - t) < PLAY_TIME);
    wave.stop();
    // check for play errors
    sdErrorCheck();
  }
  PgmPrintln("Done");
}*/
#define FILE_COUNT 6      //change these two things for audio files
char fileLetter[] =  {'0', '1', '2', '3', '4', '5'}; 
int i = 0;
void playByName(void) {
  char name[10];
  // copy flash string to RAM
  strcpy_P(name, PSTR("ABBAx.wav"));
//  for (uint8_t i = 0; i < FILE_COUNT; i++) {
    // start time
    uint32_t t = millis();
    // make file name
    name[4] = fileLetter[i];
    Serial.println(name);
    // open file by name
    if (!file.open(root, name)) error("open by name"); 
    // create wave and start play
    if (!wave.create(file)) error("wave.create");
    wave.play();
    // print time
    Serial.println(millis() - t);
    // stop after PLAY_TIME ms
    while((millis() - t) < PLAY_TIME);
    wave.stop();
    // check for play errors
    sdErrorCheck();
    i++;  //this was me, so it plays next file next person
    if (i == FILE_COUNT) {
      i = 0;
    }     //this will make it start over when it hits last defined file
//  }
  PgmPrintln("Done");
}

No comments:

Post a Comment