how to display XX.XXX on lcd

Hi all,

1st sorry if this is wrong in any way - any pointers are welcome.
Second, like every one starts, I am new and all that…:stuck_out_tongue:
The issue: on the line 145 (lcd0.print(set_voltage,3):wink: all works fine until voltage changes from 9 to 10. Than the line shifts 1 digit and when the voltage is lowered it ends up with an extra digit that is not wanted. I am looking for a solution to force formatting and the position if possible, for example 00.000
When 1.345V to show 01.345 and when 11.003 to display 11.003. Hope my issue is understood and that I can find a solution. Thank you all.

#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_MCP4725.h>


LiquidCrystal_I2C lcd0(PCF8574_ADDR_A21_A11_A01, 4, 5, 6, 16, 11, 12, 13, 14, POSITIVE);
LiquidCrystal_I2C lcd1(PCF8574_ADDR_A21_A10_A00, 4, 5, 6, 16, 11, 12, 13, 14, POSITIVE); 
Adafruit_MCP4725 dac0; //dac CH1
Adafruit_MCP4725 dac1; //dac CH2

int ClockPinV = PB10; 
int DataPinV = PB1; 
volatile int setvolt;

int incrementbutPin = PB11;
int incrementoutPin = PA0;
int stateincrement = LOW;
int stateinc = LOW;

const int incrementpin = PB0;
#define incbig           10
#define incsmall          1

int currsensPin = PA1;
int voltsensPin = PA2;

void encvolt() { 
  static unsigned long DebounceTimer;
  if ((unsigned long)(millis() - DebounceTimer) >= (100)) { // standard blink without delay timer
    DebounceTimer = millis();
    if (digitalRead(DataPinV) == LOW) // switch to LOW to reverse direction of Encoder counting
    {
      if (digitalRead(incrementpin) == HIGH && stateinc == LOW) {
      setvolt = setvolt + incbig;
      }
      else if (digitalRead(incrementpin) == LOW && stateinc == LOW) {
      setvolt = setvolt + incsmall;
      }
      // setvolt++;
    }
    else {
      if (digitalRead(incrementpin) == HIGH && stateinc == LOW) {
      setvolt = setvolt - incbig;
      }
      else if (digitalRead(incrementpin) == LOW && stateinc == LOW) {
      setvolt = setvolt - incsmall;
      }
      // setvolt--;
    }
    if (setvolt >= 292) setvolt = 292;
    if (setvolt <= 0) setvolt = 0;
  }
}

void incrementISR(){
   static unsigned long DebounceTimer;
  if ((unsigned long)(millis() - DebounceTimer) >= (200)) { 
    DebounceTimer = millis();
   stateincrement = !stateincrement;
  }
  digitalWrite(incrementoutPin, stateincrement);
}

void setup()
{
  pinMode(currsensPin , INPUT);
  pinMode(voltsensPin , INPUT);
  pinMode(incrementpin , INPUT_PULLDOWN);  
  pinMode(ClockPinV , INPUT);
  pinMode(DataPinV , INPUT);
  attachInterrupt(digitalPinToInterrupt(ClockPinV), encvolt, FALLING); 
  pinMode(incrementbutPin, INPUT);
  pinMode(incrementoutPin, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(incrementbutPin), incrementISR, CHANGE);
  Serial.begin(9600);
  Serial.print("serial is working...");
  dac0.begin(0x60);
  dac1.begin(0x61);
  Wire.begin();
  lcd0.begin(20, 4);
  lcd0.backlight();
  lcd1.begin(20, 4);
  lcd1.backlight();

}
  int val0 = 1024;
  int val1 = 2048;

float set_voltage = 00.000;
int set_curr;
int RawValue;
int real_current;
int real_curren_offset = 0;
int real_output;
float real_output2 = 00.000;
int real_current_ma;
int dac_value;

void loop()
{
  int svolt = setvolt;
  svolt = map(svolt, 0, 292, 0, 2047);
  
  set_voltage = (3.3/2047)*svolt;
  set_curr =  (3.3/2047)*svolt;
  RawValue = analogRead(currsensPin);  
  real_current = (3.3/2047 )*RawValue;         
  real_current = real_current - real_curren_offset;      
  real_output= analogRead(voltsensPin);    
  real_output2 = (32.3/2047)*svolt;  

if(real_current_ma < set_curr)
  {

    if(set_voltage > real_output2)
    {
      dac_value = dac_value + 1;                 
      dac_value = constrain(dac_value, 0, 4096);
    }

    if(set_voltage < real_output2)
    {
      dac_value = dac_value - 1;                  
      dac_value = constrain(dac_value, 0, 4096);
    }  
  }
  
  if(real_current_ma > set_curr)
  {
    dac_value = dac_value - 1;                 
    dac_value = constrain(dac_value, 0, 4096);
  }

  dac1.setVoltage(dac_value, false);
  dac0.setVoltage(100, false);
  
// char buffer [6]; // a few bytes larger than your LCD line
// uint16_t svoltage = set_voltage; // data read from instrument
// sprintf (buffer, "V: %d,%.3d", set_voltage); // send data to the buffer
  lcd0.print("Set: ");
  lcd0.setCursor(5,0);
  // char string_to_print[10] = { 0 }; 
  // sprintf(string_to_print, "%2u.%.3u", set_voltage); 
  lcd0.print(set_voltage,3);  // THIS LINE IS THE ISSUE... :P
  // lcd0.print(buffer);
  lcd0.setCursor(0,1);
  lcd0.print("Out: ");
  lcd0.setCursor(5,1);
  lcd0.print(real_output2,3);
  lcd0.setCursor(0,2);
  lcd0.print("dac 1: ");
  lcd0.print(setvolt);
  lcd0.print(" = 2.500V");
  lcd0.setCursor(0,3);
  lcd0.print(" Mode: ");
    if (stateincrement == LOW){
      lcd0.setCursor(7,3);
      lcd0.print("CV");
    }
    if (stateincrement == HIGH){
      lcd0.setCursor(7,3);
      lcd0.print("CC");
      delay(300);
      lcd0.setCursor(7,3);
      lcd0.print("  ");
      delay(300);
      // lcd0.print("CC");
    }
  

  lcd1.setCursor(0,0);
  lcd1.print(" I2C  LCD II  with -");
  lcd1.setCursor(0,1);
  lcd1.print("    STM32F103C8T6 ");
  lcd1.setCursor(0,2);
  lcd1.print("dac 0: ");
  lcd1.print(setvolt);
  lcd1.print(" = 1.250V");
  lcd1.setCursor(0,3);
  lcd1.print(" Screen II ");

  Serial.println("serial is working..." + String(dac_value));
}

Unless I missed it I can't see anywhere that you clear the display, so you display new stuff over old. If, for example, the old text has 10 characters and the new text has 9 characters then the 10th character of the old text will remain on the display.

hi,
clearing makes in blink... i would like not to make it blink. I hope there are other options...

So make sure that when you write new data to print enough characters to completely overwrite the old data. Use extra spaces if necessary, for instance. Or print enough spaces to overwrite old data, reset the cursor and print new data.

In that case make sure you print some spaces to the end of where the text goes to make sure there are no old characters left on the display.

If you want a leading 0, print a '0' if data to be printed is less than 10 then the data to be printed.

int set_whole_volts = (int)set_voltage;
int set_millivolts = (int)((set_voltage - set_whole_volts) * 1000);

if (set_whole_volts < 10) {
  lcd0.print("0");
}
lcd0.print(set_whole_volts);
lcd0.print(".");
if (set_millivolts < 10) {
  lcd0.print("00");
}
else if (set_millivolts < 100) {
  lcd0.print("0");
}
lcd0.print(set_millivolts);

Thank you odometer, that was solved. Now I have another problem. In the following scketch there is a “if” (from line 59) statement to change dac_value. It is very slow for some reason and can not figure out why. Removed serial and tried DAC from pot. Serial did not fix it and the DAC works fast if “if” removed… Please help… Thank you.

#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define MCP4725 0x61
#define MCP4725B 0x62
unsigned int val;
byte buffer[3];

LiquidCrystal_I2C lcd0(PCF8574_ADDR_A21_A11_A01, 4, 5, 6, 16, 11, 12, 13, 14, POSITIVE); //all three address pads on the PCF8574 shield are open
LiquidCrystal_I2C lcd1(PCF8574_ADDR_A21_A10_A00, 4, 5, 6, 16, 11, 12, 13, 14, POSITIVE); //all three address pads on the PCF8574 shield are shorted

int ClockPinV = PB1; 
int ClockPinA = PA6; 

const int currsensPin = PA3;
const int voltsensPin = PA2;

void setup()
{
  pinMode(currsensPin , INPUT_ANALOG);
  pinMode(voltsensPin , INPUT_ANALOG); 
  pinMode(ClockPinV , INPUT_ANALOG);
  pinMode(ClockPinA , INPUT_ANALOG);
  Wire.begin();
  lcd0.begin(20, 4);
  lcd0.backlight();

}

int val0 = 1024;
int val1 = 2048;

float set_voltage = 00.000;
float set_curr = 00.000;
float RawValue = 0;
float real_current  = 0;
float real_curren_offset = 0;
float real_output = 0;
float real_output2 = 00.000;
float real_current_ma = 0000;
volatile int dac_value;

void loop()
{
  analogReadResolution(12);

  int svolt = analogRead(ClockPinV);
  int scurr = analogRead(ClockPinA);
  set_voltage = (3.3/4096)*svolt;
  set_curr =  (3.3/4096)*scurr;
  RawValue = analogRead(currsensPin);
  real_current = RawValue * (3.3 / 4096); 
  real_current = real_current - real_curren_offset;
  real_output= analogRead(voltsensPin);
  real_output2 = (3.3/4096)*real_output;
  real_current_ma = real_current;

if(real_current_ma < set_curr)
  {
    if(set_voltage > real_output2)
    {
      dac_value ++;
        lcd0.setCursor(7,3);
        lcd0.print("CV");
    }
    if(set_voltage < real_output2)
    {
      dac_value --;
        lcd0.setCursor(7,3);
        lcd0.print("CV");
    }  
  }
  if(real_current_ma > set_curr)
  {
    dac_value --;                 
        lcd0.setCursor(7,3);
        lcd0.print("CC");
        // delay(300);
        // lcd0.setCursor(7,3);
        // lcd0.print("  ");
        // delay(300);
  }

  dac_value = constrain(dac_value, 0, 2673);
  buffer[0] = 0b01000000; //control byte
  buffer[1] = dac_value >> 4; //MSB 11-4 shift right 4 places
  buffer[2] = dac_value << 4; //LSB 3-0 shift left 4 places
  
  Wire.beginTransmission(MCP4725); //address device
  Wire.write(buffer[0]); //pointer
  Wire.write(buffer[1]); //8 MSB
  Wire.write(buffer[2]); //4 LSB
  Wire.endTransmission();


  lcd0.setCursor(0,0);
  lcd0.print("Set:"/* "Set:       V"*/);
  lcd0.setCursor(5,0);
int set_whole_volts = (int)set_voltage;
int set_millivolts = (int)((set_voltage - set_whole_volts) * 1000);

if (set_whole_volts < 10) {
  lcd0.print("0");
}
  lcd0.print(set_whole_volts);
  lcd0.print(".");
if (set_millivolts < 10) {
  lcd0.print("00");
}
else if (set_millivolts < 100) {
  lcd0.print("0");
}
  lcd0.print(set_millivolts);
  lcd0.print("V");
  lcd0.setCursor(13,0);
int set_whole_amps = (int)set_curr;
int set_milliamps = (int)((set_curr - set_whole_amps) * 1000);
if (set_whole_amps < 10) {
  lcd0.print("0");
}
  lcd0.print(set_whole_amps);
  lcd0.print(".");
if (set_milliamps < 10) {
  lcd0.print("00");
}
else if (set_milliamps < 100) {
  lcd0.print("0");
}
  lcd0.print(set_milliamps);
  lcd0.print("A");
  lcd0.setCursor(0,1);
  lcd0.print("Out:");
  lcd0.setCursor(5,1);
int out_whole_volts = (int)real_output2;
int out_millivolts = (int)((real_output2 - out_whole_volts) * 1000);
if (out_whole_volts < 10) {
  lcd0.print("0");
}
  lcd0.print(out_whole_volts);
  lcd0.print(".");
if (out_millivolts < 10) {
  lcd0.print("00");
}
else if (out_millivolts < 100) {
  lcd0.print("0");
}
  lcd0.print(out_millivolts);
  lcd0.print("V");
  lcd0.setCursor(13,1);
int out_whole_amps = (int)real_current;
int out_milliamps = (int)((real_current - out_whole_amps) * 1000);
if (out_whole_amps < 10) {
  lcd0.print("0");
}
  lcd0.print(out_whole_amps);
  lcd0.print(".");
if (out_milliamps < 10) {
  lcd0.print("00");
}
else if (out_milliamps < 100) {
  lcd0.print("0");
}
  lcd0.print(out_milliamps);
  lcd0.print("A");
  lcd0.setCursor(0,2);
  lcd0.print("dac 1: ");
  lcd0.print(svolt);
  lcd0.print(svolt);
  lcd0.setCursor(0,3);
  lcd0.print(" Mode: ");

  // Serial.println("serial is working..." + String(RawValue));
}

@mdpoz

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.
It will help you get the best out of the forum in the future.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.