Need help with garbled text

Wondered if anyone can help . I've adapted this sketch for my purpose removed things i don't need and changed the display from oled to lcd . The problem in having is in the void setstep() { section when i scroll through the steps the last part of the text from the previous step setting remains and ends up garbled up. Any help would be appreciated im still trying to learn how things work



`.`/********************************************************************************************************
  10kHz to 120MHz VFO / RF Generator with Si5351 and Arduino Nano, with Intermediate Frequency (IF)
  offset (+ or -).ORIGINAL SKETCH BY By J. CesarSound - ver 1.0 - Dec/2020. Modded by recoil 2022
*********************************************************************************************************/

//Libraries
#include <Wire.h>                 
#include <Rotary.h>               
#include <si5351.h>               
#include <LiquidCrystal_I2C.h>

//User preferences
//------------------------------------------------------------------------------------------------------------
#define IF         +7801       //Enter your IF frequency, ex: 455 = 455kHz, 10700 = 10.7MHz, 0 = to direct convert receiver or RF generator, + will add and - will subtract IF offfset.
#define FREQ_INIT  28000000   //Enter your initial frequency at startup, ex: 7000000 = 7MHz, 10000000 = 10MHz, 840000 = 840kHz.
#define XT_CAL_F   16000      //Si5351 calibration factor, adjust to get exatcly 10MHz. Increasing this value will decreases the frequency and vice versa.
#define tunestep   A0        //Change the pin used by encoder push button if you want.
//------------------------------------------------------------------------------------------------------------

Rotary r = Rotary(2, 3);
LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display
Si5351 si5351;

  
  
unsigned long freq = FREQ_INIT;
unsigned long freqold, fstep;
long interfreq = IF;
long cal = XT_CAL_F;
unsigned long long pll_freq = 90000000000ULL;
byte encoder = 1;
byte stp;
unsigned int period = 100;   //millis display active
unsigned long time_now = 0;  //millis display active

ISR(PCINT2_vect) {
  char result = r.process();
  if (result == DIR_CW) set_frequency(1);
  else if (result == DIR_CCW) set_frequency(-1);
}

void set_frequency(short dir) {
  if (encoder == 1) {                         //Up/Down frequency
    if (dir == 1) freq = freq + fstep;
    if (freq >= 120000000) freq = 120000000;
    if (dir == -1) freq = freq - fstep;
    if (fstep == 1000000 && freq <= 1000000) freq = 1000000;
    else if (freq < 10000) freq = 10000;
  }
}

void setup() {
  Wire.begin();
  lcd.init();                      // initialize the lcd
  lcd.init();
  lcd.backlight();
  
 
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(tunestep, INPUT_PULLUP);

  

  si5351.init(SI5351_CRYSTAL_LOAD_8PF, 0, cal);
  si5351.output_enable(SI5351_CLK0, 1);                  //1 - Enable / 0 - Disable CLK
  si5351.output_enable(SI5351_CLK1, 0);
  si5351.output_enable(SI5351_CLK2, 0);
  si5351.drive_strength(SI5351_CLK0, SI5351_DRIVE_2MA);  //Output current 2MA, 4MA, 6MA or 8MA

  PCICR |= (1 << PCIE2);
  PCMSK2 |= (1 << PCINT18) | (1 << PCINT19);
  sei();

  stp = 5;
  setstep();
  layout();
  displayfreq();
}

void loop() {
  if (freqold != freq) {
    time_now = millis();
    tunegen();
    freqold = freq;
  }

  if (digitalRead(tunestep) == LOW) {
    time_now = (millis() + 300);
    setstep();
    delay(300);
  }

  if ((time_now + period) > millis()) {
    displayfreq();
    layout();
  }
}

void tunegen() {
  si5351.set_freq_manual((freq + (interfreq * 1000ULL+620)) * 100ULL, pll_freq, SI5351_CLK0);
}


void displayfreq() {
  unsigned int m = freq / 1000000;
  unsigned int k = (freq % 1000000) / 1000;
  unsigned int h = (freq % 1000) / 1;

  char buffer[15] = "";
  if (m < 1) {
    lcd.setCursor(0, 0); sprintf(buffer, "%003d.%003d", k, h);
  }
  else if (m < 100) {
    lcd.setCursor(0, 0); sprintf(buffer, "%2d.%003d.%003d", m, k, h);
  }
  else if (m >= 100) {
    unsigned int h = (freq % 1000) / 10;
    lcd.setCursor(0, 0); sprintf(buffer, "%2d.%003d.%02d", m, k, h);
  }
  lcd.print(buffer);
}

void setstep() {
  lcd.setCursor(5, 1);
  switch (stp) {
    case 1:
      stp = 2;
      fstep = 1;
      lcd.print( "1hz");
      break;
    case 2:
      stp = 3;
      fstep = 10;
      lcd.print("10hz");
      break;
    case 3:
      stp = 4;
      fstep = 1000;
      lcd.print("1k");
      break;
    case 4:
      stp = 5;
      fstep = 5000;
      lcd.print("5k");
      break;
    case 5:
      stp = 6;
      fstep = 10000;
      lcd.print("10k");
      break;
    case 6:
      stp = 1;
      fstep = 1000000;
      lcd.print("1M");
      break;
      
  }
 
}

void layout() {
  
  lcd.setCursor(0, 1);
  lcd.print("STEP:");
  lcd.setCursor(13, 0);
  lcd.print("MHz");
  
}``

I am not going to try to wade through that mess! But your description indicates you need to set the cursor to the line or part of the line and print BLANKS to the LCD before printing the text you are wanting to see.

Please edit your post to add code tags ("</>" editor button).

sorry i must have done it wrong should be right now i think

There are spurious characters at the beginning and end of the code you posted.

After removing those, does the problem go away?

If you mean the . and the `` guess i messed up on uploading the code again as there not there on the original . sorry I'm new to here and Arduino code its took me a nearly a year to admit defeat and ask for help with a few things on here

I downloaded the code, got rid of those spurious characters, and do not see the problem that you describe.

Perhaps there are other spurious characters. Copy the code into a text editor, perform "new" in the Arduino IDE, and paste the code back in.

just tried that still getting the same it should say STEP:1M but it still has bits left from the previous step display

also forgot to say im using a nano if that makes any difference

I thought this meant you were having a problem with the CODE EDITOR.

Clear the display, or the field, before printing on it.

Make all the strings the same length.
(Note: The name Hertz is abbreviated "Hz", not "hz")

      lcd.print("1 Hz  ");
      lcd.print("10 Hz ");
      lcd.print("1 kHz ");
      lcd.print("5 kHz ");
      lcd.print("10 kHz");
      lcd.print("1 MHz ");

I don't see any lcd.clear() in your code. Generally, if you are overwriting existing characters, you will want to clear the full display (or at least print a line of blank spaces to the line you are writing to) before you write to it again.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.