Variable not changing

Hello all,

I'm building a MIDI controller, based on the instructable found here: http://www.instructables.com/id/Custom-Arduino-MIDI-Controller/#step1

I'm using a Yun(no need to, just happened to have one around). I've made small modifications to the code as I'm using an LCD and also need to have a switch in order to change the midi address of the rotary encoders.

Using an if...else statement, it's fairly easy to change the LCD text, but for some reason the midi address doesn't, or to be more exact, it seems to use a default address, disregarding the 2 I give in the if and else statements.

Forgive the noobie question and the bad explanation, this is my first forray in programming and try to make sense of it all.

The code:

/*
  Written by tttapa, 21/08/2015
  https://github.com/tttapa/MIDI_controller
*/


#include <MIDI_controller.h>

#include <LiquidCrystal.h>

#define VELOCITY          0b01111111  // The velocity of the buttons (0b01111111 = 127 = 100%)
#define LATCHTIME         100         // How long a note will be held on, in DigitalLatch mode (in milliseconds).

#define SPEED_MULTIPLY    1           // If the jog wheels or other encoders are too slow in your software, increase this value 
// (it will be multiplied with the actual speed of the encoder, as the name implies.) Default is 1.
#define PULSES_PER_STEP   4           // This is the number of pulses the encoder outputs when you turn it one step (or click) further. Use 4 for a normal rotary encoder, and 1 for a jogwheel.
// If you set it to 1, this uses the maximum resolution. If it is set to 4, one message will be sent per click of the encoder. 1 click matches 1 unit in the software. This is more logical for most usages (except jogwheels).

#define ANALOG_AVERAGE    8           // Use the average of 8 samples to get smooth transitions and prevent noise

//_____________________________________________________________________________________________________________________________________________________________________________________________


// initialize the library with the numbers of the interface pins for LCD
LiquidCrystal lcd(18, 19, 20, 21, 22, 23);

int buttonPin1= 2;    // change mode button

int Rot;


RotaryEncoder enc1(12,13,Rot, 1, SPEED_MULTIPLY, NORMAL_ENCODER, POS1_NEG127); // Create a new instance of class 'RotaryEncoder' called enc1, on pins 1 and 0, controller number 0x2F, on MIDI channel 1, at normal speed, using a normal encoder (4 pulses per click/step), using the POS1_NEG127 sign option


//_____________________________________________________________________________________________________________________________________________________________________________________________

void setup()
{

  // set up the LCD's number of columns and rows: 
  lcd.begin(20, 2);
  // Print a message to the LCD.
  lcd.print("    Test");
  delay(2000);  //displays for 5 secs
  lcd.clear();  //clears display

  pinMode(buttonPin1, INPUT);  //assigns button pin that switches modes

  digitalWrite(buttonPin1, HIGH);  //sets internal pulldown resistor 

  
  USBMidiController.setDelay(15);  // wait 15 ms after each message not to flood the connection

  delay(1000);         // Wait a second...
}

//_____________________________________________________________________________________________________________________________________________________________________________________________

void loop() // Refresh all inputs
{

  enc1.refresh();

  int buttonState1 = digitalRead(buttonPin1); 

  if(buttonState1== HIGH){  //button not pressed
    Rot= 0x2F; 
    lcd.setCursor(0,0);  //position cursor
    lcd.print("HIGH"); 
    
   }
   
   else if (buttonState1== LOW){   //button pressed
    Rot= 0x0C;
    lcd.setCursor(0,0);   //position cursor
    lcd.print("LOW "); 
    
   }

  
}

Normally, the rotary syntax is this:

RotaryEncoder enc1(12,13,0x2F, 1, SPEED_MULTIPLY, NORMAL_ENCODER, POS1_NEG127);

I removed the address, and placed a variable (Rot) I declared above. Any idea why I cannot change it in this way? (I can give it a different address in there, say 0x2C or whatever, just not with a variable that gets its value elsewhere)

Thanks!

RotaryEncoder enc1(12,13,Rot, 1, SPEED_MULTIPLY, NORMAL_ENCODER, POS1_NEG127); // Create a new instance of class 'RotaryEncoder' called enc1, on pins 1 and 0, controller number 0x2F, on MIDI channel 1, at normal speed, using a normal encoder (4 pulses per click/step), using the POS1_NEG127 sign option

If you change the value of Rot it will not change the value used by the object as it has already been created.

You may want to read from one instance of the RotaryEncoder class when the buttonPin1 pin is HIGH, and a different one when it is LOW. It seems extremely unlikely that you want to (or can) change the address of the rotary encoder when it has already been defined.

Thanks for both your answers.

Trying and failing to find information about instances(failing to understand anyway), I did the following experiment and it worked:

/*
  Written by tttapa, 21/08/2015
  https://github.com/tttapa/MIDI_controller
*/


#include <MIDI_controller.h>

#include <LiquidCrystal.h>

#define VELOCITY          0b01111111  // The velocity of the buttons (0b01111111 = 127 = 100%)
#define LATCHTIME         100         // How long a note will be held on, in DigitalLatch mode (in milliseconds).

#define SPEED_MULTIPLY    1           // If the jog wheels or other encoders are too slow in your software, increase this value 
// (it will be multiplied with the actual speed of the encoder, as the name implies.) Default is 1.
#define PULSES_PER_STEP   4           // This is the number of pulses the encoder outputs when you turn it one step (or click) further. Use 4 for a normal rotary encoder, and 1 for a jogwheel.
// If you set it to 1, this uses the maximum resolution. If it is set to 4, one message will be sent per click of the encoder. 1 click matches 1 unit in the software. This is more logical for most usages (except jogwheels).

#define ANALOG_AVERAGE    8           // Use the average of 8 samples to get smooth transitions and prevent noise

//_____________________________________________________________________________________________________________________________________________________________________________________________


// initialize the library with the numbers of the interface pins for LCD
LiquidCrystal lcd(18, 19, 20, 21, 22, 23);

int buttonPin1= 2;    // change mode button

int Rot;


RotaryEncoder enc1(12,13,0x0E, 1, SPEED_MULTIPLY, NORMAL_ENCODER, POS1_NEG127); // Create a new instance of class 'RotaryEncoder' called enc1, on pins 1 and 0, controller number 0x2F, on MIDI channel 1, at normal speed, using a normal encoder (4 pulses per click/step), using the POS1_NEG127 sign option

RotaryEncoder enc2(12,13,0x0C, 1, SPEED_MULTIPLY, NORMAL_ENCODER, POS1_NEG127); // Create a new instance of class 'RotaryEncoder' called enc1, on pins 1 and 0, controller number 0x2F, on MIDI channel 1, at normal speed, using a normal encoder (4 pulses per click/step), using the POS1_NEG127 sign option


//_____________________________________________________________________________________________________________________________________________________________________________________________

void setup()
{

  // set up the LCD's number of columns and rows: 
  lcd.begin(20, 2);
  // Print a message to the LCD.
  lcd.print("    Test");
  delay(2000);  //displays for 5 secs
  lcd.clear();  //clears display

  pinMode(buttonPin1, INPUT);  //assigns button pin that switches modes

  digitalWrite(buttonPin1, HIGH);  //sets internal pulldown resistor 

  
  USBMidiController.setDelay(15);  // wait 15 ms after each message not to flood the connection

  delay(1000);         // Wait a second...
}

//_____________________________________________________________________________________________________________________________________________________________________________________________

void loop() // Refresh all inputs
{

  

  int buttonState1 = digitalRead(buttonPin1); 

  if(buttonState1== HIGH){  //button not pressed
    enc1.refresh(); 
    lcd.setCursor(0,0);  //position cursor
    lcd.print("HIGH"); 
    
   }
   
   else if (buttonState1== LOW){   //button pressed
    enc2.refresh();
    lcd.setCursor(0,0);   //position cursor
    lcd.print("LOW "); 
    
   }

  
}

Basically I created two rotary encoders, enc1 and enc2 with the same attributes and pins but different addresses and then I refresh only one of the two depending on the button state. It seems to work just fine. Is it a valid method?

Thanks again.

Note the comment on the line?

RotaryEncoder enc1(12,13,Rot, 1, SPEED_MULTIPLY, NORMAL_ENCODER, POS1_NEG127); // Create a new instance of class 'RotaryEncoder' called enc1, on pins 1 and 0, controller number 0x2F, on MIDI channel 1, at normal speed, using a normal encoder (4 pulses per click/step), using the POS1_NEG127 sign option

enc1 is an instance of RotaryEncoder

The program won't change but you might like to change the comments in your code to match the commands. This might not matter now but when you look at the code in the future you may like a reminder of how and why it works.

In particular, the second of these two comments is wrong about the input being pulled down. It is actually pulled up.

pinMode(buttonPin1, INPUT);  //assigns button pin that switches modes
  digitalWrite(buttonPin1, HIGH);  //sets internal pulldown resistor

and they could be replaced with the much more explicit

pinMode(buttonPin1, INPUT_PULLUP);  //note that the input is pulled up, not down

,

Is it a valid method?

Yes. That is what I was suggesting that you do.