diff --git a/hani-mandl.ino b/hani-mandl.ino index 3879846..f57553e 100644 --- a/hani-mandl.ino +++ b/hani-mandl.ino @@ -11,7 +11,7 @@ 2019-01 Marc Wetzel | Refakturierung und Dokumentation, published in the Facebook group also 2019-02 Clemens Gruber | code beautifying mit kleineren Umbenennungen bei Funktionen und Variablen - Anpssung fuer Heltec WiFi Kit 32 (ESP32 onboard OLED) + Anpassung fuer Heltec WiFi Kit 32 (ESP32 onboard OLED) - pins bei OLED-Initialisierung geaendert - pins geaendert, um Konflikte mit hard wired pins des OLEDs zu vermeiden 2019-02 Clemens Gruber | Aktivierung der internen pull downs für alle digitalen Eingaenge @@ -23,18 +23,34 @@ Hinweise zur Hardware --------------------- - - bei allen digitalen Eingänge sind interne pull downs aktiviert, keine externen-Widerständen nötig! + - bei allen digitalen Eingängen sind interne pull downs aktiviert, keine externen Widerstände nötig! */ #include #include -#include /* aus dem Bibliotheksverwalter */ +#include /* aus dem BSP von expressif */ #include /* https://github.com/bogde/HX711 */ #include /* https://github.com/jkb-git/ESP32Servo */ -#include /* aus dem BSP von expressif */ +#include +#include /* aus dem Bibliotheksverwalter */ + +using namespace ace_button; +// if you need debug output on the serial port, enable this #define isDebug +// if you need your display rotated, enable this +//#define DISPLAY_ROTATE + +// if you need to flip the poti direction, enable this +#define FLIP_POTI + +#ifdef isDebug +#define DebugOut(a) Serial.println(a) +#else +#define DebugOut(a) +#endif + Servo servo; HX711 scale; Preferences preferences; @@ -48,29 +64,40 @@ Preferences preferences; U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ 15, /* data=*/ 4, /* reset=*/ 16); // Servo -const int servo_pin = 2; +const int servo_pin = 17; // 3x Schalter Ein 1 - Aus - Ein 2 const int switch_betrieb_pin = 19; -const int switch_vcc_pin = 22; // <- Vcc const int switch_setup_pin = 21; // Taster -const int button_start_vcc_pin = 13; // <- Vcc const int button_start_pin = 12; -const int button_stop_vcc_pin = 14; // <- Vcc const int button_stop_pin = 27; // Poti const int poti_pin = 39; // Wägezelle-IC -const int hx711_sck_pin = 17; -const int hx711_dt_pin = 5; +const int hx711_sck_pin = 22; +const int hx711_dt_pin = 13; // wir verwenden auch den pin fuer LED_BUILTIN! // es ist pin 25 fuer den Heltec WiFi Kit 32 +ButtonConfig buttonConfig; +AceButton bt_start(&buttonConfig); +AceButton bt_stop(&buttonConfig); + +/* +// currently not used, as fire events after reboot is not supported +ButtonConfig switchConfig; +AceButton sw_betrieb(&switchConfig); +AceButton sw_setup(&switchConfig); +*/ + +enum menu_state_t { state_setup = 0, state_hand, state_betrieb }; + +menu_state_t main_state; const char* gewicht_char = ""; @@ -88,26 +115,47 @@ int korrektur; int autostart; int winkel; int winkel_min = 0; -int winkel_max = 155; -int winkel_dosier_min = 45; -float fein_dosier_gewicht = 60; +int winkel_max = 90; +int winkel_dosier_min = 60; //45 +float fein_dosier_gewicht = 30; //60 int i; int u; int a; int z; void print2serial(String displayname, int value) { +#ifdef isDebug Serial.print(displayname); Serial.println(value); +#endif } void print2serial(String displayname, float value) { +#ifdef isDebug Serial.print(displayname); Serial.println(value); +#endif } +int read_poti(int map_min, int map_max) +{ +#ifdef FLIP_POTI + return map(analogRead(poti_pin), 0, 4095, map_max, map_min); +#else + return map(analogRead(poti_pin), 0, 4095, map_min, map_max); +#endif +} + +int read_scale(void) +{ + +} + + void getPreferences(void) { // EEPROM // + DebugOut(__FUNCTION__); + preferences.begin("EEPROM", false); //faktor und tara aus eeprom lesen faktor2 = preferences.getUInt("faktor2", 0); @@ -136,7 +184,19 @@ void getPreferences(void) { preferences.end(); } +void putPreferences(void) { + preferences.begin("EEPROM", false); + preferences.putUInt("faktor2", (faktor * 10000)); + preferences.putUInt("tara", tara); + preferences.putUInt("tara_raw", tara_raw); + preferences.putUInt("fmenge", fmenge); + preferences.putUInt("korrektur", korrektur); + preferences.putUInt("autostart", autostart); + preferences.end(); +} + void setupTara(void) { + DebugOut(__FUNCTION__); u8g2.setCursor(0, 8); u8g2.print("*"); @@ -146,13 +206,12 @@ void setupTara(void) { u8g2.print("OK"); u8g2.sendBuffer(); delay(2000); - preferences.begin("EEPROM", false); - preferences.putUInt("tara", tara); - preferences.end(); + putPreferences(); } } void setupCalibration(void) { + DebugOut(__FUNCTION__); u8g2.setCursor(0, 22); u8g2.print("*"); @@ -198,16 +257,14 @@ void setupCalibration(void) { i = 0; faktor = ((gewicht_raw - tara_raw) / 500.000); - preferences.begin("EEPROM", false); // faktor und tara ins eeprom schreiben - preferences.putUInt("faktor2", (faktor * 10000)); - preferences.putUInt("tara_raw", tara_raw); - preferences.end(); + putPreferences(); } } } } void setupKorrektur(void) { + DebugOut(__FUNCTION__); u8g2.setCursor(0, 36); u8g2.print("*"); @@ -218,7 +275,7 @@ void setupKorrektur(void) { u8g2.clearBuffer(); while (i > 0) { - pos = (map(analogRead(poti_pin), 0, 4095, -50, 10)); + pos = read_poti(-50, 10); u8g2.setFont(u8g2_font_courB14_tf); u8g2.clearBuffer(); u8g2.setCursor(10, 12); @@ -235,15 +292,13 @@ void setupKorrektur(void) { delay(2000); i = 0; } - - preferences.begin("EEPROM", false); - preferences.putUInt("korrektur", korrektur); - preferences.end(); + putPreferences(); } } } void setupFuellmenge(void) { + DebugOut(__FUNCTION__); u8g2.setCursor(0, 50); u8g2.print("*"); @@ -254,7 +309,8 @@ void setupFuellmenge(void) { u8g2.clearBuffer(); while (i > 0) { - pos = (map(analogRead(poti_pin), 0, 4095, 1, 4)); + pos = read_poti(1, 4); + u8g2.setFont(u8g2_font_courB14_tf); u8g2.clearBuffer(); u8g2.setCursor(10, 12); @@ -279,9 +335,8 @@ void setupFuellmenge(void) { u8g2.sendBuffer(); delay(2000); i = 0; - preferences.begin("EEPROM", false); - preferences.putUInt("fmenge", fmenge); - preferences.end(); + putPreferences(); + } } @@ -297,9 +352,8 @@ void setupFuellmenge(void) { u8g2.sendBuffer(); delay(2000); i = 0; - preferences.begin("EEPROM", false); - preferences.putUInt("fmenge", fmenge); - preferences.end(); + + putPreferences(); } } @@ -315,9 +369,7 @@ void setupFuellmenge(void) { u8g2.sendBuffer(); delay(2000); i = 0; - preferences.begin("EEPROM", false); - preferences.putUInt("fmenge", fmenge); - preferences.end(); + putPreferences(); } } @@ -333,9 +385,7 @@ void setupFuellmenge(void) { u8g2.sendBuffer(); delay(2000); i = 0; - preferences.begin("EEPROM", false); - preferences.putUInt("fmenge", fmenge); - preferences.end(); + putPreferences(); } } } @@ -343,6 +393,7 @@ void setupFuellmenge(void) { } void setupAutostart(void) { + DebugOut(__FUNCTION__); u8g2.setCursor(0, 64); u8g2.print("*"); @@ -353,7 +404,8 @@ void setupAutostart(void) { u8g2.clearBuffer(); while (i > 0) { - pos = (map(analogRead(poti_pin), 0, 4095, 1, 2)); + pos = read_poti(1, 2); + u8g2.setFont(u8g2_font_courB14_tf); u8g2.clearBuffer(); u8g2.setCursor(10, 12); @@ -374,9 +426,8 @@ void setupAutostart(void) { u8g2.sendBuffer(); delay(2000); i = 0; - preferences.begin("EEPROM", false); - preferences.putUInt("autostart", autostart); - preferences.end(); + + putPreferences(); } } @@ -392,9 +443,7 @@ void setupAutostart(void) { u8g2.sendBuffer(); delay(2000); i = 0; - preferences.begin("EEPROM", false); - preferences.putUInt("autostart", autostart); - preferences.end(); + putPreferences(); } } } @@ -402,7 +451,9 @@ void setupAutostart(void) { } void processSetup(void) { - pos = (map(analogRead(poti_pin), 0, 4095, 1, 5)); + DebugOut(__FUNCTION__); + pos = read_poti(1, 5); + u8g2.setFont(u8g2_font_courB10_tf); u8g2.clearBuffer(); @@ -441,23 +492,28 @@ void processSetup(void) { a = 0; } -void processBetrieb(void) +void showMessage(String msg) { - pos = (map(analogRead(poti_pin), 0, 4095, 0, 100)); - gewicht = ((((int(scale.read())) - tara_raw) / faktor) - tara); - - if ((autostart == 1) && (gewicht <= 5) && (gewicht >= -5) && (a == 0)) { - delay(1000); - - if ((gewicht <= 5) && (gewicht >= -5)) { u8g2.clearBuffer(); u8g2.setFont(u8g2_font_courB24_tf); u8g2.setCursor(20, 43); - u8g2.print("START"); + u8g2.print(msg); u8g2.sendBuffer(); - delay(3000); +} + +void processBetrieb(void) +{ + DebugOut(__FUNCTION__); + pos = read_poti(0, 100); + + gewicht = ((((int(scale.read())) - tara_raw) / faktor) - tara); + + if ((autostart == 1) && (gewicht <= 5) && (gewicht >= -5) && (a == 0)) { + delay(1000); + showMessage("START"); + delay(1500); a = 1; - } + } if ((autostart == 1) && (gewicht < -20)) { @@ -470,7 +526,7 @@ void processBetrieb(void) } if ((digitalRead(button_stop_pin)) == HIGH) { - (winkel = winkel_min); + winkel = winkel_min; a = 0; } @@ -498,10 +554,14 @@ void processBetrieb(void) } servo.write(winkel); + DebugOut("Servo Winkel:"); + DebugOut(winkel); + float y = ((fmenge + korrektur - gewicht) / fein_dosier_gewicht); - Serial.println(y); + #ifdef isDebug + Serial.println(y); Serial.print(scale.read_average(3)); Serial.print(" Tara_raw:"); Serial.print(tara_raw); @@ -593,7 +653,9 @@ void processBetrieb(void) void processHandbetrieb(void) { - pos = (map(analogRead(poti_pin), 0, 4095, 0, 100)); + DebugOut(__FUNCTION__); + pos = read_poti(0, 100); + gewicht = ((((int(scale.read())) - tara_raw) / faktor) - tara); if ((digitalRead(button_start_pin)) == HIGH) { @@ -601,18 +663,16 @@ void processHandbetrieb(void) } if ((digitalRead(button_stop_pin)) == HIGH) { - (winkel = winkel_min); + winkel = winkel_min; a = 0; } - if ((digitalRead(button_stop_pin)) == HIGH) { - (winkel = winkel_min); - } - if (a == 1) { winkel = ((winkel_max * pos) / 100); } + DebugOut("Servo Winkel:"); + DebugOut(winkel); servo.write(winkel); #ifdef isDebug @@ -624,7 +684,7 @@ void processHandbetrieb(void) Serial.print(" Gewicht "); Serial.println(gewicht); #endif - + u8g2.clearBuffer(); u8g2.setFont(u8g2_font_courB24_tf); @@ -681,12 +741,32 @@ void processHandbetrieb(void) u8g2.setCursor(120, 13); u8g2.print(char(37)); u8g2.sendBuffer(); -//a=0; } +void handleEvent(AceButton* button, uint8_t eventType, uint8_t buttonState ) { + DebugOut("Handle Event"); + DebugOut("Button-ID:"); + DebugOut(button->getId()); + DebugOut("Button-eventtype:"); + DebugOut(eventType); + DebugOut("Button-state:"); + DebugOut(buttonState); + + switch (eventType) { + case AceButton::kEventReleased: + // We trigger on the Released event not the Pressed event to distinguish + // this event from the LongPressed event. + //retrievePreset(button->getId()); + break; + case AceButton::kEventLongPressed: + //setPreset(button->getId()); + break; + } +} void setup() { + DebugOut(__FUNCTION__); // enable internal pull downs for digital inputs pinMode(button_start_pin, INPUT_PULLDOWN); pinMode(button_stop_pin, INPUT_PULLDOWN); @@ -694,8 +774,26 @@ void setup() pinMode(switch_setup_pin, INPUT_PULLDOWN); pinMode(LED_BUILTIN, OUTPUT); + // Configure the Button/Switch Configs with the event handler, and enable all higher + // level events. + bt_start.init(button_start_pin, LOW, 0); + bt_stop.init(button_stop_pin, LOW, 1); + /* + sw_betrieb.init(switch_betrieb_pin, LOW, 2); + sw_setup.init(switch_setup_pin, LOW, 3); + */ + buttonConfig.setEventHandler(handleEvent); + buttonConfig.setFeature(ButtonConfig::kFeatureClick); + buttonConfig.setFeature(ButtonConfig::kFeatureDoubleClick); + /* + switchConfig.setEventHandler(handleEvent); + switchConfig.setFeature(ButtonConfig::kFeatureClick); + switchConfig.setFeature(ButtonConfig::kFeatureDoubleClick); + switchConfig.setFeature(ButtonConfig::kFeatureRepeatPress); + */ // switch Vcc / GND on normal pins for convenient wiring // output 5V for VCC + /* digitalWrite (switch_vcc_pin, HIGH); digitalWrite (button_start_vcc_pin, HIGH); digitalWrite (button_stop_vcc_pin, HIGH); @@ -707,32 +805,40 @@ void setup() pinMode (button_stop_vcc_pin, OUTPUT); // short delay to let chip power up delay (100); - + */ +#ifdef isDebug Serial.begin(115200); while (!Serial) { } - +#endif u8g2.begin(); + +#ifdef DISPLAY_ROTATE + u8g2.setDisplayRotation(U8G2_R0); + u8g2.setFlipMode(1); +#endif scale.begin(hx711_dt_pin, hx711_sck_pin); scale.power_up(); + DebugOut("Servo ATTACH"); + servo.attach(servo_pin, 750, 2500); getPreferences(); + } void loop() { + bt_start.check(); + bt_stop.check(); + if ((digitalRead(switch_setup_pin)) == HIGH) processSetup(); - - // Betrieb - if ((digitalRead(switch_betrieb_pin)) == HIGH) + else if ((digitalRead(switch_betrieb_pin)) == HIGH) processBetrieb(); - - // Handbetrieb - if ((digitalRead(switch_betrieb_pin) == LOW) - && (digitalRead(switch_setup_pin) == LOW)) + else processHandbetrieb(); + }