Dear Forum,
I've never encountered a problem like this before. Therefore I'm here to seek your wisdom. Please don't be too harsh on me, it's my first post and I'll give it my best, I promise.
What do I want to do?
I want to use a rotary encoder, the KY040 to be precise (with all Pullup Resistors in place), to change a counter. This counter will then be used to see in which direction the encoder was turned and after knowing that this information will lead to a number added to float m_material (so you can change how much material is needed).
m_material is then divided by my calculated mass flow d_m_gear (float) to give me the time needed to transport the material.
This time and the mass of the material (m_material) shall be shown on a LCD Display with an I2C adapter soldered on it.
But right now, only the counter and the LCD are important. (rest is already done, tested and working!)
Sidenote: The Output of the Rotary Encoder is extremely bouncy. Therefore I'm using a digital filter I found on another forum to debounce it. Link is in the code.
How do I want to do it?
This is a part of my code. I have checked the rest and found the mistake, but not the solution. I have cut all unnecessary stuff away and only left the lines that create the problem. More to the problem under the code:
#include <Wire.h>Â Â Â Â Â Â Â Â Â Â Â //insert Wire library for I2C
#include <LiquidCrystal_I2C.h>Â Â Â Â Â //insert LiquidCrystal_I2C library
LiquidCrystal_I2C lcd(0x27, 16, 2);Â Â //define LCD type (it's a I2C LCD btw) and address
const int DT = 5;Â Â Â Â Â Â Â Â Â Â Â //Encoder Data PIN
const int CLK = 6;Â Â Â Â Â Â Â Â Â Â Â //Encoder Clock PIN
float d_m_gear = 0.48;Â Â Â Â Â Â Â Â Â //mass flow of the gear
float m_material = 3.0;Â Â Â Â Â Â Â Â //mass to be transported (later adjustable by turning the rotary encoder)
float t_ms = 0;Â Â Â Â Â Â Â Â Â Â Â Â //time to transport all material from A to B
static uint16_t state = 0;Â Â Â Â Â Â Â //static unsigned portable 16 Bit Integer
                    //used for Digital Debouncing of the Rotary Encoders Feedback
static uint16_t counter = 0;Â Â Â Â Â Â //static unsigned portable 16 Bit Integer for Counting
////////////////////////////////////////
void setup()
{
 //make LCD ready
 lcd.init();               //start LCD
 lcd.backlight();             //LCD backlight on
 //PINs of the KY040 (left SW PIN out)
 pinMode(CLK,  INPUT);         //read PIN Clock
 pinMode(DT,  INPUT);         //read PIN Data
 //feedback for monitor
 Serial.begin(9600);           //start Serial
 Serial.println("Rotary Encoder KY-040"); //print Encoder Type
 Serial.println(counter);         //print start value of counter
}
////////////////////////////////////////
void loop()
{
//// filter for the rotary encoder
 delayMicroseconds(100);       //simulate action
Â
 state=(state<<1) | digitalRead(CLK) | 0xe000;
 if (state==0xf000) //filter is explained here https://www.best-microcontroller-projects.com/rotary-encoder.html
 {
  state=0x0000;
  if(digitalRead(DT))
   counter++;           //static uint16_t counter +1
  else
   counter--;           //static uint16_t counter -1
  Serial.println(counter);     //print new counter value into the monitor
 }
 t_ms = m_material/d_m_gear;     //calculate time in ms
// if the following sequence till the end is uncommented the counter does not work anymore
// no matter how much you turn or what you do if uncommented, no new value will be printed
// into the monitor
////// standard display
//Â lcd.setCursor(0,0);Â Â Â Â Â Â Â Â //LCD textposition
//Â lcd.print("some string text");Â Â //LCD text
//Â Â
//Â lcd.setCursor(2,1);Â Â Â Â Â Â Â Â //LCD textposition
//Â lcd.print(m_material);Â Â Â Â Â Â //LCD text
//Â lcd.print("g");Â Â Â Â Â Â Â Â Â Â //LCD text
//Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
//Â lcd.setCursor(9,1);Â Â Â Â Â Â Â Â //LCD textposition
//Â lcd.print(t_ms);Â Â Â Â Â Â Â Â Â //LCD text
//Â lcd.print("s");Â Â Â Â Â Â Â Â Â Â //LCD text
}
What and where is the problem?
If you load the code like this and have the KY040 and the LCD I2C display connected at the right PINs (schematics of the complete project are attached) the serial monitor will work like a charm and show the changing counter.
Buuuuut if you uncomment the area with the lcd.print and lcd.setCursor commands the filter for the KY040 and the counter won't work anymore.
I have no idea how. This is why I'm here. Maybe it has something to do with the static portable 16 bit unsigned integer, but I just don't know. What I know is that the problem is not generated by the "delayMicroseconds();" line. Already tested uncommenting it.
Until now I was always able to debug by myself, but this time I'm just not smart enough. I'm sorry for taking your time and hope you may be able to help me.
Thank you all very much.