#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "HX711.h"
#define Pin_KNOB2_SIG 2
#define Pin_PWM2_Write 25
#define Pin_RC2_Read 35
#define Pin_SERVO2_PWM 27
#define Pin_HX711_SCK 18
#define Pin_HX711_DT 19
LiquidCrystal_I2C LCD(0x27,16,2); // 0x3F
HX711 HX711;
unsigned long Data[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
void setup() {
Serial.begin(9600);
pinMode(Pin_RC2_Read, INPUT);
pinMode(Pin_PWM2_Write, OUTPUT);
pinMode(Pin_SERVO2_PWM, OUTPUT);
LCD_start();
I2C_start();
LEDC_set();
HX711.begin(Pin_HX711_DT, Pin_HX711_SCK);}
void loop() {
KNOB_read(); // remove
PWM_write(); // remove
RC_read();
DATA_process();
SERVO_write();
HX711_read();
LCD_show();}
void LCD_print(int type, String text, int value, int col, int row) {
LCD.display();
LCD.setCursor(col, row);
switch (type) {
case 1: LCD.println(value, DEC); break;
case 2: LCD.println(value, HEX); break;
case 3: LCD.clear(); LCD.println(value); break;
case 4: LCD.println(text); break;
case 5: LCD.clear(); LCD.println(text); break;
case 6: LCD.clear(); LCD.noDisplay();}}
void LCD_start() {
LCD.init();
LCD.clear();
LCD.backlight();
LCD_print(4, "LCD ", 0, 6, 0);
LCD_print(4, "Starting >", 0, 6, 1);
for (int i = 0; i <= 3; i++) {
LCD_print(1, "", 3 - i, 15, 1);
delay(990);}
LCD_print(6, "", 0, 0, 0);}
void I2C_start() {
Wire.begin();
LCD_print(5, "I2C scanning ...", 0, 0, 0);
delay(2000);
for (byte j = 8; j < 120; j++) {
Wire.beginTransmission (j);
if (Wire.endTransmission () == 0) {
LCD_print(4, "Addr :", 0, 0, 1);
LCD_print(1, "", j, 6, 1);
LCD_print(4, " (0x", 0, 9, 1);
LCD_print(2, "", j, 13, 1);
LCD_print(4, ")", 0, 15, 1);
delay(2000);}}
LCD_print(6, "", 0, 0, 0);}
void LEDC_set() {
// ledcAttach(pin[#], freq[Hz], res[bit])
ledcAttach(Pin_PWM2_Write, 50, 8);
ledcAttach(Pin_SERVO2_PWM, 50, 8);}
void KNOB_read() {
Data[1] = analogRead(Pin_KNOB2_SIG);}
void PWM_write() {
// 508 us (0 deg); 2500 us (180 deg)
Data[2] = map(Data[1], 0, 4095, 508, 2500);
Data[3] = map(Data[2], 0, 20000, 0, 255);
// ledcWrite(pin[#], duty[0 - 255])
ledcWrite(Pin_PWM2_Write, Data[3]);}
void RC_read() {
Data[4] = pulseIn(Pin_RC2_Read, HIGH);}
void DATA_process() {
Data[5] = Data[4];}
void SERVO_write() {
Data[6] = map(Data[5], 0, 20000, 0, 255);
ledcWrite(Pin_SERVO2_PWM, Data[6]);}
void HX711_read() {
Data[7] = HX711.get_units(10);
// Change accordingly to calibration data
Data[8] = map(Data[7], 0, 2100, 0, 5000);}
void LCD_show() {
LCD_print(3, "", Data[1], 0, 0);
LCD_print(1, "", Data[2], 5, 0);
LCD_print(1, "", Data[4], 10, 0);
LCD_print(1, "", Data[6], 0, 1);
LCD_print(1, "", Data[7], 5, 1);
LCD_print(1, "", Data[8], 10, 1);}
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL
pot1:VCC
pot1:SIG
pot1:GND
cell1:VCC
cell1:DT
cell1:SCK
cell1:GND
servo1:GND
servo1:V+
servo1:PWM