From a0960eb5c694ad8490a07c44bca9415c21b61ac5 Mon Sep 17 00:00:00 2001 From: Fisch Date: Wed, 2 Mar 2022 19:58:23 +0100 Subject: [PATCH] add find end mode for buttons on startup --- platformio.ini | 2 +- src/main.cpp | 64 +++++++++++++++++++++++++++++++++++++------------- 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/platformio.ini b/platformio.ini index f409bd2..6a9997e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -9,7 +9,7 @@ ; https://docs.platformio.org/page/projectconf.html [env:d1_mini] -platform = espressif8266 +platform = espressif8266 @ 2.6.3 board = d1_mini framework = arduino diff --git a/src/main.cpp b/src/main.cpp index c792d40..40e04e1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -29,12 +29,15 @@ HomieNode blind2Node("blindr", "Blind Right", "blind"); //paramters: topic, $nam #define BUTTON_DEBOUNCE 200 #define PIN_BUTTON1 D5 #define PIN_BUTTON2 D6 +#define BUTTON_TIME_HOLD_FIND_END 5000 struct button{ uint8_t pin; unsigned long last_time_read=0; bool down=false; bool changed=false; bool manual_drive_direction=false; + unsigned long millisup=0; + unsigned long millisdown=0; }; button button1; button button2; @@ -414,35 +417,61 @@ void classifySensorValue(blindmodel &blind) { void checkButton(button &btn) { btn.changed=false; if (millis() > btn.last_time_read + BUTTON_DEBOUNCE) { + bool new_pin_button_down=!digitalRead(btn.pin); if (btn.down != new_pin_button_down) { //changed btn.down = new_pin_button_down; //update btn.changed=true; - btn.last_time_read=millis(); //delay next check - } + if (btn.down) { //remember time when button was pressed or released + btn.millisdown=millis(); + }else{ + btn.millisup=millis(); + } + } + btn.last_time_read=millis(); //delay next check } } void manualMoveHandler(button &btn, blindmodel &blind) { if (btn.changed) { - if (btn.down) { //changed to pressed - blind.mode=MODE_MANUAL; - if (btn.manual_drive_direction) { //drive up - //M1.setmotor( _CW, 100); - blind.speed=-100; - //Serial.print("CW PWM: "); - }else{ //drive down - blind.speed=100; - //Serial.print("CCW PWM: "); + + if (blind.error==ERRORCODE_UNDEFINED_POSITION) { + + if (!btn.down && (btn.millisup-btn.millisdown)>BUTTON_TIME_HOLD_FIND_END) { //changed to released + + blind.mode = MODE_FIND_END; + blind.mode_find_end_state=0; //reset mode find state + blind.position_last_classchange=blind.position; //otherwise error trips immediately + + blind.error=0; //reset + Serial.println("Reset and find end triggered"); + } + + }else{ //not undefined error + if (btn.down) { //changed to pressed + + blind.mode=MODE_MANUAL; + if (btn.manual_drive_direction) { //drive up + //M1.setmotor( _CW, 100); + blind.speed=-100; + Serial.println("CW manual "); + }else{ //drive down + blind.speed=100; + Serial.println("CCW manual "); + } + btn.manual_drive_direction=!btn.manual_drive_direction; //switch direction every new press + + }else{ //changed to released + //Serial.println("Motor STOP"); + blind.mode=MODE_IDLE; + blind.speed=0; } - btn.manual_drive_direction=!btn.manual_drive_direction; //switch direction every new press - }else{ //changed to released - //Serial.println("Motor STOP"); - blind.mode=MODE_IDLE; - blind.speed=0; } + } + + } void readSensor(blindmodel &blind, int value, HomieNode &node) @@ -746,6 +775,9 @@ String modeNumToString(uint8_t modenum){ case MODE_ERROR: return "MODE_ERROR"; break; + case MODE_MANUAL: + return "MODE_MANUAL"; + break; } return "UNDEF"; }