Hi there,
I had a running code, trying to make Button5 to reset the ESP32 when pushed longer than 5sec.
I didn't change anything on the oneButton DoubleClick
function, but now I get this failure message and cant figure out what is the reason for that.
Can somebody help?
THX
[code]
#include <FastLED.h>
// uses libraries from
// https://github.com/r89m/PushButton
// https://github.com/r89m/Button
// https://github.com/thomasfredericks/Bounce2
#include <Button.h>
#include <ButtonEventCallback.h>
#include <PushButton.h>
#include <Bounce2.h>
#include <BluetoothSerial.h>
#include <BleKeyboard.h>
// Updating server
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <Update.h>
#include "OneButton.h"
// Version
#define VERSION "2.3"
// Init BLE
BleKeyboard bleKeyboard("XCREMOTE", "XCNAV UG", 100);
// Pins
const int ledPin = 02;
const int Joy_up_pin = 15; // UP
const int Joy_down_pin = 26; // Down
const int Joy_left_pin = 25; //Left
const int Joy_right_pin = 27; //Right
const int Button_ok_pin = 17; //Enter
const int Button_2_pin = 18; // Rectangle
const int Button_3_pin = 05; // Triangle
const int Button_4_pin = 33; // Circle
const int Button_5_pin = 04; // X
// Button's keys
const char Joy_up_press_key = KEY_UP_ARROW;
const char Joy_down_press_key = KEY_DOWN_ARROW;
const char Joy_left_press_key = KEY_LEFT_ARROW;
const char Joy_right_press_key = KEY_RIGHT_ARROW;
const char Button_2_press_key = KEY_F4;
const char Button_2_hold_key = KEY_F3;
const char Button_3_press_key = KEY_F6;
const char Button_3_hold_key = KEY_F2;
const char Button_4_press_key = 'M';
const char Button_4_hold_key = KEY_F1;
const char Button_5_press_key = KEY_ESC;
const char Button_5_hold_key = 'T';
const char Button_ok_hold_key = 'P';
const char Button_ok_press_key = KEY_RETURN;
const int joy_rebounce_interval = 3;
const int joy_key_rebounce_threshold = 20;
const int joy_key_first_pressed_threshold = 100;
const int button_hold_threshold = 500;
const int joy_hold_threshold = 1;
const int button_reset_threshold = 5000;
const int button_reset_rebounce_interval = 3;
// PushButton's instances
PushButton Joy_up = PushButton(Joy_up_pin);
PushButton Joy_down = PushButton(Joy_down_pin);
PushButton Joy_left = PushButton(Joy_left_pin);
PushButton Joy_right = PushButton(Joy_right_pin);
PushButton Button_ok = PushButton(Button_ok_pin);
PushButton Button_2 = PushButton(Button_2_pin);
PushButton Button_3 = PushButton(Button_3_pin);
PushButton Button_4 = PushButton(Button_4_pin);
PushButton Button_5 = PushButton(Button_5_pin);
// Setup a new OneButton on pin PIN_INPUT
// The 2. parameter activeLOW is true, because external wiring sets the button to LOW when pressed.
OneButton button1(Button_ok_pin, true);
OneButton button2(Button_5_pin, true);
// save the millis when a press has started.
unsigned long pressStartTime;
// Variables
int bt_first_connected = false;
boolean first_pressed = 1;
int joy_key_counter = 0;
// RGB led
#define NUM_LEDS 1 // How many leds in your strip?
CRGB leds[NUM_LEDS]; // Define the array of leds
// Updating server
const char* host = "esp32";
const char* ssid = "XCREMOTE";
//const char* ssid = "xcremote";
const char* password = "xcremote";
boolean server_running = 0;
WebServer server(80);
// current Button_ok state, staring with LOW (0)
int Button_ok_State = LOW;
void setup() {
pinMode(04, INPUT_PULLUP);
pinMode(05, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP);
pinMode(17, INPUT_PULLUP);
pinMode(18, INPUT_PULLUP);
pinMode(25, INPUT_PULLUP);
pinMode(26, INPUT_PULLUP);
pinMode(27, INPUT_PULLUP);
pinMode(33, INPUT_PULLUP);
Serial.begin(115200);
Serial.println("Version: " VERSION);
Serial.print("XCRemote MAC Address: ");
Serial.println(WiFi.macAddress());
// link the xxxclick functions to be called on xxxclick event.
button1.attachDoubleClick(doubleClick);
button2.setPressTicks(5000); // that is the time when LongPressStart is called
button2.attachLongPressStart(pressStart);
// pinMode(ledPin, OUTPUT);
FastLED.addLeds<SK6812, ledPin, GRB>(leds, NUM_LEDS); // GRB ordering is typical
FastLED.setBrightness(100);
leds[0] = CRGB::White;
FastLED.show();
if (digitalRead(Button_ok_pin) == 0) updating_server_start();
else {
Serial.println("Starting BLE work!");
bleKeyboard.begin();
Joy_up.onRelease(onJoyReleased);
Joy_down.onRelease(onJoyReleased);
Joy_left.onRelease(onJoyReleased);
Joy_right.onRelease(onJoyReleased);
Joy_up.onHoldRepeat(joy_hold_threshold, joy_rebounce_interval, onJoy);
Joy_down.onHoldRepeat(joy_hold_threshold, joy_rebounce_interval, onJoy);
Joy_left.onHoldRepeat(joy_hold_threshold, joy_rebounce_interval, onJoy);
Joy_right.onHoldRepeat(joy_hold_threshold, joy_rebounce_interval, onJoy);
Button_ok.onRelease(0, button_hold_threshold - 1, onButtonReleased);
Button_2.onRelease(0, button_hold_threshold - 1, onButtonReleased);
Button_3.onRelease(0, button_hold_threshold - 1, onButtonReleased);
Button_4.onRelease(0, button_hold_threshold - 1, onButtonReleased);
Button_5.onRelease(0, button_hold_threshold - 1, onButtonReleased);
Button_ok.onHold(button_hold_threshold, onButtonHeld);
Button_2.onHold(button_hold_threshold, onButtonHeld);
Button_3.onHold(button_hold_threshold, onButtonHeld);
Button_4.onHold(button_hold_threshold, onButtonHeld);
Button_5.onHold(button_hold_threshold, onButtonHeld);
Button_5.onHoldRepeat(button_reset_threshold, button_reset_rebounce_interval, onResetButton);
joy_key_counter = 0;
}
}
void loop() {
// keep watching the push button:
button1.tick();
if (server_running) {
server.handleClient();
delay(1);
}
else {
if (bleKeyboard.isConnected()) {
leds[0] = CRGB::Blue;
FastLED.show();
if (not bt_first_connected) {
leds[0] = CRGB::Blue;
FastLED.show();
bt_first_connected = true;
Serial.println("BT connected");
}
Joy_up.update();
Joy_down.update();
Joy_left.update();
Joy_right.update();
Button_ok.update();
Button_2.update();
Button_3.update();
Button_4.update();
Button_5.update();
} else {
leds[0] = CRGB::White;
FastLED.show();
}
}
}
void keyboardPress(char key) {
bleKeyboard.press(key);
}
void onButtonReleased(Button& btn, uint16_t duration) {
if (btn.is(Button_2)) keyboardPress(Button_2_press_key);
if (btn.is(Button_3)) keyboardPress(Button_3_press_key);
if (btn.is(Button_4)) keyboardPress(Button_4_press_key);
if (btn.is(Button_5)) keyboardPress(Button_5_press_key);
if (btn.is(Button_ok)) keyboardPress(Button_ok_press_key);
bleKeyboard.releaseAll();
}
void onButtonHeld(Button& btn, uint16_t duration) {
if (btn.is(Button_2)) keyboardPress(Button_2_hold_key);
if (btn.is(Button_3)) keyboardPress(Button_3_hold_key);
if (btn.is(Button_4)) keyboardPress(Button_4_hold_key);
if (btn.is(Button_5)) keyboardPress(Button_5_hold_key);
if (btn.is(Button_ok)) keyboardPress(Button_ok_hold_key);
bleKeyboard.releaseAll();
}
void onResetButton(Button& btn, uint16_t duration)
if (btn.isPressed() && duration(5000) {
Serial.println ("ESP will restart in 2 seconds");
bleKeyboard.releaseAll();
delay(2000);
ESP.restart();
}
void onJoy(Button& btn, uint16_t duration, uint16_t repeat_count) {
if (btn.isPressed() && joy_key_counter == 5) {
if (btn.is(Joy_up)) keyboardPress(Joy_up_press_key);
if (btn.is(Joy_down)) keyboardPress(Joy_down_press_key);
if (btn.is(Joy_left)) keyboardPress(Joy_left_press_key);
if (btn.is(Joy_right)) keyboardPress(Joy_right_press_key);
bleKeyboard.releaseAll();
}
joy_key_counter = joy_key_counter + 1;
if (first_pressed && joy_key_counter > joy_key_first_pressed_threshold) {
joy_key_counter = 0;
first_pressed = 0;
}
if (!first_pressed && joy_key_counter > joy_key_rebounce_threshold) joy_key_counter = 0;
}
void onJoyReleased(Button& btn, uint16_t duration) {
joy_key_counter = 0;
first_pressed = 1;
}
// this function will be called when the button was pressed 2 times in a short timeframe.
void doubleClick() {
if (Button_ok_State == LOW) {
bleKeyboard.print("V");
Serial.println("Vario");
}
else {
bleKeyboard.print("S");
Serial.println("Sollfahrt");
}
Button_ok_State = !Button_ok_State; // reverse the Button_ok_State
} // doubleClick
// this function will be called when the button was held down for 0.5 second or more.
void pressStart()
{
Serial.println("ESP will restart in 2 seconds");
delay (2000);
ESP.restart();
pressStartTime = millis() - 5000; // as set in setPressTicks()
} // pressStart()