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(" ");
}