Hi everybody,
I change a midi program value with an encoder and send it when i push the switch, everything works well
But...
when i print this value on LCD screen, the value can be different i explain
before 0 program value write -1 on LCD but send program 127.
after 127 program value write 128 on LCD but send program 0.
I use the same variable for midi send and the screen i'm very confused...
I did it for channel number everything is good.
#include <MIDI.h>
#include <Encoder.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
MIDI_CREATE_DEFAULT_INSTANCE();
const int Up_buttonPinChannel = 2;
const int Down_buttonPinChannel = 3;
Encoder myEnc(5, 6);
// Variables will change:
int ChannelNumber = 1;
int up_buttonStateChannel = 0;
int up_lastButtonStateChannel = 0;
int down_buttonStateChannel = 0;
int down_lastButtonStateChannel = 0;
bool bPressChannel = false;
int oldPosition = 0;
int ProgramValue = 0;
void setup()
{
Serial.begin(31250);
MIDI.begin (MIDI_CHANNEL_OMNI);
pinMode(4, INPUT_PULLUP);
pinMode( Up_buttonPinChannel , INPUT_PULLUP);
pinMode( Down_buttonPinChannel , INPUT_PULLUP);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Channel:");
lcd.setCursor(9,0);
lcd.print(ChannelNumber);
lcd.setCursor(0,1);
lcd.print("Program:");
lcd.setCursor(9,1);
lcd.print(ProgramValue);
}
void loop()
{
checkUp();
checkDown();
if( bPressChannel)
{
bPressChannel = false;
lcd.setCursor(9,0);
lcd.print(" ");
lcd.setCursor(9,0);
lcd.print(ChannelNumber);
}
int newPosition = myEnc.read();
int ProgramValue = newPosition/4;
if (newPosition<0)
{
newPosition==508;
ProgramValue==127 ;
}
if (newPosition>508)
{
newPosition==0;
ProgramValue==0 ;
}
if (newPosition != oldPosition)
{
lcd.setCursor(9,1);
lcd.print(" ");
lcd.setCursor(9,1);
lcd.print(ProgramValue);
oldPosition = newPosition;
}
if(digitalRead(4) == LOW)
{
MIDI.sendProgramChange(ProgramValue,ChannelNumber);
delay(500);
}
}
void checkUp()
{
up_buttonStateChannel = digitalRead(Up_buttonPinChannel);
if (up_buttonStateChannel != up_lastButtonStateChannel)
{
if (up_buttonStateChannel == LOW) {
bPressChannel = true;
// if the current state is HIGH then the button went from off to on:
ChannelNumber++;
if (ChannelNumber>16){ChannelNumber=1;}//passe de 16 Ã 1
// Serial.println("on");
// Serial.print("number of button pushes: ");
// Serial.println(ChannelNumber);
} else {
// if the current state is LOW then the button went from on to off:
//Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
up_lastButtonStateChannel = up_buttonStateChannel;
}
void checkDown()
{
down_buttonStateChannel = digitalRead(Down_buttonPinChannel);
// compare the buttonState to its previous state
if (down_buttonStateChannel != down_lastButtonStateChannel) {
// if the state has changed, increment the counter
if (down_buttonStateChannel == LOW) {
bPressChannel = true;
// if the current state is HIGH then the button went from off to on:
ChannelNumber--;
if (ChannelNumber<1){ChannelNumber=16;}//passe de 1 Ã 16
// Serial.println("on");
//Serial.print("number of button pushes: ");
// Serial.println(ChannelNumber);
} else {
// if the current state is LOW then the button went from on to off:
//Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
down_lastButtonStateChannel = down_buttonStateChannel;
}