/*
Forum: https://forum.arduino.cc/t/lcd-display-timer1-problem/1191499
Wokwi: https://wokwi.com/projects/381947807391289345
*/
#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;
unsigned long pulseCount = 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.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);
}
long motPulseTime = motPulseoffTime;
void loop() {
if ((millis() - remembermotPulseTime) >= motPulseTime)
{
remembermotPulseTime = millis();
if (motPulseState == HIGH)
{
motPulseTime = motPulseoffTime;
Timer1.stop();
}
else
{
motPulseTime = motPulseonTime;
Timer1.start();
}
motPulseState = !motPulseState;
}
digitalWrite(motPulsePin, motPulseState);
}
/*
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);
}
*/