LCD keep flicking

Hi all
I am new here. I am working a code i found for creating a simple square generator. The program is running with my mega 2560. The issue i have is that the text on the LCD(connected with i2c) is keep flicking all time. The text except this is presented right. I am enclosing the code for help. My first thought is that maybe happening because is inside the loop. I am sorry in case my writing is not as should but i am new in the forum.

Thank you
Nik

#include <LiquidCrystal_I2C.h>
#include <TimerOne.h>
#include <Wire.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
unsigned long t = 1000, f, k = 512; // default 1000 μs (1000 Hz), meander, pulse

byte k1, kn, kn1, kn2;
int drive, drive0;

void setup()
{
lcd.init(); // initialize the lcd
lcd.backlight();

pinMode(12, OUTPUT);
pinMode(11, INPUT); // button at input 11
pinMode(22, INPUT); // button at input 22
pinMode(23, INPUT); // button at input 23
}

void loop() {
// put your main code here, to run repeatedly:

Timer1.initialize(t); // period

Timer1.pwm(12, k); // k - fill factor 0-1023.

kn = digitalRead(11); // button input 11 (- pulse period)

kn1 = digitalRead(22); // button input 22 (+ pulse period)

kn2 = digitalRead(23); // button input 23 (+ circle fill factor)

if (kn == HIGH) { // decreasing the period
drive++;
if (drive < 30) {
t = t - 1;
}
// if the button is held for a long time, the correction of the pulse

else if (drive > 30 && drive < 60 ) {
  t = t - 10;
}
else if (drive >= 60 && drive < 100) {
  t = t - 100;
}
else if (drive >= 100) {
  t = t - 1000;
}

}
else {
drive = 0;
}

if (kn1 == HIGH) { // adding a period
drive0++;
if (drive0 < 30) {
t = t + 1;
// if the button is held for a long time, the correction of the

}
else if (drive0 > 30 && drive0 < 60 ) {
  t = t + 10;
}
else if (drive0 >= 60 && drive0 < 100) {
  t = t + 100;
}
else if (drive0 >= 100) {
  t = t + 1000;
}

}
else {
drive0 = 0;
}
if (t == 0 || t > 300000) { // limiting the pulse duration to the minimum, if

t = 1;

}
if (t > 200000 && t < 300000) { // limiting the pulse duration to the

t = 200000;

}
f = 1000000 / t; // calculate the frequency
k1 = k * 100 / 1024; // calculate% fill factor

if (kn2 == HIGH) { // button for adjusting the fill factor (in a circle from

k = k + 16; // step 16 out of 1024 (you can do 8 for smoother adjustment)

}
if (k == 1024) {
k = 0;
}
// displaying information on the indicator
lcd.setCursor(0, 0);
lcd.print("T=");
lcd.print(t);
lcd.print(" us");
lcd.setCursor(12, 0);
lcd.print(k1);
lcd.print(" %");
lcd.setCursor(0, 1);
lcd.print("F=");
lcd.print(f);
lcd.print(" Hz");
delay(300);
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
}

See how your code is poorly formatted (like we can’t read it at all). Try posting the entire code in code rages

Like this so we can read it)

Read the forum guidelines to see how to properly post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Hi
I hope is good now

Nope.

Here is your code formatted with the autoformat tool and posted in code tags (</>).

#include <LiquidCrystal_I2C.h>
#include <TimerOne.h>
#include <Wire.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
unsigned long t = 1000, f, k = 512; // default 1000 μs (1000 Hz), meander, pulse

byte k1, kn, kn1, kn2;
int drive, drive0;

void setup()
{
   lcd.init(); // initialize the lcd
   lcd.backlight();

   pinMode(12, OUTPUT);
   pinMode(11, INPUT); // button at input 6
   pinMode(22, INPUT); // button at input 7
   pinMode(23, INPUT); // button at input 13
}

void loop()
{
   // put your main code here, to run repeatedly:
   Timer1.initialize(t); // period
   Timer1.pwm(12, k); // k - fill factor 0-1023.
   kn = digitalRead(11); // button input 6 (- pulse period)
   kn1 = digitalRead(22); // button input 22 (+ pulse period)
   kn2 = digitalRead(23); // button input 13 (+ circle fill factor)

   if (kn == HIGH) // decreasing the period
   {
      drive++;
      if (drive < 30)
      {
         t = t - 1;
      }
      // if the button is held for a long time, the correction of the pulse

      else if (drive > 30 && drive < 60 )
      {
         t = t - 10;
      }
      else if (drive >= 60 && drive < 100)
      {
         t = t - 100;
      }
      else if (drive >= 100)
      {
         t = t - 1000;
      }

   }
   else
   {
      drive = 0;
   }

   if (kn1 == HIGH) // adding a period
   {
      drive0++;
      if (drive0 < 30)
      {
         t = t + 1;
         // if the button is held for a long time, the correction of the

      }
      else if (drive0 > 30 && drive0 < 60 )
      {
         t = t + 10;
      }
      else if (drive0 >= 60 && drive0 < 100)
      {
         t = t + 100;
      }
      else if (drive0 >= 100)
      {
         t = t + 1000;
      }

   }
   else
   {
      drive0 = 0;
   }
   if (t == 0 || t > 300000) // limiting the pulse duration to the minimum, if
   {

      t = 1;

   }
   if (t > 200000 && t < 300000) // limiting the pulse duration to the
   {

      t = 200000;

   }
   f = 1000000 / t; // calculate the frequency
   k1 = k * 100 / 1024; // calculate% fill factor

   if (kn2 == HIGH) // button for adjusting the fill factor (in a circle from
   {

      k = k + 16; // step 16 out of 1024 (you can do 8 for smoother adjustment)

   }
   if (k == 1024)
   {
      k = 0;
   }
   // displaying information on the indicator
   lcd.setCursor(0, 0);
   lcd.print("T=");
   lcd.print(t);
   lcd.print(" us");
   lcd.setCursor(12, 0);
   lcd.print(k1);
   lcd.print(" %");
   lcd.setCursor(0, 1);
   lcd.print("F=");
   lcd.print(f);
   lcd.print(" Hz");
   delay(300);
   lcd.setCursor(0, 0);
   lcd.print(" ");
   lcd.setCursor(0, 1);
   lcd.print(" ");
}

What does that code do?

Hi
I follow the procedure as directed from you about the code and i posted again i don't know what happened. Anyway next time i will be more carefull.
Second about these 5 lines of code really i don't know why are there and it shouldn't. I took them out and reload now is ok.
The code is copy-paste from somewhere else. I just need a generator quickly to test a diy oscilloscope for other project and happened to have this arduino unused from college. Most i am doing pic microcontrollers. Not very good yet because is 1 year i started from scratch. Anyway i am trying to follow the progress even in that age. I am electric engineer in industry and these pic's are usefull to learn. Arduino is easier coding i think but is 3 days now looking at it and seems wee bit weird to. I will keep trying is the only think you can do.
Sorry for the length i just pass some info about me for know eachother bit better.
Now problem solved thank you all for your time.

Best regards
Nik

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