3 Pots good, 4 pots fail

I'm building a midi control set, sing an Adafruit Metro Mini (like a Uno). Latest codebase, etc. I have 4 its on the Analog ins, they are all 5k pots. If I have any of the 3 online, they work, but when I run 4, the last one in the chain will not work. I've even moved the analog pins around on the set up side, the issue isn't the pot or the wiring. Is there some sort of accumulated resistance problem?

int currentPot[4] ={0,0,0,0};
int i,n,val,t;
// I have literally moved the order of these around. Last one gets lost every time.
int potPin[4]={A0,A1,A2,A3};
int switchPin=9;
int pots_now=0;
int pastPot[4]={0,0,0,0};

void setup() {
// The midi out board gets set up
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
digitalWrite(3, LOW); // GND 0 Volt supply to opto-coupler
digitalWrite(2, HIGH); // +5 Volt supply to opto-coupler
// Turn some LEDS on
for(n=0; n<2; n++){
pinMode(ledPin[n], OUTPUT);
}

Serial.begin(31250); // set MIDI baud rate
//Serial.begin(9600);
Serial.flush();
//Serial.println("Let's Get this started-----------------------------");

}

void loop() {

// For whatever reason #4 gets lost.......

for(n=0; n<4; n++){
currentPot[n] = analogRead(potPin[n]);
currentPot[n] = analogRead(potPin[n]);
if(pastPot[n] < (currentPot[n]-4) || pastPot[n] > (currentPot[n]+4) ) {

freeMode(n,currentPot[n]);
}
pastPot[n]=currentPot[n];
delay (10);
}
}

/// More code after this.......

Make sure you use the code tags button when you do

Please don’t post code fragments, it just generates questions.

BTW, the array indexing looks wrong, operation should be inside brackets?

if(pastPot[n] < (currentPot[n]-4) || pastPot[n] > (currentPot[n]+4) ) {

Like this?

if(pastPot[n] < (currentPot[n-4]) || pastPot[n] > (currentPot[n+4]) ) {

WattsThat:
Please don’t post code fragments, it just generates questions.

BTW, the array indexing looks wrong, operation should be inside brackets?

No, its hysteresis code - it would perhaps be clearer written as:

  if (abs (pastPot[n] - currentPot[n]) > THRESHOLD) {

There's a glaring error though:

  if(pastPot[n] < (currentPot[n]-4) || pastPot[n] > (currentPot[n]+4) ) {
 
      freeMode(n,currentPot[n]);
    }
     pastPot[n]=currentPot[n];

should be

  if(pastPot[n] < (currentPot[n]-4) || pastPot[n] > (currentPot[n]+4) ) {
 
      freeMode(n,currentPot[n]);
      pastPot[n]=currentPot[n]; 
    }

otherwise it will be speed-sensitive! The update should only happen if a significant change
has happened. I suspect the delay(10) was an attempt to reduce the speed-sensitivity of
always updating the pastPot values.

There's no need to double-read analogRead() here as the source impedance is below 10k.

I think the code as original is OK. "freeMode()" is only called when the if statement is true and pastPot[] is updated for each value of n (@MarkT could be right though).

I am more worried about what happens in "freemode()". The indexing variable "n" is global and can be changed in "freeMode()".

I think

   for (int n=0;  n<4;  n++ )  {

would be saver.

We need a bit more code.

Willem

Here's the entire program. I'm using a repurposed joystick and some knobs to control pitchbend, modulation, cutoff, resonance and LFO speed. It is really odd, I've re-ordered/edited the array of analog ins, pretty sure that any 3 will work, but a 4th one won't.

int Modulation=1;
// Define Moog first in arrays
int Res_grp[2]={21,30};
int Cut_grp[2]={51,29};
int LFO_grp[2]={3,16};

int Resonance,Cutoff,LFO;
int Mchan[2]={0,3};
// #define Moog_LFO 3
// #define DM_Res 30
// #define DM_Cutoff 29
// #define DM_LFO 16
#define Pitchbend 2
// 2 LEDs 
// 1 joystick
// 1 switches - select device
// 2 extra Knobs
 
//int midiByte;
//int MIDIchannel = 1;
int channel;
int status=0;
int statusType;
int statusTest;
int runningStatus;
int Flag;
int runStatusFlag=0;
int realTimeTest;
int count;

int ledPin[2]={10,11}; // select the pin for the LED
int switchState;
int lightState[2]={0,0};
int currentSwitch;
int currentPot[4] ={0,0,0,0};
int i,n,val,t;
int potPin[4]={A0,A1,A2,A3};
int switchPin=9;
int pots_now=0;
int pastPot[4]={0,0,0,0};

byte midiByte;
byte MIDIchannel;
byte x;

void setup() {

  pinMode(2, OUTPUT);     
  pinMode(3, OUTPUT);  
  digitalWrite(3, LOW); // GND 0 Volt supply to opto-coupler
  digitalWrite(2, HIGH); // +5 Volt supply to opto-coupler
    

  //pinMode(4, INPUT); // Set Inputs for 4 way DIP Switch
  //pinMode(5, INPUT); 
  //pinMode(6, INPUT); 
  //pinMode(7, INPUT); 
  //digitalWrite(4, HIGH); // Set inputs Pull-up resistors High
  //digitalWrite(5, HIGH);
  //digitalWrite(6, HIGH);
  //digitalWrite(7, HIGH);
  pinMode(switchPin, INPUT_PULLUP);
    
  // pinMode(switchPin, INPUT);
      for(n=0; n<2; n++){
     pinMode(ledPin[n], OUTPUT);
    }

 
  

 Serial.begin(31250);   // set MIDI baud rate
 //Serial.begin(9600);
  Serial.flush();

  //Serial.println("Let's Get this started-----------------------------");
}

void loop() {
  
   // Read 4-way DIP switch
  //  MIDIchannel=digitalRead(4) + (digitalRead(5)<<1) + (digitalRead(6)<<2) + (digitalRead(7)<<3);
 // midiThru();

  
  currentSwitch = digitalRead(switchPin);
      if( currentSwitch == LOW && switchState == LOW ){
      setMidi(1);
//    LED = Yellow  switchState=currentSwitch;
      } 
      if( currentSwitch == LOW && switchState == HIGH ){      
      setMidi(1);
      
      }
    if( currentSwitch == HIGH && switchState == LOW ){      
        setMidi(2);        
    }
    switchState=currentSwitch;            

    
    for(n=0; n<5; n++){
    currentPot[n] = analogRead(potPin[n]);
    currentPot[n] = analogRead(potPin[n]);
  if(pastPot[n] < (currentPot[n]-4) || pastPot[n] > (currentPot[n]+4) ) {
  
      freeMode(n,currentPot[n]);
    }
     pastPot[n]=currentPot[n];  
     delay (10);    
    }
 
  
  
}

//++++++++++++++++++++++Functions++++++++++++++++++++++++++++++++++++++++
void midiThru(){
   if (Serial.available() > 0) { 
          midiByte = Serial.read();
       }

       }


// Send a MIDI control message.  
  void control(byte channel, byte control, byte range) {
  midiMsg( (0xB0 | channel), control, range);
}

void pitchBend(byte range){
  int PB =map(range, 81,968,-8000, 8000);  
   byte change = 0x2000+PB;
   byte low = change &0x7F; 
   byte high = (change >>7)&0x7F;
   midiMsg(0xE0,low,high);

  
}

// Send a general MIDI message
void midiMsg(byte cmd, byte data1, byte data2) {
  Serial.write(cmd);
  Serial.write(data1);
  Serial.write(data2);

  //Serial.println(cmd);
  //Serial.println(data1);
  //Serial.println(data2);


}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

void setMidi(byte select) {
    int t=0;
    switch (select) {
  case 1:
  // release
    
      LFO=LFO_grp[t];
      Resonance =Res_grp[t];
      Cutoff=Cut_grp[t];
      digitalWrite(ledPin[0], HIGH);
      digitalWrite(ledPin[1], LOW);
      MIDIchannel=Mchan[t];
      break;
      
 case 2:
        // release
      t =1;
      LFO=LFO_grp[t];
      Resonance =Res_grp[t];
      Cutoff=Cut_grp[t];
      digitalWrite(ledPin[1], HIGH);
      digitalWrite(ledPin[0], LOW);
      MIDIchannel=Mchan[t];
      break; 
  }
}


void freeMode(byte select, int range) { 
switch (select) {
  case 0:
   //The Pitchbend (X) axis. 
    pitchBend(range);
    break; 

     case 1:
    //Modulation Case
    range = range/2;     
      if (range > 300){
        int MOD = map(range, 300, 511, 0, 127);
        control(MIDIchannel,Modulation,  MOD);
      
      }
      else {
              int RES = map(range, 299, 83, 0, 127);
                  control(MIDIchannel,Resonance, RES);

      }
      
      break;  

       case 2:
       // LFO
       int LFOSig = map(range, 1023, 0, 0, 127);
       control(MIDIchannel,LFO, LFOSig);

       break;  

      case 3:
      //Cutoff
      int CutSig = map(range, 1023, 0, 0, 127);
       control(MIDIchannel,Cutoff, CutSig);

       break;  



  }                     
}

Bump

cmackg:
Bump

That's going to hurt! :astonished:

This doesn't look good:

  for (n = 0; n < 5; n++)

Should be n<4

You’ve said the forth pot doesn’t work, doesn’t matter where it is physically.

Okay, just what does that mean? The value is zero? 1023? Something in between? And exactly which value are you referring to? currentPot? pastPot?

It’s difficult to help when we don’t have a detailed description of the exact problem.

A schematic (hand drawn is enough) may be also helpful. What is "chain of pots"?

In steps remove everything unrelated from your sketch. At some point it will start working and you will know which part of the code is bugged.

WattsThat:
You’ve said the forth pot doesn’t work, doesn’t matter where it is physically.

Okay, just what does that mean? The value is zero? 1023? Something in between? And exactly which value are you referring to? currentPot? pastPot?

very much this.

After a few more hours..... The value for the pot is captured, but I'm failing to make it through the case/select tree in void freeMode. If I comment out the actions for case 2, then I can get to case 3. If I uncomment the case 2 actions, then no case 3 actions. And this can be the case even IF there's no motion on any of the other pots. All of the cases (except case 0 for pitchbend) are using the same midi control send command. I guess something is resetting or changing the vale of my case/select variable, but for the life of me I don't see it.

If you’re dealing with the usual analog input values, your map() calls are incorrect.

int LFOSig = map(range, 1023, 0, 0, 127);

Should be:

int LFOSig = map(range, 0, 1023, 127, 0);

You cannot invert using the input range, only the output. See: map() - Arduino Reference

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.