Why is this not doing what I want it to? - Number selector help.

I am working on this project where you can create a variable duty-cycle and variable frequency PWM with Arduino. The idea is that you select the duty-cycle and percent with a rotary encoder, which is displayed on a liquid-crystal display, then it creates a PWM with those specifications.

There has been a ton of stuff that turned out to be way more difficult and time consuming than I initially thought. So I am trying to do this step-by-step. I am now working on selecting the 10^6, 10^5, ... to the 10^-2 digit of a number. This way you can select a number between 0000000.00 and 1000000.00.

It is behaving in a very weird way, often displaying random dashes and flickering and stuff. I am not sure what the problem is. The code itself has no syntax errors, so that is why I am confused. Please help. So here is my code:

#include <LiquidCrystal.h>
LiquidCrystal lcd(53,51,49,47,45,43);
    
#define outputA 27
#define outputB 25
float counter = 0;
int aState;
int aLastState;

int cLim;

int button = 23; 
int mode = 0; 
boolean pbs = false;
boolean bsn = false;

long number;
int number16 = 0;
int number15 = 0;
int number14 = 0;
int number13 = 0;
int number12 = 0;
int number11 = 0;
int number10 = 0;
int number1a = 0;
int number1b = 0;

void setup(){
   
}

void loop() {
number = 1000000*number16 + 100000*number15 + 10000*number14 + 1000*number13 + 100*number12 + 10*number11 + number10 + .1*number1a + .01*number1b;

lcd.cursor();
lcd.setCursor(mode + 3,0);
lcd.blink();

if (digitalRead(button) == HIGH) {
bsn = false;
}
else if (digitalRead(button) == LOW) {
bsn = true;
pbs = true;
}
if (pbs != bsn){
mode++;
upDis();
pbs = bsn;
}

cLims();
rotaryStuff();

if(mode == 0){

}
  
else if(mode == 1){
cLim = 1;
number16 = counter;
if (number16 == 1){
number15 = 0;
number14 = 0;
number13 = 0;
number12 = 0;
number11 = 0;
number10 = 0;
number1a = 0;
number1b = 0;  
mode = 0;
}
} 
else if(mode == 2){
cLim = 9;
number15 = counter;
}
else if(mode == 3){
cLim = 9;
number14 = counter;
}
else if(mode == 4){
cLim = 9;
number13 = counter;
}
else if(mode == 5){
cLim = 9;
number13 = counter;
}
else if(mode == 6){
cLim = 9;
number12 = counter;
}
else if(mode == 7){
cLim = 9;
number11 = counter;
}
else if(mode == 8){
cLim = 9;
number10 = counter;
}
else if(mode == 9){
cLim = 9;
number1a = counter;
}
else if(mode == 10){
cLim = 9;
number1b = counter;
}
else{
mode = 0;  
}
}


void upDis(){
lcd.clear();
lcd.print("# = ");
lcd.print(number16); 
lcd.print(number15); 
lcd.print(number14); 
lcd.print(number13); 
lcd.print(number12); 
lcd.print(number11); 
lcd.print(number10);
lcd.print("."); 
lcd.print(number1a); 
lcd.print(number1b); 

}

void rotaryStuff(){
  aState = digitalRead(outputA); 
   if (aState != aLastState){     
     if (digitalRead(outputB) != aState) { 
        counter ++;
        upDis();
     }
      else {
       counter --;
       upDis();
     }
   }    
   aLastState = aState;
}


void cLims(){
if (counter < 0){
counter = 0;
upDis();
}
if (counter >= cLim){
counter = cLim;
upDis();
}

}

First, which Arduino are you using? Then let's go step by step. First let's get the LCD working because end it is part of the project and between now and then it can be used to display debug messages. So put aside the code you have and get Hello World displayed on the LCD. (I already see missing items for using the display which are covered in the Hello World sketch). The link in the reference section here is LiquidCrystal - Arduino Reference.

Once you have the LCD working, we can work through just reading the encoder and displaying it on the LCD

The LCD is working fine, and I have done plenty of other stuff with it without experiencing problems. The rotary encoder part was actually copied from another sketch of mine, so I don't think that is it. I am using an Arduino 2560 Mega (which I know is totally overkill for this).

Here is a similar sketch I made for an RGB LED color mixer that makes a higher or lower pitched sound, depending on the brightness. It does not constantly display the color because you can tell from the brightness. It just updates it once you have chosen the color for all of them. It could use some improvements, but it works.

#include <LiquidCrystal.h>
LiquidCrystal lcd(53,51,49,47,45,43);
    
#define outputA 27
#define outputB 25

int red = 2;
int green = 3;
int blue = 4;
int spk = 5;

float counter = 0; 
int aState;
int aLastState;

int button = 23; 
int bs; //button state 
boolean pbs = false;
boolean bsn = false;

float reds; 
float greens;
float blues;

int a;
int b;
int c;
int z=0;

int ptn;
int cLim; 

void setup() { 

pinMode (outputA,INPUT_PULLUP);
pinMode (outputB,INPUT_PULLUP);
pinMode (button,INPUT_PULLUP); 
pinMode(spk, OUTPUT);

pinMode (red,OUTPUT);
pinMode (green,OUTPUT);
pinMode (blue,OUTPUT);

aLastState = digitalRead(outputA);  
lcd.begin(16, 2); 

bs = 0; 
cLim = 51;
} 


void loop() {
if (reds + greens + blues > 100){
tone(spk, (reds + greens + blues) * 2);
}
else {
digitalWrite(spk, LOW);  
}



rotaryStuff();

if (counter < 0){
counter = 0;
  }
if 
(counter > cLim){
counter = cLim;
}

ifConditions();
}






void ifConditions(){

if (bs == 0)
{
analogWrite(red,counter*5);
analogWrite(green,0);
analogWrite(blue,0);
reds = counter*5;
cLim = 51;
}

else if (bs == 1){
analogWrite(red,0);
analogWrite(green,counter*5);
analogWrite(blue,0);
greens = counter*5;
cLim = 51;
}

else  if (bs == 2){
analogWrite(red,0);
analogWrite(green,0);
analogWrite(blue,counter*5);
blues = counter*5;
cLim = 51;
}  

else if (bs == 3){
//cLim = 4;

analogWrite(red,reds);
analogWrite(green,greens);
analogWrite(blue,blues);

lcd.clear();
lcd.print("r:");
lcd.print(round(reds/2.55));
lcd.print("% g:");
lcd.print(round(greens/2.55));
lcd.print("% b:");
lcd.print(round(blues/2.55));
lcd.print("%");

bs++;
}

else if (bs == 4){
analogWrite(red,reds);
analogWrite(green,greens);
analogWrite(blue,blues);
if (millis() % 500 == 0){ 
  lcd.scrollDisplayRight();
}
}

else if (bs == 5){
bs = 0;
}
}

void rotaryStuff(){
  aState = digitalRead(outputA); 
   if (aState != aLastState){     
     if (digitalRead(outputB) != aState) { 
       if (counter >= 1){
        counter ++;
       }
       else if (counter < 1) {
        counter = counter +.2; }
     }
      else {
       if (counter > 1){
        counter --;
       }
       else if (counter <= 1) {
        counter = counter -.2; }
     }
   }    
   aLastState = aState;
}

I would think the second sketch does have a working LCD, whereas the first does not. The sketches have different function calls. Hint: lcd.begin()

:o REALLY ??

                                     pinMode (outputA,INPUT_PULLUP);
                                       pinMode (outputB,INPUT_PULLUP);
                                       pinMode (button,INPUT_PULLUP);