#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RotaryEncoder.h>
#define PIN_IN1 17
#define PIN_IN2 16
int ROTARYSTEPS = 1;
int ROTARYMIN = 0;
int ROTARYMAX = 999;
RotaryEncoder encoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::TWO03);
#define SCREEN_WIDTH 128 // OLED 寬度像素
#define SCREEN_HEIGHT 32 // OLED 高度像素
// 設定OLED
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int Pos = 0;
void testdrawstyles(int x,int x2) {
display.clearDisplay(); // 清除畫面
String y = String(x); // 將整數轉換成字串
if (y.length() < 3) { // 如果字串長度小於3
while (y.length() < 3) { // 在字串前面補0直到長度為3
y = "0" + y;
}
}
// Serial.println(y); // 輸出轉換後的字串
display.setTextSize(2.5); // 設定文字大小
display.setTextColor(1); // 1:OLED預設的顏色(這個會依該OLED的顏色來決定)
display.setCursor(5, 0); // 設定起始座標
display.print(y); // 要顯示的字串
//display.startscrollleft(0x00, 0x01);
y = String(x2); // 將整數轉換成字串
if (y.length() < 3) { // 如果字串長度小於3
while (y.length() < 3) { // 在字串前面補0直到長度為3
y = "0" + y;
}
}
display.setTextSize(2.5); // 設定文字大小
display.setTextColor(1); // 1:OLED預設的顏色(這個會依該OLED的顏色來決定)
display.setCursor(5, 16); // 設定起始座標
display.print(y); // 要顯示的字串
display.display(); // 要有這行才會把文字顯示出來
}
TaskHandle_t Task1;
void setup() {
Serial.begin(9600);
encoder.setPosition(0);
// 偵測是否安裝好OLED了
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // 一般1306 OLED的位址都是0x3C
Serial.println(F("SSD1306 allocation failed"));
for (;;)
; // Don't proceed, loop forever
}
display.clearDisplay(); // 清除畫面
// put your setup code here, to run once:
xTaskCreatePinnedToCore(
loop2, // name of the task function
"buttonCheck", // name of the task
10000, // memory assigned for the task
NULL, // parameter to pass if any
1, // priority of task, starting from 0(Highestpriority) *IMPORTANT*( if set to 1 and there is no activity in your 2nd loop, it will reset the esp32)
&Task1, // Reference name of taskHandle variable
1); // choose core (0 or 1)
}
void loop() {
// put your main code here, to run repeatedly:
testdrawstyles(Pos,0);
}
void loop2(void* parameter) {
for (;;) {
encoder.tick();
Pos = encoder.getPosition();
if (Pos < ROTARYMIN) {
encoder.setPosition(ROTARYMIN);
Pos = ROTARYMIN;
} else if (Pos > ROTARYMAX) {
encoder.setPosition(ROTARYMAX);
Pos = ROTARYMAX;
}
}
}Loading
esp32-s2-devkitm-1
esp32-s2-devkitm-1