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!