Arduino Quick Start
APIs and example programs related to the Dial rotary encoder.
#include "M5Dial.h"
long oldPosition = -999;
void setup() {
auto cfg = M5.config();
M5Dial.begin(cfg, true, false); // encoder, RFID
M5Dial.Display.setTextColor(GREEN);
M5Dial.Display.setTextDatum(middle_center);
M5Dial.Display.setTextFont(&fonts::Orbitron_Light_32);
M5Dial.Display.setTextSize(2);
}
void loop() {
M5Dial.update();
long newPosition = M5Dial.Encoder.read();
if (newPosition != oldPosition) {
M5Dial.Speaker.tone(8000, 20);
M5Dial.Display.clear();
M5Dial.Display.drawString(String(newPosition),
M5Dial.Display.width() / 2,
M5Dial.Display.height() / 2);
oldPosition = newPosition;
}
if (M5Dial.BtnA.wasPressed()) {
M5Dial.Encoder.readAndReset();
}
if (M5Dial.BtnA.pressedFor(5000)) {
M5Dial.Encoder.write(100);
}
}
After compiling and uploading, the screen will display a number that follows the changes of the rotary encoder. Pressing the button briefly will reset the number to 0, and pressing and holding the button for 5 seconds will reset the number to 100.
Function Prototype:
void begin();
Description:
When calling M5Dial.begin()
, you can set the parameter enableEncoder
to true
to initialize it together.
M5Dial.begin(m5::M5Unified::config_t cfg, bool enableEncoder, bool enableRFID);
Input Parameters:
Return Value:
Function Prototype:
int32_t read();
Description:
Input Parameters:
Return Value:
Function Prototype:
void write(int32_t p);
Description:
Input Parameters:
Return Value:
Function Prototype:
int32_t readAndReset();
Description:
Input Parameters:
Return Value: