I've added extra wavetables to the "_1khz_sine_gen_potpitch" sketch , added the "weighted average" function, but it doesn't quite work.
weightValue = (analogRead(potPin) / 1024);
weightRemainder = (1 - weightValue);
(((weightValue * sineArray_) + (weightRemainder * triangleArray*)) == weighted average; [/quote]_
warning: no proper syntax applied, just in plain words...
The last line always returned 0, works (sort of) when reversed:
weighted average= (((weightValue * sineArray_) + (weightRemainder * triangleArray))
I've been at it for hours now, and I'm getting somewhat discouraged, so i'll post the sketch as is.
Could someone have a look at this:
```_
/* intro skipped for now */
#include <avr/interrupt.h>
#include <stdlib.h>
//Preset "classic" synthesizer wavetables.
byte wavetableA [32]; // sinus
byte wavetableB [32]; //square
byte wavetableC [32]; //rampUp
byte wavetableD [32]; //rampDown
byte wavetableE [32];//triangle
byte wavetableF [32]; //pulse33
byte wavetableG [32]; //pulse25
//byte wavetableH [32]; //noise , not yet implemented
byte wavetableI [32]; // Contains all zero's . For easy testing of "Weighted_Average" of two wavetables. Also usable as a volume function.
// insert more wavetables here....
byte wavetableOUT [32]; // the output waveform
//to do: implement your way to select the wavetable. Now defauls to wavetableA, sinus.
float weightValue; // Needed to test the "Weighted_Average" function to average two wavetables
float weightRemainder; //Needed to test the "Weighted_Average" function to average two wavetables
int i ;
int pitchval; // used for pitch
// fuctions used in the loop
void ioinit (void)
{
//Initialize output ports
PORTD = B11111111;
DDRD = B11111111;
}
void timer_setup(){
TCCR2A = 0;
TCNT2=455; //455 outputs 1.007khz
TCCR2B = B00000010;
//Timer2 Overflow Interrupt Enable
TIMSK2 = 1<<TOIE2;
}
// end of functions
void setup(){
Serial.begin (9600); // to monitor with Serial.print
ioinit(); // sets the output pins
arraysetup_sinus();
arraysetup_square();
arraysetup_rampUp();
arraysetup_rampDown();
arraysetup_triangle();
arraysetup_pulse33();
arraysetup_pulse25();
// arraysetup_noise(); // noise not yet implemented
cli();
timer_setup();
i = 0;
sei();
}
ISR(TIMER2_OVF_vect) {
PORTD=(wavetableOUT[i++]); // choose wavetable option here!
TCNT2=pitchval; //sets the pitch
if(i==32){ // loop the output
i=0;
}
}
void arraysetup_sinus(void){
wavetableA[0]=127; // Put 32 step 8 bit sine table into array.
wavetableA[1]=152;
wavetableA[2]=176;
wavetableA[3]=198;
wavetableA[4]=217;
wavetableA[5]=233;
wavetableA[6]=245;
wavetableA[7]=252;
wavetableA[8]=254;
wavetableA[9]=252;
wavetableA[10]=245;
wavetableA[11]=233;
wavetableA[12]=217;
wavetableA[13]=198;
wavetableA[14]=176;
wavetableA[15]=152;
wavetableA[16]=128;
wavetableA[17]=103;
wavetableA[18]=79;
wavetableA[19]=57;
wavetableA[20]=38;
wavetableA[21]=22;
wavetableA[22]=10;
wavetableA[23]=3;
wavetableA[24]=0;
wavetableA[25]=3;
wavetableA[26]=10;
wavetableA[27]=22;
wavetableA[28]=38;
wavetableA[29]=57;
wavetableA[30]=79;
wavetableA[31]=103;
}
void arraysetup_square(void){
wavetableB[0]=255; // Put 32 step 8 bit square table into array.
wavetableB[1]=255;
wavetableB[2]=255;
wavetableB[3]=255;
wavetableB[4]=255;
wavetableB[5]=255;
wavetableB[6]=255;
wavetableB[7]=255;
wavetableB[8]=255;
wavetableB[9]=255;
wavetableB[10]=255;
wavetableB[11]=255;
wavetableB[12]=255;
wavetableB[13]=255;
wavetableB[14]=255;
wavetableB[15]=255;
wavetableB[16]=0;
wavetableB[17]=0;
wavetableB[18]=0;
wavetableB[19]=0;
wavetableB[20]=0;
wavetableB[21]=0;
wavetableB[22]=0;
wavetableB[23]=0;
wavetableB[24]=0;
wavetableB[25]=0;
wavetableB[26]=0;
wavetableB[27]=0;
wavetableB[28]=0;
wavetableB[29]=0;
wavetableB[30]=0;
wavetableB[31]=0;
}
//Oops , code too long...*
```