playing 2 or more different sound samples each one triggered by its own button? sound really exciting!! Thanks again for your important help.. this should be my last request: i rearranged (a little piece of) my code with your useful advices, so could you please give it a glance and tell me if it's everything ok and what i can do to improve it? here it is:
#define dataP 11 //data pin for the shift reg
#define clockP 1 //clock pin for the shift reg
#define latchP 8 //latch clock pin for the shift reg
#define LAST_SAMPLE 4096 //max size of every sample
#define PIEZOTHRESHOLD 150 //the fewest value that can be read
#define TOTAL_PINS 6 //number of analog devices that trim the samples
//those are the audio samples, codified in 8bit
//the last array contains all the previous data
uint8_t Sample1[LAST_SAMPLE];
uint8_t Sample2[LAST_SAMPLE];
uint8_t Sample3[LAST_SAMPLE];
uint8_t Sample4[LAST_SAMPLE];
uint8_t Sample5[LAST_SAMPLE];
uint8_t Sample6[LAST_SAMPLE];
uint8_t* Samples[TOTAL_PINS]={Sample1,Sample2,Sample3,Sample4,Sample5,Sample6};
int playflags[TOTAL_PINS]={0,0,0,0,0,0}; //flags: 0=unset 1=set
int piezovals[TOTAL_PINS]; //values read from each pin
int i[TOTAL_PINS]={0,0,0,0,0,0}; //the counters, every sample has its own
int pins;
int samp;
int currentMicros;
int previousMicros;
void setup()
{
//set up the shift reg pins
pinMode(latchP, OUTPUT);
pinMode(clockP, OUTPUT);
pinMode(dataP, OUTPUT);
}
void loop()
{
for(pins=0;pins<TOTAL_PINS;pins++){ //Search for any not-yet-activated pin,
if(playflags[pins]==0){ //then check if some of them have been pressed
piezovals[pins]=readPiezo(pins); //using a function that return its "velocity" or 0.
if(piezovals[pins]!=0) //If the value is different from 0 (in other words, if
playflags[pins]=1; //the botton has been pressed) set his flag.
}
}
currentMicros=micros(); //Read the current time, checking if 32us have passed
if(currentMicros-previousMicros>=32){
previousMicros=currentMicros;
for(pins=0;pins<TOTAL_PINS;pins++){ //Search among all the pins
if(playflags[pins]==1){ //the ones whose flag has been set.
samp=i[pins]; //Temporarily save the current counter
digitalWrite(latchP, LOW); //Toggle the latch clock
shiftOut(dataP, clockP, MSBFIRST, Samples[pins][samp]); //Send the data to the shift reg
digitalWrite(latchP, HIGH); //Toggle the latch clock again
i[pins]++; //Increase the counter
if(i[pins]==LAST_SAMPLE){ //If the the last sample has been reached,
i[pins]=0; //reset the current counter and
playflags[pins]=0; //toggle his respective flag.
}
}
}
}
}
//This function reads a given analog pin, and returns his average
//velocity, checking how many times his value stays above threshold/2.
//In this way, is possible to play sounds at different velocities, only
//multiply or divide the last shiftOut parameter by a certain value
//(related to the velocity we've read). I'm wondering if all this might
//add to much delay between the pression and the play.
int readPiezo(int piezoPin)
{
int val, t;
val=analogRead(piezoPin);
if(val>=PIEZOTHRESHOLD){
t=0;
while(analogRead(piezoPin)>=PIEZOTHRESHOLD/2)
t++;
return(t*2);
}
return(0);
}
thanks in advance!!