Compare commits
10 Commits
9b52482681
...
cde6a0ee9e
| Author | SHA1 | Date | |
|---|---|---|---|
| cde6a0ee9e | |||
| 5dad73affd | |||
| d0ad3d9236 | |||
| 20cc225d08 | |||
| 8a7db7450e | |||
| a4a79d7f91 | |||
| 1881b830b3 | |||
|
|
8cd22d1cf1 | ||
| 53de234870 | |||
| 270b93ab3c |
@ -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>
|
||||
|
||||
@ -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,15 +136,7 @@ void loop() {
|
||||
|
||||
// Датчик давления 0 - 1000 0 - 10 атмосфер
|
||||
pressure = analogRead(pressure_port);
|
||||
|
||||
// Если прошло 7 сек с момента взаимодействия с энкодером, то отключить дисплей
|
||||
if (millis() - last_time2 > 7000) {
|
||||
if (is_on_display) {
|
||||
last_time2 = millis();
|
||||
is_on_display = false;
|
||||
disp.clear();
|
||||
}
|
||||
}
|
||||
pressure = midArifm2(pressure);
|
||||
|
||||
if (millis() - last_time1 > 350) {
|
||||
last_time1 = millis();
|
||||
@ -126,6 +145,10 @@ void loop() {
|
||||
}
|
||||
}
|
||||
|
||||
// измерение каждые 1500 мс
|
||||
if (millis() - last_time_pressure > 1500) {
|
||||
last_time_pressure = millis();
|
||||
|
||||
// Если текущее давление ниже нижнего порога - включить насос
|
||||
if (pressure < pressure_low) {
|
||||
if (!is_on_pump) {
|
||||
@ -141,7 +164,13 @@ void loop() {
|
||||
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);
|
||||
}
|
||||
|
||||
@ -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 (millis() - last_time_pressure > 700) {
|
||||
last_time_pressure = millis();
|
||||
|
||||
if (pressure < pressure_min) {
|
||||
if (!flag) {
|
||||
digitalWrite(relay_Dport, HIGH);
|
||||
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
20
justfile
Executable 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"
|
||||
Loading…
x
Reference in New Issue
Block a user