LCD Display?Timer1 Problem

I am relatively new at Arduino. I have a sketch that uses Timer1 to generate pulses. The loop portion of the sketch generates a lower frequency pulse (motPulse) with a user-settable duty cycle. I am using Timer1.start() and Timer1.stop() commands to stop the generation of Timer1 output pulses while the low frequency pulse signal motPulse is LOW and re-start them when the low frequency pulse signal motPulse is HIGH.
It works fine until I try to incorporate the LCD display commands. When I do, the Timer1.start() and Timer1.stop() commands appear to stop working since the Timer1 output pulses no longer stop during the time while the low frequency pulse signal motPulse is LOW.
The sketch is presented below. I currently have the LCD commands commented out so that the pulse generation works as designed). It goes south when I de-comment the lcd.init() command or the entire lcd sequence in Setup.


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

LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 20 chars and 4 line display

const int outPin1 = 10; //for timer
const int inPin1 = 3; // for timer
const int motPulsePin =  2; // simulates either the CW or CCW motor-on signal 
const long motPulseonTime = 70; // time in milliseconds that mot signal is high
const long motPulseoffTime = 20; // time in milliseconds that mot signal is low
int motPulseState = LOW;
long remembermotPulseTime = 0;

void setup() 
{
 Serial.begin(9600);
 pinMode (outPin1, OUTPUT); // for timer
 pinMode (inPin1, INPUT); // for timer
 pinMode(motPulsePin,OUTPUT); // simulates either the CW or CCW motor-on signal
 Timer1.initialize(5000);//sets time between pulses
 Timer1.pwm(outPin1, 50); //sets pulse width

 //lcd.init();                      // initialize the lcd 
  //lcd.init();
  //lcd.backlight();
  //lcd.setCursor(1,0);
  //lcd.print("  CCW:");
  //lcd.setCursor(1,1);
  //lcd.print("   CW:");
  //lcd.setCursor(1,2);
  //lcd.print("COUNT:");
  //lcd.setCursor(1,3);
  //lcd.print("ALARM:");
  //lcd.setCursor(14,2);
//lcd.print(pulseCount);
}

void loop() 
{
if (motPulseState == HIGH) 
 {
  if ((millis() - remembermotPulseTime) >= motPulseonTime)
  {
  motPulseState = LOW;
  Timer1.stop();
  remembermotPulseTime = millis();
  }
 }
else
{
  if ( (millis() - remembermotPulseTime) >= motPulseoffTime)
  {
    motPulseState = HIGH;
    Timer1.start();
    remembermotPulseTime = millis();
  }
}
 digitalWrite(motPulsePin,motPulseState);
 
}

I would appreciate any help I can get here. I have never had a problem incorporating LCD display into sketches.

Hi @n7kd,

which Arduino are you using?

And I assume this is not the full code ( e.g. pulseCount is not declared) ...

I have copied your sketch to Wokwi and de-commented the lcd lines. It works in the simulation as you would like it to do (at least with an Arduino UNO).

See https://wokwi.com/projects/381946778462389249

As the sketch is as yours in post #1 except that I added this declaration

unsigned long pulseCount = 0;

I will not post it here again. The Wokwi test is wired like this
image

I am using an UNO R3 and watching the output of the Timer1 on pin 10 (outPin1) on a 2 channel oscilloscope.

The problem was that I had been using a Mega which has internal pull-ups on the SDA and SLC pins and I forgot to add the pull-ups when I ported the sketch to an UNO. It is weird that this caused the entire sketch to malfunction. But it works now that I added 4.7K pull-ups to the SDA and SLC pins.

Do you know if the LEONARDO has internal pull-ups on the SDA and SLC pins? I can’t seem to find this information online.

Ken

You may go to

https://docs.arduino.cc/hardware/leonardo

and download the schematic diagram...

If I'm not missing something there are no external resistors on the Leonardo board for SCL and SDA

The LiquidCrystal_I2C library is for hd44780 LCD devices that use a PCF8574 i/o expander, typically on a backpack.
Most of the backpacks have pullups on them.

I do have a couple of backpacks without pullups on them that I use for testing my hd44780 library, but they are not very common.

If you install the hd44780 library, it includes a diagnostic tool in the hd44780_I2Cexp i/o class that can be used to test the LCD. It tests the SDA and SCL signal lines and will report if pullups are installed on the signals.
If you do decide to install the hd44780 library (install it from the IDE library manager), please take the time to read the documentation to get a feel for the library structure and its use of i/o classes.
The hd44780 library has extensive documenation and has a wiki on its github page:
https://github.com/duinoWitchery/hd44780/wiki

--- bill

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