I am working on building a 5 channel "passive preamp" ie volume control that will accept data from 5 push button rotary encoders to control the volume and mute of each channel. I've got code that successfully increments in steps of one between 0 and a set value. I have separate code working with the push button part of the rotary encoder that I would like to use to "mute" the volume of that channel.
//define variables for incrementing and instantiation logic.
int vol1 = 0; //variable for storing a volume value until I flesh out the volume control portion of the electronics
char name1[ ] = "one "; //temporary, for differentiating channels during testing
//variables to keep track of buttonpress state
int mute1Store = 0; //storage for volume when muted
int mute1Counter = 0; //variable to count button presses
int mute1State = 0; // current state of the button
int mute1Last = 0; // previous state of the button
Rotary r = Rotary(2, 3);
void setup() {
Serial.begin(9600);
//initialize mutepin as input
pinMode(mutePin1, INPUT);
}
void loop() {
//read mutePin1 state
mute1State = digitalRead(mutePin1);
//check for button press
if(mute1State != mute1Last) {
// if the state has changed, increment the counter
if (mute1State == HIGH){
// if the current state is HIGH(button was pressed), the counter does not indicate a muted state
//and the volume is not 0 then increment the counter
mute1Counter++;
}
}
mute1Last = mute1State;
//check if mute button has been pressed, if it has been pressed
//and the volume is not 0, store the volume and set the volume to 0
if ((mute1Counter % 2 == 0) && (vol1 != 0) && (mute1Counter != 0)) {
mute1Store = vol1;
vol1 = 0;
Serial.print(name1);
Serial.println(vol1);
} else if((mute1Counter % 2 != 0) && (mute1State != mute1Last)){
vol1 = mute1Store;
Serial.println(vol1);
}
// code to increment counter based on rotary encoder input (CW or CCW) and constrain counter to 0 through 20
unsigned char result = r.process();
if (result == DIR_CCW){
if(vol1 < 20){
vol1 = vol1 + 1;
}
Serial.print(name1);
Serial.println(vol1);
}
else if(result == DIR_CW) {
if(vol1 > 0){
vol1 = vol1 - 1;
}
Serial.print(name1);
Serial.println(vol1);
}
}
}
Currently the rotary encoder works, and pushing the button once sets vol1 to 0, but pushing it again does not set it back to the previous value. Any suggestions for that problem, or ways to do what I'm doing better would be greatly appreciated.