#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
// Chọn loại màn hình OLED 1.3 inch
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0);
#include <RotaryEncoder.h>
#define PIN_IN1 3
#define PIN_IN2 2
#define PIN_SW 4
RotaryEncoder encoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::TWO03);
int BT, counter=0;
int macdinh = 1;
int dem_rota = 0;
int congtru_tong = 0; int congtru_menu1 = 0; int congtru_menu2 = 0;
int demmenu1 = 0; int demmenu2 = 0;int demtong = 0; //Đém tổng là giá trị nút nhấn Encoder
void setup() {
Serial.begin(115200);
u8g2.begin();
u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2.setColorIndex(1);
pinMode(PIN_SW,INPUT_PULLUP);
}
void loop() {
BT = digitalRead(PIN_SW); //Serial.println(BT);
if(BT != macdinh){
if(BT == 0){
counter++;
}
macdinh = BT;
} //Serial.print(counter); Serial.print(" ");
static int pos = 0;
encoder.tick();
int newPos = encoder.getPosition();
if (pos != newPos) {
// Serial.print(" dir:"); Serial.println((int)(encoder.getDirection()));
pos = newPos;
Serial.print("pos:"); Serial.println(newPos);
}
//if(counter == 1){
// dem_rota=newPos;
//Serial.println(dem_rota);
// if(dem_rota>3) dem_rota=0;
// congtru_tong = dem_rota;
// if (congtru_tong > 3) congtru_tong = 0;
// menu();
//}
}
void menu(){
if(congtru_tong=0){
u8g2.clearBuffer();
u8g2.drawStr(5, 10, "Menu1");
u8g2.drawStr(5, 20, "Menu2");
u8g2.drawStr(5, 30, "Menu3");
u8g2.sendBuffer();
}
if(congtru_tong=1){
u8g2.clearBuffer();
u8g2.drawStr(5, 10, ">Menu1");
u8g2.drawStr(5, 20, "Menu2");
u8g2.drawStr(5, 30, "Menu3");
u8g2.sendBuffer();
}
if(congtru_tong=2){
u8g2.clearBuffer();
u8g2.drawStr(5, 10, "Menu1");
u8g2.drawStr(5, 20, ">Menu2");
u8g2.drawStr(5, 30, "Menu3");
u8g2.sendBuffer();
}
if(congtru_tong=3){
u8g2.clearBuffer();
u8g2.drawStr(5, 10, "Menu1");
u8g2.drawStr(5, 20, "Menu2");
u8g2.drawStr(5, 30, ">Menu3");
u8g2.sendBuffer();
}
}
void Measurement(){
u8g2.clearBuffer();
u8g2.drawStr(5, 10, "Distance:");
u8g2.sendBuffer();
}
void Encoder() {
static int pos = 0;
encoder.tick();
int newPos = encoder.getPosition();
if (pos != newPos) {
// Serial.print(" dir:"); Serial.println((int)(encoder.getDirection()));
u8g2.clearBuffer();
u8g2.drawStr(5, 30, "Position:");
u8g2.drawStr(65, 30, String(newPos).c_str());
u8g2.sendBuffer();
pos = newPos;
Serial.print("pos:"); Serial.println(newPos);
}
}