#include <HardwareSerial.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
Servo servo1, servo2;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int switchPins[] = {25, 26, 27, 14, 12, 13};
const int numSwitches = 6;
const int ledPins[] = {23, 19, 18, 5, 4, 0, 2, 15};
const int numLeds = 8;
const uint16_t switchAddr[] = {0x5000, 0x5001, 0x5002, 0x5003,
0x5004, 0x5005, 0x5006, 0x5007
};
const uint16_t ledAddr[] = {0x5008, 0x5009, 0x500A, 0x500B,
0x500C, 0x500D, 0x500E, 0x500F
};
const uint16_t numberAddr[] = {0x5010, 0x5011};
const uint16_t gaugeAddr[] = {0x5012, 0x5013};
const uint16_t sliderAddr[] = {0x5014, 0x5015};
unsigned long skr = 0;
uint8_t a0 = 0;
uint8_t b0 = 0;
uint8_t receiveBuffer[32];
uint8_t bufferIndex = 0;
uint8_t ledStates[8] = {0};
uint8_t dwinBuffer[8] = {0x5A, 0xA5, 0x05, 0x82,
0x00, 0x00, 0x00, 0x00
};
uint8_t dwinGraph[14] = {0x5A, 0xA5, 0x0B, 0x82, 0x03,
0x10, 0x5A, 0xA5, 0x01, 0x00,
0x00, 0x01, 0x00, 0x00
};
HardwareSerial SerialDWIN(2);
void setup() {
Serial.begin(9600);
SerialDWIN.begin(9600, SERIAL_8N1, 16, 17);
servo1.attach(32);
servo2.attach(33);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("In1: 0 Ou1: 0");
lcd.setCursor(0, 1);
lcd.print("In2: 0 Ou2: 0");
for (int i = 0; i < numSwitches; i++) {
pinMode(switchPins[i], INPUT_PULLUP);
}
for (int i = 0; i < numLeds; i++) {
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW);
}
Serial.println("ESP32-DWIN siap!");
}
void loop() {
if (millis() - skr > 200) {
skr = millis();
static uint8_t lastStates[numSwitches] = {0};
uint8_t currentState;
for (int i = 0; i < numSwitches; i++) {
currentState = !digitalRead(switchPins[i]);
if (currentState != lastStates[i]) {
sendToDwin(ledAddr[i + 2], 0x00, currentState);
lastStates[i] = currentState;
}
}
uint8_t a = analogRead(34) > 0 ? 0 : 1;
sendToDwin(ledAddr[0], 0x00, a);
uint8_t b = analogRead(35) > 0 ? 0 : 1;
sendToDwin(ledAddr[1], 0x00, b);
int pot1 = analogRead(36);
int meter1 = map(pot1, 0, 4095, 0, 100);
int grafik1 = map(pot1, 0, 4095, 0, 1000);
sendToDwin(numberAddr[0], (pot1 >> 8) & 0xFF, pot1 & 0xFF);
sendToDwin(gaugeAddr[0], (meter1 >> 8) & 0xFF, meter1 & 0xFF);
sendToGraph(0x00, (grafik1 >> 8) & 0xFF, grafik1 & 0xFF);
lcd.setCursor(4, 0);
lcd.print(" ");
lcd.setCursor(4, 0);
lcd.print(pot1);
int pot2 = analogRead(39);
int meter2 = map(pot2, 0, 4095, 0, 100);
int grafik2 = map(pot2, 0, 4095, 0, 1000);
sendToDwin(numberAddr[1], (pot2 >> 8) & 0xFF, pot2 & 0xFF);
sendToDwin(gaugeAddr[1], (meter2 >> 8) & 0xFF, meter2 & 0xFF);
sendToGraph(0x01, (grafik2 >> 8) & 0xFF, grafik2 & 0xFF);
lcd.setCursor(4, 1);
lcd.print(" ");
lcd.setCursor(4, 1);
lcd.print(pot2);
}
while (SerialDWIN.available()) {
receiveBuffer[bufferIndex] = SerialDWIN.read();
if (bufferIndex == 0 && receiveBuffer[0] != 0x5A) {
bufferIndex = 0;
continue;
}
if (bufferIndex == 1 && receiveBuffer[1] != 0xA5) {
bufferIndex = 0;
continue;
}
bufferIndex++;
if (bufferIndex >= 9) {
if (receiveBuffer[3] == 0x83) {
processDwinCommand();
}
bufferIndex = 0;
}
}
}
void sendToDwin(uint16_t addr, uint8_t data1, uint8_t data2) {
dwinBuffer[4] = highByte(addr);
dwinBuffer[5] = lowByte(addr);
dwinBuffer[6] = data1;
dwinBuffer[7] = data2;
SerialDWIN.write(dwinBuffer, 8);
SerialDWIN.flush();
}
void sendToGraph(uint8_t channel, uint8_t data1, uint8_t data2) {
dwinGraph[10] = channel;
dwinGraph[12] = data1;
dwinGraph[13] = data2;
SerialDWIN.write(dwinGraph, 14);
SerialDWIN.flush();
}
void processDwinCommand() {
uint16_t vpAddress = (receiveBuffer[4] << 8) | receiveBuffer[5];
uint8_t data = receiveBuffer[8];
for (int i = 0; i < numLeds; i++) {
if (vpAddress == switchAddr[i]) {
digitalWrite(ledPins[i], data ? HIGH : LOW);
break;
}
}
if (vpAddress == sliderAddr[0]) {
lcd.setCursor(13, 0);
lcd.print(" ");
lcd.setCursor(13, 0);
lcd.print(data);
servo1.write(data);
delay(15);
}
if (vpAddress == sliderAddr[1]) {
lcd.setCursor(13, 1);
lcd.print(" ");
lcd.setCursor(13, 1);
lcd.print(data);
servo2.write(data);
delay(15);
}
}2 OUTPUT ANALOG:
- SERVO1
- SERVO2
2 INPUT ANALOG:
- POTENSIO PUTAR
- POTENSIO GESER
8 OUTPUT DIGITAL
- LED BAR GRAPH
8 INPUT DIGITAL
- DIP SWITCH
TAMPILAN LCD I2C
- INPUT & OUTPUT ANALOG