Frequency changeable sine wave generator by tact switches

Hi,

Now I'm going to make up a arduino pro mini sine wave generator which is able to change the frequency by pushing tact switches.
However, I'm in trouble...
Pushing switches make change the "value" on the code, but the changed "value" doesn't makes any effect on the sine wave output.

Give me some advice to fix it.

/* http://arduino.cc/en/Hacking/LibraryTutorial */
/* http://ww1.microchip.com/downloads/en/DeviceDoc/22250A.pdf */

#include <avr/pgmspace.h>
#include <MCP4922.h>
#include <SPI.h>
#include "SineWaveTable.h"
//MCP4922 DAC(51,52,53,5);    // (MOSI,SCK,CS,LDAC) define Connections for MEGA_board, 
//MCP4922 DAC(11,13,10,5);    // (MOSI,SCK,CS,LDAC) define Connections for UNO_board, 
MCP4922 DAC(11,13,10,5);  //  (MOSI,SCK,CS,LDAC) define Connections for pro_mini_board, 
volatile int value = 30;
const int plusbuttonPin = 2; const int minusbuttonPin = 3;
const int LEDPin1 = 9; const int LEDPin2 = 8; int i = 0;


void setup(void)
{
  //Serial.begin(9600);
  SPI.begin();
  pinMode(plusbuttonPin,INPUT_PULLUP);
  pinMode(minusbuttonPin,INPUT_PULLUP);
  pinMode(LEDPin1,OUTPUT);
  pinMode(LEDPin2,OUTPUT);
  attachInterrupt(digitalPinToInterrupt(plusbuttonPin),button,CHANGE);
  attachInterrupt(digitalPinToInterrupt(minusbuttonPin),button,CHANGE);
}

void loop(void)
{
  unsigned long nowmicros = micros();  unsigned long prevmicros;
  if (nowmicros - prevmicros > value){
      prevmicros = nowmicros;
      DAC.Set(pgm_read_word(&(DACLookup_FullSine_8Bit[i])),4095- pgm_read_word(&(DACLookup_FullSine_8Bit[i])));
      i++;
      if (i>255){
        i = 0;
      }}
/*
  Serial.print("value: ");
  Serial.print(value);
  Serial.print("  SineWave: ");
  Serial.println(pgm_read_word(&(DACLookup_FullSine_8Bit[i])));
*/
}

void button(void){
  volatile unsigned long now = millis(); 
  unsigned long prev = 0; unsigned long interval = 200;
    if (now - prev > interval){
      prev = now;
      bool buttonStateplus = digitalRead(plusbuttonPin);
      bool buttonStateminus = digitalRead(minusbuttonPin);
      if (buttonStateplus == LOW){
        value++; 
        digitalWrite(LEDPin1,HIGH);
      }
      else if (buttonStateminus == LOW){
        value--;
        digitalWrite(LEDPin2,HIGH);
      }
      else {
        digitalWrite(LEDPin1,LOW);
        digitalWrite(LEDPin2,LOW);
      }
  }
   if (value > 100){
     value = 30;
     }
     if (value < 0){
       value = 30;
       }
}

SineWaveTable.h

const PROGMEM uint16_t DACLookup_FullSine_8Bit [256] =
{
  2048, 2098, 2148, 2198, 2248, 2298, 2348, 2398,
  2447, 2496, 2545, 2594, 2642, 2690, 2737, 2784,
  2831, 2877, 2923, 2968, 3013, 3057, 3100, 3143,
  3185, 3226, 3267, 3307, 3346, 3385, 3423, 3459,
  3495, 3530, 3565, 3598, 3630, 3662, 3692, 3722,
  3750, 3777, 3804, 3829, 3853, 3876, 3898, 3919,
  3939, 3958, 3975, 3992, 4007, 4021, 4034, 4045,
  4056, 4065, 4073, 4080, 4085, 4089, 4093, 4094,
  4095, 4094, 4093, 4089, 4085, 4080, 4073, 4065,
  4056, 4045, 4034, 4021, 4007, 3992, 3975, 3958,
  3939, 3919, 3898, 3876, 3853, 3829, 3804, 3777,
  3750, 3722, 3692, 3662, 3630, 3598, 3565, 3530,
  3495, 3459, 3423, 3385, 3346, 3307, 3267, 3226,
  3185, 3143, 3100, 3057, 3013, 2968, 2923, 2877,
  2831, 2784, 2737, 2690, 2642, 2594, 2545, 2496,
  2447, 2398, 2348, 2298, 2248, 2198, 2148, 2098,
  2048, 1997, 1947, 1897, 1847, 1797, 1747, 1697,
  1648, 1599, 1550, 1501, 1453, 1405, 1358, 1311,
  1264, 1218, 1172, 1127, 1082, 1038,  995,  952,
   910,  869,  828,  788,  749,  710,  672,  636,
   600,  565,  530,  497,  465,  433,  403,  373,
   345,  318,  291,  266,  242,  219,  197,  176,
   156,  137,  120,  103,   88,   74,   61,   50,
    39,   30,   22,   15,   10,    6,    2,    1,
     0,    1,    2,    6,   10,   15,   22,   30,
    39,   50,   61,   74,   88,  103,  120,  137,
   156,  176,  197,  219,  242,  266,  291,  318,
   345,  373,  403,  433,  465,  497,  530,  565,
   600,  636,  672,  710,  749,  788,  828,  869,
   910,  952,  995, 1038, 1082, 1127, 1172, 1218,
  1264, 1311, 1358, 1405, 1453, 1501, 1550, 1599,
  1648, 1697, 1747, 1797, 1847, 1897, 1947, 1997
};
  unsigned long prevmicros;

Your mistake is that prevmicros is neither global nor static nor initialized so it gets a garbage value from the stack each time loop() is called. Didn't you get a warning messages about the variable being used uninitialized? If not, TURN UP WARNINGS TO ALL.

Change it to   static unsigned long prevmicros = 0;

You will get better timing if you use:

      prevmicros += value;

instead of:

      prevmicros = nowmicros;

That way a delay of a microsecond or more won't cause all future samples to be delayed.

Note: Since 'value' is between 30 and 100 you should probably make it a 'byte' instead of 'int'.

I have fixed it!!
I had written wrong code on the button function's in addition your point.
The problem was in the function of "void button()".

I have modified

value = value ++ ; 

value = value -- ;

to

value++;

value--;

Thank you for the advice.

the completed code here.

#include <avr/pgmspace.h>
#include <MCP4922.h>
#include <SPI.h>
#include "SineWaveTable.h"
//MCP4922 DAC(51,52,53,5);    // (MOSI,SCK,CS,LDAC) define Connections for MEGA_board, 
//MCP4922 DAC(11,13,10,5);    // (MOSI,SCK,CS,LDAC) define Connections for UNO_board, 
MCP4922 DAC(11,13,10,5);  //  (MOSI,SCK,CS,LDAC) define Connections for pro_mini_board, 
static unsigned long value = 30;
const int plusbuttonPin = 2; const int minusbuttonPin = 3;
const int LEDPin1 = 9; const int LEDPin2 = 8; int i = 0;


void setup(void)
{
  //Serial.begin(9600);
  SPI.begin();
  pinMode(plusbuttonPin,INPUT_PULLUP);
  pinMode(minusbuttonPin,INPUT_PULLUP);
  pinMode(LEDPin1,OUTPUT);
  pinMode(LEDPin2,OUTPUT);
  attachInterrupt(digitalPinToInterrupt(plusbuttonPin),button,CHANGE);
  attachInterrupt(digitalPinToInterrupt(minusbuttonPin),button,CHANGE);
}

void loop(void)
{
  unsigned long nowmicros = micros();  static unsigned long prevmicros = 0;
  if (nowmicros - prevmicros > value){
      prevmicros += value;
      DAC.Set(pgm_read_word(&(DACLookup_FullSine_7Bit[i])),4095- pgm_read_word(&(DACLookup_FullSine_7Bit[i])));
      i++;
      if (i>127){
        i = 0;
//    delayMicroseconds(value);
//    Serial.print (analogRead(A2)); Serial.print("  "); Serial.println (analogRead(A3));
     }}/*
  Serial.print("value: ");
  Serial.print(value);
  Serial.print("  SineWave: ");
  Serial.println(pgm_read_word(&(DACLookup_FullSine_8Bit[i])));
*/
}

void button(void){
/*  volatile unsigned long now = millis(); 
  unsigned long prev = 0; unsigned long interval = 20;
    if (now - prev > interval){
      prev = now; */
      bool buttonStateplus = digitalRead(plusbuttonPin);
      bool buttonStateminus = digitalRead(minusbuttonPin);
      if (buttonStateplus == LOW){
        value++ ; 
        digitalWrite(LEDPin1,HIGH);
        delay(20);
      }
      else if (buttonStateminus == LOW){
        value-- ;
        digitalWrite(LEDPin2,HIGH);
        delay(20);
      }
      else {
        digitalWrite(LEDPin1,LOW);
        digitalWrite(LEDPin2,LOW);
      }
  //}
   if (value > 500){
     value = 500;
     }
     if (value < 1){
       value = 1;
       }
}

SineWaveTable.h

const PROGMEM uint16_t DACLookup_FullSine_7Bit[128] =
{
  2048, 2148, 2248, 2348, 2447, 2545, 2642, 2737,
  2831, 2923, 3013, 3100, 3185, 3267, 3346, 3423,
  3495, 3565, 3630, 3692, 3750, 3804, 3853, 3898,
  3939, 3975, 4007, 4034, 4056, 4073, 4085, 4093,
  4095, 4093, 4085, 4073, 4056, 4034, 4007, 3975,
  3939, 3898, 3853, 3804, 3750, 3692, 3630, 3565,
  3495, 3423, 3346, 3267, 3185, 3100, 3013, 2923,
  2831, 2737, 2642, 2545, 2447, 2348, 2248, 2148,
  2048, 1947, 1847, 1747, 1648, 1550, 1453, 1358,
  1264, 1172, 1082,  995,  910,  828,  749,  672,
   600,  530,  465,  403,  345,  291,  242,  197,
   156,  120,   88,   61,   39,   22,   10,    2,
     0,    2,   10,   22,   39,   61,   88,  120,
   156,  197,  242,  291,  345,  403,  465,  530,
   600,  672,  749,  828,  910,  995, 1082, 1172,
  1264, 1358, 1453, 1550, 1648, 1747, 1847, 1947
};

const PROGMEM uint16_t DACLookup_FullSine_6Bit[64] =
{
  2048, 2248, 2447, 2642, 2831, 3013, 3185, 3346,
  3495, 3630, 3750, 3853, 3939, 4007, 4056, 4085,
  4095, 4085, 4056, 4007, 3939, 3853, 3750, 3630,
  3495, 3346, 3185, 3013, 2831, 2642, 2447, 2248,
  2048, 1847, 1648, 1453, 1264, 1082,  910,  749,
   600,  465,  345,  242,  156,   88,   39,   10,
     0,   10,   39,   88,  156,  242,  345,  465,
   600,  749,  910, 1082, 1264, 1453, 1648, 1847
};

const PROGMEM uint16_t DACLookup_FullSine_5Bit[32] =
{
  2048, 2447, 2831, 3185, 3495, 3750, 3939, 4056,
  4095, 4056, 3939, 3750, 3495, 3185, 2831, 2447,
  2048, 1648, 1264,  910,  600,  345,  156,   39,
     0,   39,  156,  345,  600,  910, 1264, 1648
};

[/b]