Compare commits

..

No commits in common. "cde6a0ee9eb239cc779903c3c552f94937e4a39e" and "9b52482681fc89d551eb5f7d49adff79b6179572" have entirely different histories.

4 changed files with 73 additions and 123 deletions

View File

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

View File

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

View File

@ -1,20 +0,0 @@
#!/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"