First post here and I'm new to Arduino so please don't be too harsh.
I've started a project that will use an M5Dial as an encoder which will provide an output via it's I2C interface. I've set up the encoder with a display on the LCD which works really nicely and have moved on to the I2C output which is where I have hit an issue.
When using the dial without the I2C interface active it works well and is very responsive, the dial position is presented on the screen as a series of dots around the outside and a number in the middle. To test the I2C output I have connected an LCD display which just shows the dial position, it works but the extra code dulls the responsiveness of the dial, as if the time taken to write out the data to I2C means that not every dial movement is captured.
I've tried running the output within the main loop and in it's own loop (as it is below) and the results are the same. Writing out to I2C seems like it should be a minimal overhead but clearly it isn't, or I'm doing something wrong.
Any feedback on my code and why I am seeing this issue would be greatly appreciated.
#include "M5Dial.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int x[] = { 33, 23, 20, 23, 33, 49, 70, 94, 120, 146, 170, 191, 207, 217, 220, 217, 207 };
int y[] = { 170, 146, 120, 94, 70, 49, 33, 23, 20, 23, 33, 49, 70, 94, 120, 146, 170 };
int dots;
int dialPosition = 0;
int dialOld = 1;
int exactPosition = 0;
int exactOld = 1;
bool inRange(int val, int minimum, int maximum)
{
return ((minimum <= val) && (val <= maximum));
}
void setup() {
auto cfg = M5.config();
M5Dial.begin(cfg, true, false);
M5Dial.Display.setTextColor(GREEN);
M5Dial.Display.setTextDatum(middle_center);
M5Dial.Display.setTextFont(&fonts::Font7);
M5Dial.Display.setTextSize(1.5);
lcd.init();
lcd.backlight();
Serial.begin(9600);
}
void loop() {
M5Dial.update();
exactPosition = M5Dial.Encoder.read();
if (exactPosition < 0){
exactPosition = 0;
M5Dial.Encoder.write(exactPosition);
}
if (exactPosition > 68){
exactPosition = 68;
M5Dial.Encoder.write(exactPosition);
}
if (exactOld != exactPosition){
Serial.println(exactPosition);
exactOld = exactPosition;
}
dialPosition = exactPosition/4;
if (dialPosition != dialOld && inRange(dialPosition, 0, 16) ) {
M5Dial.Speaker.tone(800, 50);
M5Dial.Display.fillScreen(BLACK);
M5Dial.Display.drawString(String(dialPosition),
M5Dial.Display.width() / 2,
M5Dial.Display.height() / 2);
dots = dialPosition;
while (dots > -1){
if ( inRange(dots, 0, 2) ){
M5Dial.Lcd.fillCircle(x[dots],y[dots],8,BLUE);
}else if ( inRange(dots, 3, 13) ){
M5Dial.Lcd.fillCircle(x[dots],y[dots],8,YELLOW);
}else if ( dots > 13 ){
M5Dial.Lcd.fillCircle(x[dots],y[dots],8,RED);
}
dots = (dots - 1);
}
dialOld = dialPosition;
LCD();
}
if (M5Dial.BtnA.wasPressed()) {
M5Dial.Encoder.readAndReset();
}
}
void LCD(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Exact Pstn: ");
lcd.print(exactPosition);
lcd.setCursor(0, 1);
lcd.print("Dial Pstn: ");
lcd.print(dialPosition);
}