LCD screen with different value

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 :slight_smile:

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;

}

Oops

perfect thanks a lot
before 0 it's 127
after 127 it's 0
but no more, value stay and doesn't continue.
the program change follows the screen this time

I'm sorry, I have no idea what any of that means.

I will try to explain better

I want the value after 127 be 0 then 1,2,3,4....
But it stay to 0
The same before 0 i have 127 but no 126,125...
When i plug i have all values but only in this cycle.

Is it clear?

Please post your code as it looks now.

#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;

}