add find end mode for buttons on startup

This commit is contained in:
interfisch 2022-03-02 19:58:23 +01:00
parent 6cd5c9c9e0
commit a0960eb5c6
2 changed files with 49 additions and 17 deletions

View File

@ -9,7 +9,7 @@
; https://docs.platformio.org/page/projectconf.html ; https://docs.platformio.org/page/projectconf.html
[env:d1_mini] [env:d1_mini]
platform = espressif8266 platform = espressif8266 @ 2.6.3
board = d1_mini board = d1_mini
framework = arduino framework = arduino

View File

@ -29,12 +29,15 @@ HomieNode blind2Node("blindr", "Blind Right", "blind"); //paramters: topic, $nam
#define BUTTON_DEBOUNCE 200 #define BUTTON_DEBOUNCE 200
#define PIN_BUTTON1 D5 #define PIN_BUTTON1 D5
#define PIN_BUTTON2 D6 #define PIN_BUTTON2 D6
#define BUTTON_TIME_HOLD_FIND_END 5000
struct button{ struct button{
uint8_t pin; uint8_t pin;
unsigned long last_time_read=0; unsigned long last_time_read=0;
bool down=false; bool down=false;
bool changed=false; bool changed=false;
bool manual_drive_direction=false; bool manual_drive_direction=false;
unsigned long millisup=0;
unsigned long millisdown=0;
}; };
button button1; button button1;
button button2; button button2;
@ -414,35 +417,61 @@ void classifySensorValue(blindmodel &blind) {
void checkButton(button &btn) { void checkButton(button &btn) {
btn.changed=false; btn.changed=false;
if (millis() > btn.last_time_read + BUTTON_DEBOUNCE) { if (millis() > btn.last_time_read + BUTTON_DEBOUNCE) {
bool new_pin_button_down=!digitalRead(btn.pin); bool new_pin_button_down=!digitalRead(btn.pin);
if (btn.down != new_pin_button_down) { //changed if (btn.down != new_pin_button_down) { //changed
btn.down = new_pin_button_down; //update btn.down = new_pin_button_down; //update
btn.changed=true; 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) void manualMoveHandler(button &btn, blindmodel &blind)
{ {
if (btn.changed) { if (btn.changed) {
if (btn.down) { //changed to pressed
blind.mode=MODE_MANUAL; if (blind.error==ERRORCODE_UNDEFINED_POSITION) {
if (btn.manual_drive_direction) { //drive up
//M1.setmotor( _CW, 100); if (!btn.down && (btn.millisup-btn.millisdown)>BUTTON_TIME_HOLD_FIND_END) { //changed to released
blind.speed=-100;
//Serial.print("CW PWM: "); blind.mode = MODE_FIND_END;
}else{ //drive down blind.mode_find_end_state=0; //reset mode find state
blind.speed=100; blind.position_last_classchange=blind.position; //otherwise error trips immediately
//Serial.print("CCW PWM: ");
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) void readSensor(blindmodel &blind, int value, HomieNode &node)
@ -746,6 +775,9 @@ String modeNumToString(uint8_t modenum){
case MODE_ERROR: case MODE_ERROR:
return "MODE_ERROR"; return "MODE_ERROR";
break; break;
case MODE_MANUAL:
return "MODE_MANUAL";
break;
} }
return "UNDEF"; return "UNDEF";
} }