Ramp Controls for WaveTable Sine Wave Rate Control

Hello,

I am working on a project which uses a wavetable sine wave to control an LED, so it will fade on and off at a rate set to a master rate control potentiometer which i’ve achieved this pretty well on the example I've attached.

But i want to add a Ramp control with a secondary pot, with a push button which will ramp up or down the rate to the secondary pot value, which i cant seem to figure out the code too yet.

What the ramp control will do is there are 3 potentiometers and a momentary push button to activate it:
1: (secondary rate pot) Ramp Rate End Point - the set rate id like the sine wave rate to End on when you hold down the ramp button.
2: Attack - Synth style attack envelope, slow the time it takes from the master rate pot value to make it to the ramp rate end point value.
3: Release - Synth style release envelope, slow the time it takes from the ramp rate end point value back to the master rate pot value.
4: momentary push button - holding it down will activate the ramp controls above, releasing will turn it back to only using the master rate potentiometer.

The ramp code ive got on my code below is only there to test the attack and release settings, it doesnt actually effect the rate which id like it too.
If anyone can help me figure out this code id be very grateful!

const byte sineWaveTable[256] = {
  //sine wave
  128,131,134,137,140,143,146,149,
  152,155,158,162,165,167,170,173,
  176,179,182,185,188,190,193,196,
  198,201,203,206,208,211,213,215,
  218,220,222,224,226,228,230,232,
  234,235,237,238,240,241,243,244,
  245,246,248,249,250,250,251,252,
  253,253,254,254,254,255,255,255,
  255,255,255,255,254,254,254,253,
  253,252,251,250,250,249,248,246,
  245,244,243,241,240,238,237,235,
  234,232,230,228,226,224,222,220,
  218,215,213,211,208,206,203,201,
  198,196,193,190,188,185,182,179,
  176,173,170,167,165,162,158,155,
  152,149,146,143,140,137,134,131,
  128,124,121,118,115,112,109,106,
  103,100,97,93,90,88,85,82,
  79,76,73,70,67,65,62,59,
  57,54,52,49,47,44,42,40,
  37,35,33,31,29,27,25,23,
  21,20,18,17,15,14,12,11,
  10,9,7,6,5,5,4,3,
  2,2,1,1,1,0,0,0,
  0,0,0,0,1,1,1,2,
  2,3,4,5,5,6,7,9,
  10,11,12,14,15,17,18,20,
  21,23,25,27,29,31,33,35,
  37,40,42,44,47,49,52,54,
  57,59,62,65,67,70,73,76,
  79,82,85,88,90,93,97,100,
  103,106,109,112,115,118,121,124,
  };

const int outputPin = 9;
const int ratePin = A1;
const int rampInPin = 2;
const int rampOutPin = 5;
const int rampAttackPin = A2;
const int rampReleasePin = A6;
const int rampEndPin = A5; //not used in yet but declared

int x = 1;
int i;
int rate;
int rampAttack;
int rampRelease;
int rampVoltage;

unsigned long previousMicros;
unsigned long attackMicros;
unsigned long releaseMicros;

void setup() {

pinMode(outputPin, OUTPUT);
pinMode(ratePin, INPUT);
pinMode(rampInPin, INPUT_PULLUP);
pinMode(rampEndPin, INPUT); //not used in yet but declared
pinMode(rampOutPin, OUTPUT);

}

void loop() {

   unsigned long currentMicros = micros();
   rampAttack = map(analogRead(rampAttackPin), 0, 1023, 0, 10000);
   rampRelease = map(analogRead(rampReleasePin), 0, 1023, 0, 10000);
   rate = map(analogRead(ratePin), 0, 1023, 20000, 100);

//Sine Wave LFO

   if (currentMicros - previousMicros >= rate) {
    previousMicros = currentMicros;

    i++;
    if(i == 256)
    i = 0;
   }

//RAMP (Outputting to an LED to figure out attack and release settings, not affecting Rate yet)

  if (digitalRead(rampInPin) == LOW) {
  if (currentMicros - attackMicros >= rampAttack) {
  attackMicros = currentMicros; 
  rampVoltage = rampVoltage + x;
  if (rampVoltage >= 255) {
    rampVoltage = 255;
  }
  }
  }

  if (digitalRead(rampInPin) == HIGH) {
  if (currentMicros - releaseMicros >= rampRelease) {
  releaseMicros = currentMicros; 
  rampVoltage = rampVoltage - x;
  if (rampVoltage <= 0) {
    rampVoltage = 0;
  }
  }
  }

  analogWrite(rampOutPin, rampVoltage);
  analogWrite(outputPin, (sineWaveTable[i]));

}

Why is it necessary to use a sine wave table?

I see the LED fading up and down.

I do not understand (yet) how the pushbutton works. It will help if you drew kinda like a timing diagram showing what happens when the button is not pressed, then what is different when it is pressed.

And/or you could draw a block diagram if it is possible to describe it using common voltage control synthesizer modules - sounds like an LFO modulating an LFO modulating LED intensity.

If you have to, invent a voltage control synthesis module if it isn't a common one. Just say what it is supposed to do in isolation from the things you hook it up to.

Also, the timing in microseconds and the map values you have used end up yielding between 0 and 10 milliseconds, or 0.01 seconds, which will be hard to see it goes by so fast. Did you mean to aim for 0 to 10 seconds? Or is that time per step?

a7