Thursday, June 19, 2014

Audio File reading from arduino

Code

/*

 Demonstrates the use of the Audio library for the Arduino Due

 Hardware required :
 *Arduino shield with a SD card on CS 4 (the Ethernet sheild will work)
 *Speaker attched to ground and DAC0

 Original by Massimo Banzi September 20, 2012
 Modified by Scott Fitzgerald October 19, 2012

*/


#include <SD.h>
#include <SPI.h>
#include <Audio.h>

void setup()
{
  // debug output at 9600 baud
  Serial.begin(9600);

  // setup SD-card
  Serial.print("Initializing SD card...");
  if (!SD.begin(4)) {
    Serial.println(" failed!");
    return;
  }
  Serial.println(" done.");
  // hi-speed SPI transfers
  SPI.setClockDivider(4);

  // 44100 Hz stereo => 88200 sample rate
  // 100 mSec of prebuffering.
  Audio.begin(88200, 100);
}

void loop()
{
  int count=0;

  // open wave file from sdcard
  File myFile = SD.open("test.wav");
  if (!myFile) {
    // if the file didn't open, print an error and stop
    Serial.println("error opening test.wav");
    while (true);
  }

  const int S=1024; // Number of samples to read in block
  short buffer[S];

  Serial.print("Playing");
  // until the file is not finished
  while (myFile.available()) {
    // read from the file into buffer
    myFile.read(buffer, sizeof(buffer));

    // Prepare samples
    int volume = 1023;
    Audio.prepare(buffer, S, volume);
    // Feed samples to audio
    Audio.write(buffer, S);

    // Every 100 block print a '.'
    count++;
    if (count == 100) {
      Serial.print(".");
      count = 0;
    }
  }
  myFile.close();

  Serial.println("End of file. Thank you for listening!");
  while (true) ;
}
 

Reading configuration file from an SD-card

#include <SD.h>
#include <ctype.h>
File myFile;
struct parameters {
  int interval;
  boolean co2;
  boolean temp;
  boolean rh;
  boolean lux;
  boolean valid;
  boolean heater;
  String lokaal;
} settings;
// Setting for SD-card reader
const int chipSelect = 10;
void getSettings()
{
 // Open the settings file for reading:
  myFile = SD.open("settings.txt");
  char character;
  String description = "";
  String value = "";
  boolean valid = true;
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      character = myFile.read();
             if(character == '/')         {
               // Comment - ignore this line
               while(character != '\n'){
                 character = myFile.read();
               };
      } else if(isalnum(character))      {  // Add a character to the description
        description.concat(character);
      } else if(character =='=')         {  // start checking the value for possible results
      // First going to trim out all trailing white spaces
      do {
        character = myFile.read();
      } while(character == ' ');
        if(description == "interval") {
          value = "";
          while(character != '\n') {
            if(isdigit(character)) {
              value.concat(character);
            } else if(character != '\n') {
              // Use of invalid values
              valid = false;
            }
            character = myFile.read();            
          };
          if (valid) { 
            // Convert string to array of chars
            char charBuf[value.length()+1];
            value.toCharArray(charBuf,value.length()+1);
            // Convert chars to integer
            settings.interval = atoi(charBuf);
          } else {
            // revert to default value for invalid entry in settings
            settings.interval = 60;
          }
        } else if(description == "co2") {
           if (character=='1') {
             settings.co2 = true;
           } else {
             settings.co2 = false;
           }
        } else if(description == "rh") {
           if (character=='1') {
             settings.rh = true;
           } else {
             settings.rh = false;
           }        
        } else if(description == "temp") {
           if (character=='1') {
             settings.temp = true;
           } else {
             settings.temp = false;
           }
        } else if(description == "lux") {
           if (character=='1') {
             settings.lux = true;
           } else {
             settings.lux = false;
           }        
        } else if(description == "heater") {
           if (character=='1') {
             settings.heater = true;
           } else {
             settings.heater = false;
           }        
        } else if(description == "location") {
           value = "";
           do {
             value.concat(character);
             character = myFile.read();
           } while(character != '\n');
           settings.lokaal = value;
        
        }else { // unknown parameter
          while(character != '\n')
          character = myFile.read();
        }
        description = "";
      } else {
        // Ignore this character (could be space, tab, newline, carriage return or something else)
      }
    
    }
    // close the file:
    myFile.close();
}
void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  Serial.println("Starting...");
  pinMode(10, OUTPUT);
  
  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
  }
  getSettings();
  Serial.print("Interval: ");
  Serial.println(settings.interval);
  Serial.print("CO2: ");
  if(settings.co2) { Serial.println("YES"); } else { Serial.println("NO"); }
  Serial.print("TEMP:");
  if(settings.temp) { Serial.println("YES"); } else { Serial.println("NO"); }
  Serial.print("RH:  ");
  if(settings.rh) { Serial.println("YES"); } else { Serial.println("NO"); }
  Serial.print("lux: ");
  if(settings.lux) { Serial.println("YES"); } else { Serial.println("NO"); }
  Serial.print("htr: ");
  if(settings.heater) { Serial.println("YES"); } else { Serial.println("NO"); }
  Serial.print("Lokaal: ");
  Serial.println(settings.lokaal);
  
}
void loop()
{
 // nothing happens after setup
}

wav file on sdcard using arduino

code

#include <SD.h>                      // need to include the SD library
//#define SD_ChipSelectPin 53  //example uses hardware SS pin 53 on Mega2560
#define SD_ChipSelectPin 10  //using digital pin 4 on arduino nano 328
#include <TMRpcm.h>           //  also need to include this library...

TMRpcm tmrpcm;   // create an object for use in this sketch
char mychar;

void setup(){

  tmrpcm.speakerPin = 9; //11 on Mega, 9 on Uno, Nano, etc

  Serial.begin(9600);
  if (!SD.begin(SD_ChipSelectPin)) {  // see if the card is present and can be initialized:
    Serial.println("SD fail");  
    return;   // don't do anything more if not
  }
  tmrpcm.play("beware.wav"); //the sound file "music" will play each time the arduino powers up, or is reset
}

void loop(){  

  if(Serial.available()){   
    mychar = Serial.read();

    if(mychar == 'o'){ //send the letter p over the serial monitor to start playback
      tmrpcm.play("helpyou.wav");
    } else if(mychar == 'r'){ //send the letter p over the serial monitor to start playback
      tmrpcm.play("chortle.wav");
    } else if(mychar == 'q'){ //send the letter p over the serial monitor to start playback
      tmrpcm.play("helpyou.wav");
    } else if(mychar == 'p'){
      tmrpcm.play("beware.wav");
    }
    else if(mychar == 'w'){ //send the letter p over the serial monitor to start playback
      tmrpcm.play("impresiv.wav");
    }
    else if(mychar == 't'){ //send the letter p over the serial monitor to start playback
      tmrpcm.play("seekyoda.wav");
    }
    else if(mychar == 'y'){ //send the letter p over the serial monitor to start playback
      tmrpcm.play("sensefear.wav");
    }
    else if(mychar == 'u'){ //send the letter p over the serial monitor to start playback
      tmrpcm.play("strongami.wav");
    }
    else if(mychar == 'i'){ //send the letter p over the serial monitor to start playback
      tmrpcm.play("whyhere.wav");
    }
  }

}

python class topic video