Specifying the '1's place

Setup - LCD 1602 character display hooked up to an arduino. Arduino is running a basic color changing RGB led by varying the R, G, and B outputs (0-255).

LCD displays "RED: BLUE: "
GREEN: "

and is supposed to show the values for each (i.e. “RED:255 BLUE:0 GREEN:255”

The problem I have is that the initial value is 255. As it counts down, when it gets below 100, the ones and tens place shifts left, leaving a tailing zero. When the initial value is 0, then it counts up through 255, the ones place gets shifted to the right at 10 and again at 100.

I would like the display to show the values with the '1’s place locked in a specific spot and removing leading zeros.

Also, in my code, I have the print function within each 255 - 0 transition, but I would like to make it a subroutine that’s called in each location and the many lines of setting the cursors and words is only done once.

Thanks for any help you can offer!

#define GREEN 6
#define BLUE 11
#define RED 10
#define delayTime 20
#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 5, 4, 12);

void setup() {

  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  pinMode(RED, OUTPUT);
  digitalWrite(GREEN, HIGH);
  digitalWrite(BLUE, HIGH);
  digitalWrite(RED, HIGH);
  lcd.begin(16,2);
  }

int redVal;
int blueVal;
int greenVal;
 
void loop() {
 
  int redVal = 255;
  int blueVal = 0;
  int greenVal = 0;
  
  for( int i = 0 ; i < 255 ; i += 1 ){
    greenVal += 1;
    redVal -= 1;
    analogWrite( GREEN, greenVal );
    analogWrite( RED, redVal );
    lcd.setCursor(0,0);
    lcd.print("Red:");
    lcd.setCursor(4,0);
    lcd.print(redVal);
    lcd.setCursor(8,0);
    lcd.print("Blue:");
    lcd.setCursor(12,0);
    lcd.print(blueVal);
    lcd.setCursor(0,1);
    lcd.print("Green:");
    lcd.setCursor(8,1);
    lcd.print(greenVal);  
    delay( delayTime );
    }
    
  lcd.clear();
  redVal = 0;
  blueVal = 0;
  greenVal = 255;
  for( int i = 0 ; i < 255 ; i += 1 ){
    blueVal += 1;
    greenVal -= 1;
    analogWrite( BLUE, blueVal );
    analogWrite( GREEN, greenVal );
    lcd.setCursor(0,0);
    lcd.print("Red:");
    lcd.setCursor(4,0);
    lcd.print(redVal);
    lcd.setCursor(8,0);
    lcd.print("Blue:");
    lcd.setCursor(12,0);
    lcd.print(blueVal);
    lcd.setCursor(0,1);
    lcd.print("Green:");
    lcd.setCursor(8,1);
    lcd.print(greenVal);  
    delay( delayTime );
  }
  lcd.clear();
  redVal = 0;
  blueVal = 255;
  greenVal = 0;
  for( int i = 0 ; i < 255 ; i += 1 ){
    redVal += 1;
    blueVal -= 1;
    analogWrite( RED, redVal );
    analogWrite( BLUE, blueVal );
    lcd.setCursor(0,0);
    lcd.print("Red:");
    lcd.setCursor(4,0);
    lcd.print(redVal);
    lcd.setCursor(8,0);
    lcd.print("Blue:");
    lcd.setCursor(12,0);
    lcd.print(blueVal);
    lcd.setCursor(0,1);
    lcd.print("Green:");
    lcd.setCursor(8,1);
    lcd.print(greenVal);  
    delay( delayTime );
  }
  
}

Use sprintf with a field width specifier, or print a space after any number you print

char buffer [5];
sprintf (buffer, "%4d", redVal);
lcd.print(buffer);

I may have placed it in the wrong spot, but currently I am still getting the same issue with the ones place moving to the right (increasing) and to the left (decreasing).

Here is the modified code:

#define GREEN 10
#define BLUE 6
#define RED 11
#define delayTime 20
#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 5, 4, 12);

void setup() {

  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  pinMode(RED, OUTPUT);
  digitalWrite(GREEN, HIGH);
  digitalWrite(BLUE, HIGH);
  digitalWrite(RED, HIGH);
  lcd.begin(16,2);
  }

int redVal;
int blueVal;
int greenVal;
 
void loop() {
 
  int redVal = 255;
  int blueVal = 0;
  int greenVal = 0;
  
  for( int i = 0 ; i < 255 ; i += 1 ){
    greenVal += 1;
    redVal -= 1;
    analogWrite( GREEN, greenVal );
    analogWrite( RED, redVal );
    lcd.setCursor(0,0);
    lcd.print("Red:");
    lcd.setCursor(4,0);
    char buffer [4];
    sprintf(buffer, "%3d", redVal);
    lcd.print(redVal);
    lcd.setCursor(8,0);
    lcd.print("Blue:");
    lcd.setCursor(12,0);
    char buffeb [4];
    sprintf(buffeb, "%3d", blueVal);
    lcd.print(blueVal);
    lcd.setCursor(0,1);
    lcd.print("Green:");
    lcd.setCursor(8,1);
    char buffeg [4];
    sprintf(buffeg, "%3d", greenVal);
    lcd.print(greenVal);  
    delay( delayTime );
    }
    
  lcd.clear();
  redVal = 0;
  blueVal = 0;
  greenVal = 255;
  for( int i = 0 ; i < 255 ; i += 1 ){
    blueVal += 1;
    greenVal -= 1;
    analogWrite( BLUE, blueVal );
    analogWrite( GREEN, greenVal );
    lcd.setCursor(0,0);
    lcd.print("Red:");
    lcd.setCursor(4,0);
    char buffer [4];
    sprintf(buffer, "%3d", redVal);
    lcd.print(redVal);
    lcd.setCursor(8,0);
    lcd.print("Blue:");
    lcd.setCursor(12,0);
    char buffeb [4];
    sprintf(buffeb, "%3d", blueVal);
    lcd.print(blueVal);
    lcd.setCursor(0,1);
    lcd.print("Green:");
    lcd.setCursor(8,1);
    char buffeg [4];
    sprintf(buffeg, "%3d", greenVal);
    lcd.print(greenVal);  
    delay( delayTime );
  }
  lcd.clear();
  redVal = 0;
  blueVal = 255;
  greenVal = 0;
  for( int i = 0 ; i < 255 ; i += 1 ){
    redVal += 1;
    blueVal -= 1;
    analogWrite( RED, redVal );
    analogWrite( BLUE, blueVal );
    lcd.setCursor(0,0);
    lcd.print("Red:");
    lcd.setCursor(4,0);
    char buffer [4];
    sprintf(buffer, "%3d", redVal);
    lcd.print(redVal);
    lcd.setCursor(8,0);
    lcd.print("Blue:");
    lcd.setCursor(12,0);
    char buffeb [4];
    sprintf(buffeb, "%3d", blueVal);
    lcd.print(blueVal);
    lcd.setCursor(0,1);
    lcd.print("Green:");
    lcd.setCursor(8,1);
    char buffeg [4];
    sprintf(buffeg, "%3d", greenVal);
    lcd.print(greenVal);  
    delay( delayTime );
  }
  
}

Read my last post again.
Play "spot the difference"

AWOL:
Read my last post again.
Play "spot the difference"

I must have put a decimal point in the wrong place or something. S--t! I always do that. I always mess up some mundane detail.
-Michael Bolton, Office Space

Yup, all fixed now. Thank you so much!

So for the second part of the question: how do I set up the "print status of the RGB values" stuff as a separate function to call out in each section, rather than cut/pasting it three times?