using timer1 with lcd

Hi I want to use timerone with lcd normally lcd working but if ı decrease OCR1A=159; lcd not working
thanks

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


LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // See above - same as DF Robot
char* menu[] = {
  "Ali", "veli", "a3", "a4", "a5", "a6",
  "a7", "a8", "a9", "a10", "a11", "a12",
  "a13", "A14", "A15", "A16", "A17", "A18", "A19",
  "a20", "a21"
};
int freqvar[21] = {
  6947, 4900, 8000, 8830, 1414, 11646, 11902, 12692, 16760, 17032, 700, 4000, 4139, 4500, 5400, 5480, 1600, 5200, 5301, 5373, 5494
};
int dutyvar[21];
int freq[21];
int freq_addr[] = {
  50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94
};
byte menum;
float duty_on;
float duty_off;
int buttonState[6];
int duty[21] = {
  70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90
};
int duty_addr[] = {
  100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120
};
int i;
int y;
int x;
int buttonPin[] = {
  2, 3, 4, 5, 6, 7};
float pil_sev;
//*********************************************************
//
float pulseWidth;
int pulseWidthScaled;
int PWCurrent;
byte PWTolerance = 8;
//
int frequency;
int freqCurrent;
byte freqTolerance = 2;
unsigned int freqScaled;
//
byte wave;
long t;
long samplerate;
long period;
//
void setup()
{
  Serial.begin(9600);
  lcd.begin (16, 2);
  lcd.setBacklight(HIGH);
  DDRD = DDRD | B11111100;
  pinMode(9, OUTPUT);
  cli();
  TCCR1A=0;
  TCCR1B=0;
  OCR1A=159;
  TCCR1B|=(1<<WGM12);
  TCCR1B|=(1<<CS10);
  TIMSK1|=(1<<OCIE1A);
  samplerate = 100000;
  Timer1.attachInterrupt(sinyal);
  sei();
}

void loop()
{
  check_buttonState();
  check_menu();  
  check_freq();
  check_duty();
  sinyal();
  var();
  pil();
}
///////////////////////////////
void check_menu()
{
  if (buttonState[0] == HIGH)
  {
    menum++;
    if (menum == 21)
    {
      menum = 0;
    }

    delay(100);
    buttonState[0] = LOW;
    lcd.clear();
  }
  else
  {
    delay(50);
  }
  for (i = 0; i < 21; i++)
  {
    freq[i] = EEPROMReadlong(freq_addr[i]);
    duty[i] = EEPROM.read(duty_addr[i]);
    lcd.setCursor(0, 0);
    lcd.print(menu[menum]);
  }

}
//////////////////////////////////
void check_freq()
{
  if (buttonState[1] == HIGH)
  {
    freq[menum]++;
    delay(100);
  }
  if (buttonState[2] == HIGH)
  {
    freq[menum]--;
    delay(100);
  }
  EEPROMWritelong(freq_addr[menum], freq[menum]);
  freq[menum] = EEPROMReadlong(freq_addr[menum]);
  frequency = freq[menum];
  period = samplerate / frequency;
  Serial.println(period);
  Serial.println(frequency);
  lcd.setCursor(0, 1);
  lcd.print(freq[menum]);

}
//////////////////////////////////
void check_duty()
{
  if (buttonState[3] == HIGH)
  {
    duty[menum]++;
    delay(100);
  }
  if (buttonState[4] == HIGH)
  {
    duty[menum]--;
    delay(100);
  }

  EEPROM.write(duty_addr[menum], duty[menum]);
  if (duty[menum] > 100) {
    EEPROM.write(duty_addr[menum], 0);
  }
  if (duty[menum] < 0) {
    EEPROM.write(duty_addr[menum], 100);
  }
  duty[menum] = EEPROM.read(duty_addr[menum]);
  pulseWidth = (duty[menum]);
  pulseWidthScaled = pulseWidth * period / 100;
  Serial.println(pulseWidthScaled);
  Serial.println(duty[menum]);
  Serial.println(pulseWidth);
  lcd.setCursor(12, 0);
  lcd.print("%");
  lcd.setCursor(13, 0);
  lcd.print(duty[menum]);
}
//////////////////////////////////
void sinyal()
{
  t += 1;
  if (t >= period) {
    t = 0;
  }

  if (pulseWidthScaled <= t) {
    digitalWrite(9, HIGH);
  }
  else {
    digitalWrite(9, LOW);
  }


}
////////////////////////////////
void var()
{
  if (buttonState[5] == HIGH)
  {
    for (i = 0; i < 22; i++)
    {
      EEPROMWritelong(freq_addr[i], freqvar[i]);
      EEPROMWritelong(duty_addr[i], dutyvar[i]);
      lcd.clear();
      lcd.print("Kaydedildi");
    }
    menum = 0;

  }

}
///////////////////////
void pil()
{ 
  pil_sev = analogRead(A3) * 9 / 1024;
  lcd.setCursor(12, 1);
  lcd.print(pil_sev);


}
void check_buttonState()
{
  for (i = 0; i < 6; i++)
  {
    buttonState[i] = digitalRead(buttonPin[i]);
  }
}

void EEPROMWritelong(int address, long value)
{
  //Decomposition from a long to 4 bytes by using bitshift.
  //One = Most significant -> Four = Least significant byte
  byte one = (value & 0xFF);
  byte two = ((value >> 8) & 0xFF);

  //Write the 4 bytes into the eeprom memory.
  EEPROM.write(address, two);
  EEPROM.write(address + 1, one);
}
long EEPROMReadlong(long address)
{
  //Read the 4 bytes from the eeprom memory.

  long two = EEPROM.read(address + 1);
  long one = EEPROM.read(address);

  //Return the recomposed long by using bitshift.
  return ((two << 0) & 0xFF) + ((one << 8) & 0xFFFF);
}

forum.ino (4.6 KB)

Welcome to the Forum. Please read this post:

How to use this forum - please read.

Please post your code using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpreted by the forum code as italics or funny emoticons.

Unless the sketch is too large, it's better if you post your code, rather than attach it. When it's attached, we have to download it, create a folder then open your code in our IDE. And afterwards, the folder remains unless we navigate to the "Temp" folder and manually remove it. It's much easier to just view the code in your post.

After you have cleaned up the presentation of you code in this tread, you can consider this:

You're driving timer1 without a prescaler, so OCR1A with a value of 159 yields 100,000 Hz.
I guess at that rate, the interrupt service routine is comsuming all the resources.

Edit: Hz not kHz

What part of the second paragraph that I posted, did you not understand?

You have set up the timer yourself and have not used Timer1.initialize() to control the period so you might as well not bother with calling the library interrupt. It can only add overhead to the standard compare match interrupt.

I agree with 6v6gt that your timing is too close. My estimation that entering the interrupt, performing the digitalWrite() and exiting the interrupt will take at least 8 us, and that's without any added overhead of the library call of the interrupt.

I'm not sure how you are managing pin 9, but since it is timer1 output A you might be better off using the hardware output control of TCCR1A and avoiding the use of the interrupt.