add find end mode for buttons on startup
This commit is contained in:
parent
6cd5c9c9e0
commit
a0960eb5c6
|
@ -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
|
||||
|
||||
|
|
38
src/main.cpp
38
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 (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.print("CW PWM: ");
|
||||
Serial.println("CW manual ");
|
||||
}else{ //drive down
|
||||
blind.speed=100;
|
||||
//Serial.print("CCW PWM: ");
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
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";
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue