Ok, this could have been a continuation from another thread, but seeing as I have gone a long way from the original thread's title, and I am facing whole new problems, I figure it's worth starting a new thread with a relevant title for this.
What I am trying to do is use a pot to select a couple of wave arrays and to select the mix of the two chosen arrays to go to the output.
To save using processor time on continuous analogReads, I have set up a periodic analogRead using this:
unsigned long currentTime = millis();
if(currentTime - previousTime >= (readWave)) //check to see if it's time to play the next sample based on pitch decided by the pitch pot
{
wave = analogRead(A0); // potentiometer that controls the blend
Within the if statment, i want the two wave arrays to be selected by the position of the pot on A0. I have tried various methods for this already to no avail. My latest idea is to create two more arrays that hold the wave array addresses (pointers?) that I can access with index varialble "j".
I am having two problems with this. Firstly, I am struggling to get my new address/pointer arrays to be compatible with the wave arrays. When I verify it tells me there are problems with the two data types. Secondly, I'm not sure how to then call up the wave array data for the selected arrays. The loop currently looks like this:
void loop()
{
unsigned long currentTime = millis();
if(currentTime - previousTime >= (readWave)) //check to see if it's time to play the next sample based on pitch decided by the pitch pot
{
wave = analogRead(A0); // potentiometer that controls the blend between xthree and xfive
previousTime = currentTime; // regester the current time in previousMillis
waveSelect = map(wave, 0, 1024, 0, 4); // works out which wavetables will be used (have to map an extra sector to take imaginary number 1024
waveMix1 = (((waveSelect+1)*256-1)-wave); // calculates the weight of first wave in osc1
waveMix2 = (wave-(256*waveSelect)); // calculates the weight of second wave in osc1
j = waveSelect; // j is the index for the arrays that holds the wave array addresses
}
osc1 = (wave1[j]*waveMix1)+(wave2[j]*waveMix2); // wave1[] and wave2[] hold the selected array addresses
Here is the entire code, leaving out the figures in the wave arrays to save on charactors.
Any help you can give would be greatly appreciated!
Pete
long previousTime = 0; // will store last time LED was updated
int readWave = 100;
short waveSelect; // used to work out which oscillators to mix between
int wave;
int waveMix1;
int waveMix2;
unsigned int sine; // sine wave array
unsigned int osc1; // final oscillator 1 mix
unsigned int out; // total output
short i = 0; // "i" is soon to become the array index, and is set to start at index 0
short j = 0; // "j" is the array index for the arrays containing the wave array addresses (wave1[] and wave2[])
char *wave1[4]; // arrays containing the address of the arrays
char *wave2[4];
unsigned char xtwo[256] = // xtwo[] is an array that holds the sample data for a x2-base251 wave
{
};
unsigned char xthree[256] = // xthree[] is an array that holds the sample data for a x3-base251 wave
{
};
unsigned char xfour[256] = // xfour[] is an array that holds the sample data for a x4-base251 wave
{
};
unsigned char xfive[256] = // xfive[] is an array that holds the sample data for a x5-base251 wave
{
};
unsigned char xsix[256] = // xsix[] is an array that holds the sample data for a x6-base251 wave
{
};
unsigned char xsine[256] = //sine wave array
{
};
void setup()
{
DDRC = 255; // 8 bit DAC on port C
Serial.begin(9600); // begin serial communication
wave1[0] = xtwo; //arrays that hold the addresses of the wave arrays
wave1[1] = xthree;
wave1[2] = xfour;
wave1[3] = xfive;
wave2[0] = xthree;
wave2[1] = xfour;
wave2[2] = xfive;
wave2[3] = xsix;
}
void loop()
{
unsigned long currentTime = millis();
if(currentTime - previousTime >= (readWave)) //check to see if it's time to play the next sample based on pitch decided by the pitch pot
{
wave = analogRead(A0); // potentiometer that controls the blend between xthree and xfive
previousTime = currentTime; // regester the current time in previousMillis
waveSelect = map(wave, 0, 1024, 0, 4); // works out which wavetables will be used (have to map an extra sector to take imaginary number 1024
waveMix1 = (((waveSelect+1)*256-1)-wave); // calculates the weight of first wave in osc1
waveMix2 = (wave-(256*waveSelect)); // calculates the weight of second wave in osc1
j = waveSelect; // j is the index for the arrays that holds the wave array addresses
}
Serial.println(wave1[j]);
Serial.println(wave2[j]);
Serial.println();
Serial.println(xtwo[i]);
Serial.println(xthree[i]);
Serial.println();
Serial.println();
osc1 = (wave1[j]*waveMix1)+(wave2[j]*waveMix2);
out = (osc1+(sine))/512; //mix the wavetables with the sine wave
PORTC = out; // output the mixed signal before doing anything else for speed and accuracy?
i++; // increase i in increments to cycle through the waveforms
if(i >= 256) // check if the index is at the end of the sample
i = 0; // if it is, reset the sample to 0
}