I've introduced the EEPROM to the code, which works great. However, now that I have the analogRead in there as well, the "potVal" (which will eventually become the MIDI CC value) will 'snap' back to the value located at the EEPROM address when I'm not turning my pot. In the old version of the code above (which doesn't work anymore because i fiddled with it too much), it had the desired behavior of keeping each value located at the EEPROM address in place until the respective pot was turned, and then stayed with the pot until another EEPROM address was read. The difference between the two codes is that in the current version, the action of pressing the button is separate from where the address value is determined, so I'm not sure where to place the 'if' statement, or if that's even something I have to do.
I've been at this for a few hours now trying different things, like trying while statements (nope), placing the analogRead above all of the button actions, trying to do an 'if' statement with things like:
Code:
if(initPotVal != readPotVal[i]){
potVal[i] = initPotVal[i];}
I think the one above did the opposite of the current behavior but worse (only read analogRead, did not read from EEPROM address at all).
Code:
#include <EEPROM.h>
int bankAddr[] = {10, 11, 12};
int buttonState1;
int lastButtonState1;
int buttonState2;
int lastButtonState2;
int BankNumber = 1;
int FirstSelectedPresetWithinBank = 1;
void setup() {
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
Serial.begin(115200);
}
void loop() {
byte potPin[] = {A0, A1, A2};
byte initPot[3];
byte lastInitPot[3];
byte potVal[3];
byte threshold = 2;
lastButtonState1 = buttonState1;
lastButtonState2 = buttonState2;
buttonState1 = buttonState2 = HIGH;
buttonState1 = digitalRead(2);
if (buttonState1 != lastButtonState1) {
if (buttonState1 == LOW) {
FirstSelectedPresetWithinBank = FirstSelectedPresetWithinBank + 3;
if (FirstSelectedPresetWithinBank > 7) FirstSelectedPresetWithinBank = 1;
}
}
buttonState2 = digitalRead(3);
if (buttonState2 != lastButtonState2) {
if (buttonState2 == LOW) {
BankNumber = BankNumber + 1;
if (BankNumber > 3) BankNumber = 1;
FirstSelectedPresetWithinBank = 1;
}
}
for(int i = 0; i < 3; i++){
initPot[i] = analogRead(potPin[i]); // here's all of the pot mess
if (abs(initPot[i] - lastInitPot[i]) > threshold){
lastInitPot[i] = initPot[i];
potVal[i] = initPot[i];
}else{
bankAddr[i] = BankNumber*9 + FirstSelectedPresetWithinBank + i;
potVal[i] = EEPROM.read(bankAddr[i]);
}
Serial.print("ADDRESS: ");
Serial.print(bankAddr[i]);
Serial.println( " ");
Serial.print("potVal: ");
Serial.print(potVal[i]);
Serial.println( " ");
}
Serial.println();
delay(2000);
}