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.
