Pages: [1]   Go Down
Author Topic: Order of execution in my Waveshield code.  (Read 387 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello. I successfully merged my Arduino waveshield sketch, which plays back different files according to which number is sent to the serial port, with some sample code from the Waveshield's sit which allows for pitch control.

 I've got it so that I can control the pitch from a potentiometer, but it only changes the pitch at the beginning of each triggered sound - I have monitored the analogRead to see that I am getting continuous readings - but I am not able to get continuous control of the pitch.

I have a feeling I have just not put the code in the right place - i'm hoping someone with more experienced eyes can see where I might have gone wrong. I commented out bits that were interfering with the normal function of the sketch, but kept them there in case they are necessary. Code follows, thank you for looking!

#include <AF_Wave.h>
#include <avr/pgmspace.h>
#include "util.h"
#include "wave.h"


AF_Wave card;
File f;
Wavefile wave;
#define redled 9
void setup() {

 

 
  // set up serial port
  Serial.begin(9600);
  
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(redled, OUTPUT);




  // open memory card
  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;
  }
}

void loop() {
 Serial.println(analogRead(5), DEC);
 char c = 0;
 if(Serial.available()) {
   c = Serial.read();
 int16_t potval;
 uint32_t newsamplerate;
 int16_t lastpotval = 0;
 #define HYSTERESIS 3
 //Serial.println(c, BYTE);
  switch (c) {
   case '1':
     playfile("1.WAV");
     break;
   case '2':
     playfile("2.WAV");
     break;
   case '3':
     playfile("3.WAV");
     break;
   case '4':
     playfile("4.WAV");
     break;
   case '5':
     playfile("5.WAV");
     break;
   case '6':
     playfile("6.WAV");
     break;
   case '7':
     playfile("7.WAV");
     break;
   case '8':
     playfile("8.WAV");
     break;
   default:
   Serial.println("dogs and cats");
  
  }
 }
}


void playfile(char *name) {
  Serial.print("bang");
 int16_t potval;
 uint32_t newsamplerate;
 int16_t lastpotval = 0;
 #define HYSTERESIS 3

 
  // stop any file already playing
  if (wave.isplaying) {  
    wave.stop();
  }
  // close file if open
  card.close_file(f);
  // play specified file
  
  f = card.open_file(name);
  if (f && wave.create(f)) {
  wave.play();
     // while (wave.isplaying) {
     potval = analogRead(5);
     //if ( ((potval - lastpotval) > HYSTERESIS) || ((lastpotval - potval) > HYSTERESIS)) {
         putstring("pot = "); Serial.println(potval, DEC);
         putstring("tickspersam = "); Serial.print(wave.dwSamplesPerSec, DEC); putstring(" -> ");
         newsamplerate = wave.dwSamplesPerSec;
         newsamplerate *= potval;
        newsamplerate /= 512;   // we want to 'split' between sped up and slowed down.
        if (newsamplerate > 24000) {
          newsamplerate = 24000;  
        }
        wave.setSampleRate(newsamplerate);
        
        Serial.println(newsamplerate, DEC);
        lastpotval = potval;
     //}
     //delay(100);
   //}

  
}
}
Logged

Pages: [1]   Go Up
Jump to: