Using a potentiometer to control two variables separately, selected via a switch

I am currently building a synthesizer using the MOZZI library, for controls it has 3 potentiometers and a push button switch. I would like to program it so that when the button is held the potentiometers will change one set of 3 variables and when the button is released they will control a different set of 3 variables. Effectively each potentiometer will control 2 different variables, depending on what state the switch is in. The issue I am having using conditional statements is that when the potentiometer is controlling one variable, the other variable is flickering randomly and vice versa. When not being controlled by the potentiometer I would like each variable to hold its last value. Have I made a mistake in my code or should I be approaching this in a different way? I have included the control portion of my sketch below where I am trying to have INPUT_PIN1 control the variable 'pitch' when the switch is up and 'cutoff' when the switch is depressed.

void updateControl(){
  
   //assign variables to analog pin
  int detune = mozziAnalogRead(INPUT_PIN0);
  int vol = mozziAnalogRead(INPUT_PIN2);
  int pitch;
  int cutoff;

  if(digitalRead(8) == LOW) { // here is where the switch selects which value is controlled by the potentiometer
    pitch = mozziAnalogRead(INPUT_PIN1);
  }
  else {
    cutoff = mozziAnalogRead(INPUT_PIN1);
  }

  
  pitch = map(pitch, 0, 1023, 65, 262); // map the pot range (0-1023 to a new set of values)
  cutoff = map(cutoff, 0, 1023, 10, 255);
  vol = map(vol, 0, 1023, 10, 255);
  detune = map(detune, 0, 1023, 0, 100);
  detune = (detune*0.05);
  
  lpf.setCutoffFreq(cutoff);
  aSaw.setFreq(pitch); //assign variables to oscilator parameters
  bSaw.setFreq(pitch * detune);
  aSin.setFreq((pitch)/2);
}

You might have to ask http://snippets-r-us.com/ :wink: For example, wtf is mozziAnalogRead()?

But one problem I see is you unconditionally assign cutoff and pitch a value based on itself. But depending on pin 8 (why doesn't it have a variable name?) you only initialize one of the two :wink:

PS Heard of arrays? Real timesaver :wink:

From what you showed, pitch should remain constant while cutoff's being changed, and vice versa.

How do you know they're changing?- do you have Serial.print()s in there somewhere?

How is pin 8 wired?

LarryD:
How is pin 8 wired?

Pin 8 is wired to one terminal of the switch and the other terminal is wired to ground

ardy_guy:
From what you showed, pitch should remain constant while cutoff's being changed, and vice versa.

How do you know they're changing?- do you have Serial.print()s in there somewhere?

I know they are changing because I can hear it in the audio output, I havne't quite got my head around using the serial feed to debug stuff but I'll have a look now and see if I can go about adding some Serial.print()'s for the variable values

septillion:
You might have to ask http://snippets-r-us.com/ :wink: For example, wtf is mozziAnalogRead()?

But one problem I see is you unconditionally assign cutoff and pitch a value based on itself. But depending on pin 8 (why doesn't it have a variable name?) you only initialize one of the two :wink:

PS Heard of arrays? Real timesaver :wink:

Sorry I thought this might be easier than posting all the code but I'll stick the whole sketch underneath if that's better. Everything I know so far is pretty cobbled together so I'm sorry if it's a bit ugly.

//#include <ADC.h>  // Teensy 3.1 uncomment this line and install http://github.com/pedvide/ADC
#include <MozziGuts.h>
#include <Oscil.h> // oscillator template
#include <tables/saw2048_int8.h> // saw table for oscillator
#include <tables/sin2048_int8.h> // saw table for oscillator
#include <LowPassFilter.h>
//#include <tables/saw1024_int8.h> // saw table for oscillator


// use: Oscil <table_size, update_rate> oscilName (wavetable), look in .h file of table #included above
Oscil <SAW2048_NUM_CELLS, AUDIO_RATE> aSaw(SAW2048_DATA);
Oscil <SAW2048_NUM_CELLS, AUDIO_RATE> bSaw(SAW2048_DATA);
Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin(SIN2048_DATA);

//LowPassFilter lpf; //for implementing my LPF

// use #define for CONTROL_RATE, not a constant
#define CONTROL_RATE 64 // powers of 2 please

const char INPUT_PIN0 = 0;
const char INPUT_PIN1 = 1;
const char INPUT_PIN2 = 2;
const char BUTTON_PIN1 = 12;

byte pitch;
byte vol;
byte detune;

LowPassFilter lpf;

void setup(){
  Serial.begin(115200);
  startMozzi(CONTROL_RATE); // set a control rate of 64 (powers of 2 please)
  lpf.setResonance(200);
  pinMode(8, INPUT_PULLUP);
  
}


void updateControl(){
  
   //assign variables to analog pin
  int detune = mozziAnalogRead(INPUT_PIN0);
  int vol = mozziAnalogRead(INPUT_PIN2);
  int pitch;
  int cutoff;

  if(digitalRead(8) == LOW) {
    pitch = mozziAnalogRead(INPUT_PIN1);
  }
  else {
    cutoff = mozziAnalogRead(INPUT_PIN1);
  }

  
  pitch = map(pitch, 0, 1023, 65, 262); // map the pot range (0-1023 to a new set of values)
  cutoff = map(cutoff, 0, 1023, 10, 255);
  vol = map(vol, 0, 1023, 10, 255);
  detune = map(detune, 0, 1023, 0, 100);
  detune = (detune*0.05);
  
  lpf.setCutoffFreq(cutoff);
  aSaw.setFreq(pitch); //assign variables to oscilator parameters
  bSaw.setFreq(pitch * detune);
  aSin.setFreq((pitch)/2);
}


int updateAudio(){
  

  
    return (int)lpf.next(((
      ((int)aSin.next()) 
      + 
      ((int)aSaw.next() * 0.5)
      +
      ((int)bSaw.next() * 0.5)
      ) * 0.1));     // vol);
}


void loop(){
  audioHook(); // required here
}

But uhm, I gave you the answer already :wink: Bit hidden, I know. But let me rephrase it:

You assign the other variable (the one you don't want to change) unconditionally with a map() of a un-ininitialized variable (aka, you have NO idea what the value is).

Move the map and assignment into the condition and all is fine :slight_smile: