So I am implementing a synth envelop on an ATMega328p and am using a button reference in the envelope function. Each time a different note-button is pressed, the corresponding button should be referenced to the envelope function for releasing.
The problem is that, everything works as intended if I change the starting value of that reference in its declaration, but it does not change through assigning a value to it in my main code.
uint8_t envelopeStage[2] = {4, 4}; //Tracks the current stage of the envelope bool& envButton = buttonState[0]; //The button envelope tracks for releasing uint16_t millisecsEnvelope[2] = {0, 0}; //Timer for the envelope incremented in ms void buttonAction(volatile const uint8_t* PINX, const uint8_t PINXno) { if (PINX == buttonPort[0] && PINXno == buttonPin[0]) { UDR0 = 'A'; envButton = buttonState[0]; //Does not work!!!! play(); } else if (PINX == buttonPort[1] && PINXno == buttonPin[1]) { UDR0 = 'B'; envButton = buttonState[1]; //Does not work!!!! play(); } else if (PINX == buttonPort[2] && PINXno == buttonPin[2]) { UDR0 = 'C'; envButton = buttonState[2]; //Does not work!!!! play(); } else if (PINX == buttonPort[3] && PINXno == buttonPin[3]) { UDR0 = 'D'; envButton = buttonState[3]; //Does not work!!!! play(); } } void doEnvelope(uint8_t oscillator, bool& buttonState) { waitForSample(); switch (envelopeStage[oscillator]) { case 0: if (buttonState && ((millisecsEnvelope[oscillator] << 4) / attack[oscillator] < 255)) { volume[oscillator] = (millisecsEnvelope[oscillator] << 4) / attack[oscillator]; // millisecsEnvelope[oscillator] / (attack/16) } else if (buttonState) { volume[oscillator] = 255; ++envelopeStage[oscillator]; millisecsEnvelope[oscillator] = 0; } else { ++envelopeStage[oscillator]; millisecsEnvelope[oscillator] = 0; } break; case 1: if (buttonState && volume[oscillator] > sustain[oscillator]) { volume[oscillator] = 255 - ((millisecsEnvelope[oscillator] * 16) / decay[oscillator]) ; // 255 - millisecsEnvelope[oscillator] / (decay/16); } else if (buttonState) { volume[oscillator] = sustain[oscillator]; ++envelopeStage[oscillator]; millisecsEnvelope[oscillator] = 0; } else { ++envelopeStage[oscillator]; millisecsEnvelope[oscillator] = 0; } break; case 2: if (!buttonState || (sustain[oscillator] == 0)) { ++envelopeStage[oscillator]; millisecsEnvelope[oscillator] = 0; } break; case 3: if (volume[oscillator] <= 1) { volume[oscillator] = 0; millisecsEnvelope[oscillator] = 0; ++envelopeStage[oscillator]; notePlaying[oscillator] = 0; } else if (release[oscillator] > 253) { ++envelopeStage[oscillator]; break; } else { if (!volumeRelease[oscillator]) volumeRelease[oscillator] = volume[oscillator]; if (!releaseMode[oscillator]) if (volumeRelease[oscillator] > ((millisecsEnvelope[oscillator] << 4) / release[oscillator])) volume[oscillator] = volumeRelease[oscillator] - ((millisecsEnvelope[oscillator] << 4) / release[oscillator]); //Linear release else volume[oscillator] = 0; else volume[oscillator] = (volumeRelease[oscillator] * release[oscillator]) / ((millisecsEnvelope[oscillator] >> 1) + release[oscillator]) - (millisecsEnvelope[oscillator]/1000); //Non linear release } } } int main() { doEnvelope(0, envButton); doEnvelope(1, envButton); }
I know that execution enters each button action because I can see A,B,C and D being outputted from serial for each button. If I change the declaration to:
bool& envButton = buttonState[1]; //The button envelope tracks for releasing
It works as intended. But it seems that the assignments in the buttonAction function doesn't work. I tried making buttonState and envButton volatiles.. it still didn't work. Am I missing somehting?