Using Nick Gammon's frequency counter sketch and a LCD HD44780

Hi,
I'm trying to display the measured data on a HD44780 LCD. The LCD-output sketch works separately but in combination with the counter sketch it don't.
Somehow the frequency sketch interrupts the display pins.
How can I solve this with not changing the output pins for the display? Thinking and trying about several days without a solution so far :confused:

best,
tim

// Timer and Counter example
// Author: Nick Gammon
// Date: 17th January 2012

// Input: Pin D5



#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"
Adafruit_LiquidCrystal lcd(4, 2, 6, 7, 8, 9);
byte block_1[8] = {  0b00000,  0b00000,  0b00000,  0b00000,  0b00000,  0b00000,  0b00000,  0b11111};
byte block_2[8] = {  0b00000,  0b00000,  0b00000,  0b00000,  0b00000,  0b00000,  0b11111,  0b11111};
byte block_3[8] = {  0b00000,  0b00000,  0b00000,  0b00000,  0b00000,  0b11111,  0b11111,  0b11111};
byte block_4[8] = {  0b00000,  0b00000,  0b00000,  0b00000,  0b11111,  0b11111,  0b11111,  0b11111};
byte block_5[8] = {  0b00000,  0b00000,  0b00000,  0b11111,  0b11111,  0b11111,  0b11111,  0b11111};
byte block_6[8] = {  0b00000,  0b00000,  0b11111,  0b11111,  0b11111,  0b11111,  0b11111,  0b11111};
byte block_7[8] = {  0b00000,  0b11111,  0b11111,  0b11111,  0b11111,  0b11111,  0b11111,  0b11111};
byte block_8[8] = {  0b11111,  0b11111,  0b11111,  0b11111,  0b11111,  0b11111,  0b11111,  0b11111};

unsigned long i = 3000;
int sensorValue = 9999;
int k = 6000;
int gegossen = 0;
boolean watering = LOW;

// these are checked for in the main program
volatile unsigned long timerCounts;
volatile boolean counterReady;

// internal to counting routine
unsigned long overflowCount;
unsigned int timerTicks;
unsigned int timerPeriod;

void startCounting (unsigned int ms) 
  {
  counterReady = false;         // time not up yet
  timerPeriod = ms;             // how many 1 ms counts to do
  timerTicks = 0;               // reset interrupt counter
  overflowCount = 0;            // no overflows yet

  // reset Timer 1 and Timer 2
  TCCR1A = 0;             
  TCCR1B = 0;              
  TCCR2A = 0;
  TCCR2B = 0;

  // Timer 1 - counts events on pin D5
  TIMSK1 = bit (TOIE1);   // interrupt on Timer 1 overflow

  // Timer 2 - gives us our 1 ms counting interval
  // 16 MHz clock (62.5 ns per tick) - prescaled by 128
  //  counter increments every 8 µs. 
  // So we count 125 of them, giving exactly 1000 µs (1 ms)
  TCCR2A = bit (WGM21) ;   // CTC mode
  OCR2A  = 124;            // count up to 125  (zero relative!!!!)

  // Timer 2 - interrupt on match (ie. every 1 ms)
  TIMSK2 = bit (OCIE2A);   // enable Timer2 Interrupt

  TCNT1 = 0;      // Both counters to zero
  TCNT2 = 0;     

  // Reset prescalers
  GTCCR = bit (PSRASY);        // reset prescaler now
  // start Timer 2
  TCCR2B =  bit (CS20) | bit (CS22) ;  // prescaler of 128
  // start Timer 1
  // External clock source on T1 pin (D5). Clock on rising edge.
  TCCR1B =  bit (CS10) | bit (CS11) | bit (CS12);
  }  // end of startCounting

ISR (TIMER1_OVF_vect)
  {
  ++overflowCount;               // count number of Counter1 overflows  
  }  // end of TIMER1_OVF_vect


//******************************************************************
//  Timer2 Interrupt Service is invoked by hardware Timer 2 every 1 ms = 1000 Hz
//  16Mhz / 128 / 125 = 1000 Hz

ISR (TIMER2_COMPA_vect) 
  {
  // grab counter value before it changes any more
  unsigned int timer1CounterValue;
  timer1CounterValue = TCNT1;  // see datasheet, page 117 (accessing 16-bit registers)
  unsigned long overflowCopy = overflowCount;

  // see if we have reached timing period
  if (++timerTicks < timerPeriod) 
    return;  // not yet

  // if just missed an overflow
  if ((TIFR1 & bit (TOV1)) && timer1CounterValue < 256)
    overflowCopy++;

  // end of gate time, measurement ready

  TCCR1A = 0;    // stop timer 1
  TCCR1B = 0;    

  TCCR2A = 0;    // stop timer 2
  TCCR2B = 0;    

  TIMSK1 = 0;    // disable Timer1 Interrupt
  TIMSK2 = 0;    // disable Timer2 Interrupt
    
  // calculate total count
  timerCounts = (overflowCopy << 16) + timer1CounterValue;  // each overflow is 65536 more
  counterReady = true;              // set global flag for end count period
  }  // end of TIMER2_COMPA_vect

void setup () 
  {
  Serial.begin(115200);       
  Serial.println("Frequency Counter");
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  digitalWrite(11, HIGH); //grünes kabel
  digitalWrite(12, LOW);
 
  pinMode(4, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(5, INPUT);
  analogWrite(3, 128);
  // set up the LCD's number of rows and columns:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Feuchte:");
  lcd.createChar(0, block_1);
  lcd.createChar(1, block_2);
  lcd.createChar(2, block_3);
  lcd.createChar(3, block_4);
  lcd.createChar(4, block_5);
  lcd.createChar(5, block_6);
  lcd.createChar(6, block_7);
  lcd.createChar(7, block_8);
  } // end of setup




void loop () 
  {

  // stop Timer 0 interrupts from throwing the count out
  byte oldTCCR0A = TCCR0A;
  byte oldTCCR0B = TCCR0B;
  TCCR0A = 0;    // stop timer 0
  TCCR0B = 0;    
  
  startCounting (500);  // how many ms to count for

  while (!counterReady) 
     { }  // loop until count over

  // adjust counts by counting interval to give frequency in Hz
  float frq = (timerCounts *  1000.0) / timerPeriod;
  frq=frq/270;
  Serial.print ("Sensor: ");
  Serial.println ((unsigned long) frq);

  // restart timer 0
  TCCR0A = oldTCCR0A;
  TCCR0B = oldTCCR0B;
  
  // let serial stuff finish
  delay(200);

    lcd.clear();
    lcd.print("Moisture:");
    lcd.setCursor(8, 0);
    lcd.print("Moisture:");
    lcd.setCursor(12, 0);
    lcd.print(k);




  }   // end of loop

The LCD-output sketch works separately but in combination with the counter sketch it don't.
Somehow the frequency sketch interrupts the display pins.

I have run your sketch, and find no fundamental pin conflicts with the lcd. What specific problems are you seeing that indicate the frequency sketch disturbs the lcd pins. I was using a different liquid crystal library and didn't see the custom characters so if that is the problem, I didn't test that. I can see "Feuchte:" in setup and "Moisture 6000" in loop.

What are you using for input to pin5 for the counter?

 analogWrite(3, 128);

If you are trying to jumper pin3 to pin 5 for a test signal, this will not work. You need to bring a signal from another arduino or signal source. Connect the grounds.

With analogWrite(3,128) this will give a value of 1 as 490/270 is cast to an integer

frq=frq/270;//reduces to value = 1 with integer  
  Serial.print ("Sensor: ");
  Serial.println ((unsigned long) frq);

You print "Moisture" twice on the same line

lcd.clear();
    lcd.print("Moisture:");
    lcd.setCursor(8, 0);
   // lcd.print("Moisture:");
    lcd.setCursor(12, 0);
    lcd.print(k);