Waveform switch with DDS sketch

Hello everyone, newbie question here.

I'm using the sketch i've found in this blog http://mouro.info/signal-generator-using-arduino-and-dds/ to play sound signals out of the Arduino using the DDS technique.

I must admit i didn't understand every line of code of the sketch (and i tried to!...), but finally i understood how the whole thing works and i came up with a variable frequency and different kind of waveforms. What i wanted to do then was a realtime variable waveform switch with something like a pot or sorta. I tried to search for something similar in the forum and google but i didn't came up with a solution yet...

The part of the code i have to work on i think is this one, because that's where i've modified it to make other waveforms working.

// TIMER1 will overflow at a 62.5KHz(Sampling frequency).
// Updates the OCR1A value and the accumulator.
// Computes the next sample to be sent to the PWM.
ISR(TIMER1_OVF_vect)
{
static uint8_t osc = 0;

// Send oscillator output to PWM
OCR1A = osc;

// Update accumulator
phaseAccumulator += phaseIncrement;
index = phaseAccumulator >> 8;

// Read oscillator value for next interrupt
 osc = pgm_read_byte( &sineTable[index] );
// osc = pgm_read_byte( &squareTable[index] );
// osc = pgm_read_byte( &triTable[index] );

}

I've tried with analogRead() from a pot and an "if" construct to change the osc "value" but it doesn't work, and also the other pot for the variable stopped working.

:-? I guess that's something i'm missing in the overall process of creating the signal? any help?

Thanks.

Are you saying you can't select between square/sine/triangle tables? I'd do it outside the ISR, and just give the ISR a table pointer, as a global.

solved!
in the end i contacted João, the man behind the code i started from, who has been very helpful and guided me in the right way.

i was almost there, just forgetting the existence of the switch construct, a big fault i know :-X

ok, so for everyone who might find this helpful:
? first i mapped in the loop a pot to have three different spots

void loop()
{
  int waveSelector_analog = analogRead(5);

  if (waveSelector_analog < 350)  
       waveSelector = 1;
  else if (waveSelector_analog > 350 && waveSelector_analog < 700) 
       waveSelector = 2;
  else 
       waveSelector = 3;
}

and that’s ok.

? then in the ISR part i placed the switch construct to choose the right “value” for osc.

ISR(TIMER1_OVF_vect)
{
static uint8_t osc = 0;

// Send oscillator output to PWM
OCR1A = osc;

// Update accumulator
phaseAccumulator += phaseIncrement;
index = phaseAccumulator >> 8;

// OLD Read oscillator value for next interrupt
// osc = pgm_read_byte( &sineTable[index] );
// osc = pgm_read_byte( &squareTable[index] );
// osc = pgm_read_byte( &triTable[index] );

// Read oscillator value for next interrupt
   switch(waveSelector)
   {
       case 1:
            osc = pgm_read_byte( &sineTable[index] );
            break;
       case 2:
            osc = pgm_read_byte( &squareTable[index] );
            break;
       case 3:
            osc = pgm_read_byte( &triTable[index] );
            break;
   }
}

In the end “waveSelector” has to be obviously declared as a global variable, and that was something i was doing wrong!..
Now it’s working good!

So, thanks to João and i hope that this can help someone.
Bye.

Why not eliminate the switch in the ISR altogether, with a global table pointer selected in ‘loop’, which is what (I think) AWOL was suggesting?
Keep those ISRs lean!

That's exactly what I meant. The global table pointer is just an "unsigned int", and you add the current value of "index" to it to get the next value. Though, thinking about it further, for an even leaner ISR, you'd probably want to copy the current table to RAM when you changed the waveform selection. Quicker access to table entries, at the expense of RAM.

You are right!

Sorry for the delay of my reply but these have been crazy days...

By the way, i think i understand what you mean, but really i don't know how to realize it. My expertise in coding is not that great though :-[

So the wave selection has to be outside of the interrupt service routine, 'cause the fact that it is someway linked with something happening inside of the loop will freeze the execution as long as the loop keep going. Is it right?

And i think that's why the waveform does not change while i'm pushing a button, as i'm controlling the frequency of the tone with 8 different buttons. Instead it changes if i control the frequency with just 1 pot, because i'm not getting stuck in the loop. That's what i came up to, but i don't know if i'm right, 'cause i didnt't find a solution yet.

I tried to put the switch outside of the ISR, but it doesnt work because i don't know how to point at that value.

more detailed suggestion? thanks.