Compare commits

...

10 Commits

Author SHA1 Message Date
cde6a0ee9e fix just 2026-05-27 20:12:26 +03:00
5dad73affd update to gyver EncButton v3.x 2026-05-27 20:09:39 +03:00
d0ad3d9236 refactor 2026-05-26 19:50:52 +03:00
20cc225d08 refactor 2023-11-08 23:03:15 +03:00
8a7db7450e fix-bug: add pressure measurement 2022-10-03 17:35:23 +03:00
a4a79d7f91 average pressure v2 2022-08-11 22:34:59 +03:00
1881b830b3 average pressure 2022-08-11 22:10:20 +03:00
Kan
8cd22d1cf1
Update README.md 2021-12-21 23:19:43 +03:00
53de234870 ref simple 2021-09-22 16:24:28 +03:00
270b93ab3c fix bug, up perfomance 2021-09-22 02:06:35 +03:00
4 changed files with 123 additions and 73 deletions

View File

@ -55,4 +55,10 @@ Arduino Controller for water pump
* [**EncButton**](https://github.com/GyverLibs/EncButton)
## ToDO
* create board on easyeda
* LGT8F328P
* average pressure
<h1 align="center"><a href="#top"></a></h1>

View File

@ -13,6 +13,12 @@
#define DIO 7
#if DEBUG
#define DEBUG_MSG(x) Serial.println(x)
#else
#define DEBUG_MSG(x)
#endif
// ====Consts====
#define MIN_PRESSURE 60
#define MAX_PRESSURE 800
@ -26,10 +32,10 @@
int pressure, pressure_low, pressure_high;
bool is_on_pump, is_on_display, display_cur_pressure;
unsigned long last_time1, last_time2, last_time3;
unsigned long last_time1, last_time2, last_time3, last_time_pressure;
const int NUM_READ = 3;
EncButton<EB_TICK, S1, S2, KEY> enc;
EncButton eb(S1, S2, KEY);
GyverTM1637 disp(CLK, DIO);
@ -43,8 +49,33 @@ int get_constrained_pressure_high(int pressure_high_local) {
return constrain(pressure_high_local, pressure_low+10, MAX_PRESSURE);
}
// растянутое среднее арифметическое
int midArifm2(int newVal) {
static byte counter = 0; // счётчик
static int prevResult = 0; // хранит предыдущее готовое значение
static int sum = 0; // сумма
sum += newVal; // суммируем новое значение
counter++; // счётчик++
if (counter == NUM_READ) { // достигли кол-ва измерений
prevResult = sum / NUM_READ; // считаем среднее
sum = 0; // обнуляем сумму
counter = 0; // сброс счётчика
}
return prevResult;
}
void setup() {
// Serial.begin(9600);
#if DEBUG
Serial.begin(9600);
#endif
DEBUG_MSG("pressure");
DEBUG_MSG(',');
DEBUG_MSG("low");
DEBUG_MSG(',');
DEBUG_MSG("high");
DEBUG_MSG(',');
DEBUG_MSG("status");
pinMode(relay_port, OUTPUT);
@ -55,32 +86,28 @@ void setup() {
disp.clear();
disp.brightness(5); // яркость, 0 - 7 (минимум - максимум)
disp.clear();
}
void loop() {
enc.tick();
if (enc.isTurn()) {
eb.tick();
if (eb.turn()) {
disp.clear();
if (enc.isRight()) {
if (eb.right()) {
pressure_low = get_constrained_pressure_low(pressure_low+10);
disp.displayInt(pressure_low);
disp.displayByte(0, _L);
}
if (enc.isLeft()) {
if (eb.left()) {
pressure_low = get_constrained_pressure_low(pressure_low-10);
disp.displayInt(pressure_low);
disp.displayByte(0, _L);
}
if (enc.isRightH()) {
if (eb.rightH()) {
pressure_high = get_constrained_pressure_high(pressure_high+10);
disp.displayInt(pressure_high);
disp.displayByte(0, _H);
}
if (enc.isLeftH()) {
if (eb.leftH()) {
pressure_high = get_constrained_pressure_high(pressure_high-10);
disp.displayInt(pressure_high);
disp.displayByte(0, _H);
@ -91,7 +118,7 @@ void loop() {
}
// нажать на энкодер - показать текущее давление
if (enc.isClick()) {
if (eb.click()) {
display_cur_pressure = true;
is_on_display = true;
last_time2 = millis();
@ -99,7 +126,7 @@ void loop() {
// Если энкодер зажат то записать текущие пороги в энергонезависимую память
if (enc.isHolded()) {
if (eb.hold()) {
EEPROM.put(0, pressure_low);
EEPROM.put(2, pressure_high);
disp.displayByte(_S, _A, _U, _E);
@ -109,39 +136,41 @@ void loop() {
// Датчик давления 0 - 1000 0 - 10 атмосфер
pressure = analogRead(pressure_port);
pressure = midArifm2(pressure);
// Если прошло 7 сек с момента взаимодействия с энкодером, то отключить дисплей
if (millis() - last_time2 > 7000) {
if (is_on_display) {
last_time2 = millis();
is_on_display = false;
disp.clear();
}
}
if (millis() - last_time1 > 350) {
last_time1 = millis();
if (display_cur_pressure & is_on_display) {
disp.displayInt(pressure);
}
}
// Если текущее давление ниже нижнего порога - включить насос
if (pressure < pressure_low) {
if (!is_on_pump) {
digitalWrite(relay_port, HIGH);
is_on_pump = true;
}
if (millis() - last_time1 > 350) {
last_time1 = millis();
if (display_cur_pressure & is_on_display) {
disp.displayInt(pressure);
}
}
// Иначе если давление выше верхнего порога - выключить насос
else if (pressure > pressure_high) {
if (is_on_pump) {
digitalWrite(relay_port, LOW);
is_on_pump = false;
}
// измерение каждые 1500 мс
if (millis() - last_time_pressure > 1500) {
last_time_pressure = millis();
// Если текущее давление ниже нижнего порога - включить насос
if (pressure < pressure_low) {
if (!is_on_pump) {
digitalWrite(relay_port, HIGH);
is_on_pump = true;
}
}
// Иначе если давление выше верхнего порога - выключить насос
else if (pressure > pressure_high) {
if (is_on_pump) {
digitalWrite(relay_port, LOW);
is_on_pump = false;
}
}
}
// Serial.print(pressure_low);
// Serial.print(',');
// Serial.println(pressure_high);
DEBUG_MSG(pressure);
DEBUG_MSG(',');
DEBUG_MSG(pressure_low);
DEBUG_MSG(',');
DEBUG_MSG(pressure_high);
DEBUG_MSG(',');
DEBUG_MSG(is_on_pump*100);
}

View File

@ -1,46 +1,41 @@
#define pressure_Aport 0
#define relay_Dport 5
#define pressure_port A0
#define relay_port 5
#define DEBUG 0
int pressure;
int pressure_min = 180;
int pressure_max = 350;
bool flag;
unsigned long last_time_pressure;
void setup() {
Serial.begin(9600);
pinMode(relay_Dport, OUTPUT);
if (DEBUG) Serial.begin(9600);
pinMode(relay_port, OUTPUT);
}
void loop() {
pressure = analogRead(pressure_port);
if (DEBUG) Serial.println(pressure);
pressure = analogRead(pressure_Aport);
Serial.println(pressure);
if (pressure < pressure_min) {
if (!flag) {
digitalWrite(relay_Dport, HIGH);
flag = true;
}
} else {
if (millis() - last_time_pressure > 700) {
last_time_pressure = millis();
if (pressure < pressure_min) {
if (!flag) {
digitalWrite(relay_port, HIGH);
flag = true;
}
} else {
if (pressure > pressure_max) {
if (flag) {
digitalWrite(relay_Dport, LOW);
digitalWrite(relay_port, LOW);
flag = false;
}
}
}
}
}
}

20
justfile Executable file
View File

@ -0,0 +1,20 @@
#!/usr/bin/env -S just --justfile
PORT := env("PORT", "/dev/ttyACM0")
BOARD := env("BOARD", "lgt8fx:avr:328")
[working-directory: 'firmware/pump_encoder']
build:
arduino-cli compile --fqbn "{{BOARD}}"
[working-directory: 'firmware/pump_encoder']
upload:
arduino-cli upload --port "{{PORT}}" --fqbn "{{BOARD}}"
[working-directory: 'firmware/pump_encoder']
install-libs:
arduino-cli lib install "EncButton@3.7.3"
arduino-cli lib install "GyverTM1637@1.4.2"