Code causes esp32 to go into panicked

This code causes my esp32 to go into panicked only when trying to make the bluetooth connection. Any help would be appreciated.


#include <BLEDevice.h>
#include <BLE2902.h>
#include "MultiMap.h"
BLEServer* pServer = NULL;

BLECharacteristic* pCharacteristic = NULL;
BLECharacteristic* pCharacteristic2 = NULL;

//Variable Initializations
bool deviceConnected = false;
bool oldDeviceConnected = false;
int32_t value = 0;
int txValue = 0;
char customKey = 0;
int compare_value = 0;
static unsigned long time_now = 0;

#define SERVICE_UUID        "442f1570-8a00-9a28-cbe1-"
#define CHARACTERISTIC_UUID "442f1571-8a00-9a28-cbe1-"
#define CHARACTERISTIC_UUID2 "442f1572-8a00-9a28-cbe1-"

class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      deviceConnected = true;
      BLEDevice::startAdvertising();
    }

    void onDisconnect(BLEServer* pServer) {
      deviceConnected = false;
    }
};

const byte ROWS = 8;
const byte COLS = 11;

char hexaKeys[] = {
  'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
  'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'X', 'T', 'U', 'V',
  'W', 'S', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7',
  '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
  'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  'u', 'v', 'w', 'x', 'y', 'z', '!', '@', '#', '$', '%',
  '^', '&', '*', '(', ')', '-', '?', ';', ':', '"', '[',
  '{', ']', '}', '_', '=', '+', ',', '<', '.', '>', '/',
};

int Keys[] = {
  33, 34, 35, 36, 37, 38, 40, 41, 42, 43, 44, 45, 46, 47,                  // !, ", #, $, %, &, (, ), *, +, ,, -, ., /,
  49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,                  //1, 2, 3, 4, 5, 6, 7, 8, 9, :, ;, <, =, >,
  63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,                  //?, @, A, B, C, D, E, F, G, H, I, J, K, L,
  77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,                  //M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
  91, 92, 93, 94, 95, 97, 98, 99, 100, 101, 102, 103, 104, 105,            //[, \, ], ^, _, a, b, c, d, e, f, g, h, i,
  106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,    //j, k, l, m, n, o, p, q, r, s, t, u, v, w,
  120, 121, 122, 123, 125,                                                 //x, y, z, {, }
};

char *results[] = {
  "10.4@", "5.4@", "5.3@", "5.2@", "3.4@", "6.5@", "11.4@", "6.3@", "11.5@", "BTN@", "10.6@", "BTN@",  "5.6@", "3.6@",
  "7.2@", "BTN@", "0.4@", "0.5@", "1.5@", "1.3@", "2.5@", "4.3@", "4.2@", "5.5@", "9.5@", "9.6@", "8.5@", "4.6@",
  "10.5@", "9.4@", "2.0@", "3.0@", "9.1@", "10.1@", "7.1@", "BTN@", "0.0@", "0.1@", "1.2@", "1.0@", "2.3@", "2.1@",
  "3.1@", "9.0@", "10.0@", "7.0@", "BTN@", "0.2@", "3.2@", "1.4@", "1.1@", "2.4@", "2.2@", "0.3@", "9.2@", "10.2@",
  "3.5@", "BTN@", "6.6@", "7.5@", "8.6@", "8.2@", "8.3@", "8.4@", "BTN@", "4.5@", "0.6@", "1.6@", "4.4@", "2.6@",
  "7.3@", "6.2@", "11.0@", "11.1@", "6.1@", "4.0@", "10.3@", "9.3@", "5.0@", "5.1@", "3.3@", "7.4@", "6.4@", "11.3@",
  "11.2@", "6.0@", "8.0@", "7.6@", "11.6@",
};

int results_ind[89];
int len = sizeof(Keys) / sizeof(Keys[0]);

byte colPins[COLS] = {12, 13, 14, 15, 18, 23, 25, 26, 27, 32, 33,};
byte rowPins[ROWS] =  {0, 2, 3, 4, 5, 19, 21, 22,};



String inputString;
long inputInt;

void setup()
{
 // Serial.begin(115200); --> Uncomment to debug

  for (int i = 0; i < ROWS; i++) {
    pinMode(rowPins[i], INPUT_PULLUP);
  }

  for (int i = 0; i < COLS; i++) {
    pinMode(colPins[i], OUTPUT);
    digitalWrite(colPins[i], HIGH);
  }
  
  inputString.reserve(10); // maximum number of digit for a number is 10, change if needed

  BLEDevice::init("GRANBOARD");
  pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());
  BLEService *pService = pServer->createService(SERVICE_UUID);
  pCharacteristic = pService->createCharacteristic(
                      CHARACTERISTIC_UUID,
                      BLECharacteristic::PROPERTY_NOTIFY |
                      BLECharacteristic::PROPERTY_READ
                    );
  pCharacteristic2 = pService->createCharacteristic(
                       CHARACTERISTIC_UUID2,
                       BLECharacteristic::PROPERTY_WRITE_NR
                     );
  pCharacteristic->addDescriptor(new BLE2902());

  pService->start();

  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->setScanResponse(true);
  pAdvertising->setMinPreferred(0x00);  // functions that help with iPhone connections issue
  pAdvertising->setMinPreferred(0x00);
  BLEDevice::startAdvertising();

  for (int i = 0; i < 89; i++) {
    results_ind[i] = i;
  }
  Serial.println("Waiting a client connection to notify..."); //--> Uncomment to debug
}
int calculate_index(int row, int col){
  int index = (row*COLS)+col;
  return(index);
}
void loop() {

  if (deviceConnected) {
     for(int i = 0; i < COLS; i++){
        digitalWrite(colPins[i], LOW);
        for(int j = 0; j < ROWS; j++){
            if(digitalRead(rowPins[j]) == LOW){
                compare_value = int(hexaKeys[calculate_index(j,i)]);
                pCharacteristic->setValue(results[multiMap(compare_value, Keys, results_ind, len)]);
                Serial.println(results[multiMap(compare_value, Keys, results_ind, len)]); //   ---> Uncomment to debug
                pCharacteristic->notify();
                value++;
                delay(250); //debounce delay
                break;
            }
        }
        digitalWrite(rowPins[i], HIGH);//}
    }
    pCharacteristic->setValue("");
    customKey = NULL;
    compare_value = 0;
  }    
 }

@darts123, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

Your UUIDs are not complete/correct. When I go to this site
https://www.uuidgenerator.net/
and create complete UUID's The code compiles and loads without the core panic.

//#define SERVICE_UUID "442f1570-8a00-9a28-cbe1-"
//#define CHARACTERISTIC_UUID "442f1571-8a00-9a28-cbe1-"
//#define CHARACTERISTIC_UUID2 "442f1572-8a00-9a28-cbe1-"

#define SERVICE_UUID "c6e20f67-c6fb-4064-b794-b633643bbd13"
#define CHARACTERISTIC_UUID "20a68986-07c9-4848-9b7f-9a5d1def830e"
#define CHARACTERISTIC_UUID2 "9b243d6f-9787-42f3-8787-9bff7764ef10"

I can get to this point

16:28:46.948 -> Waiting a client connection to notify...

On connection from nrfConnect there are all sorts of errors which I didn't really look at the code to understand.

E (147000) gpio: gpio_set_level(226): GPIO output gpio_num error

The ADC2 hardware is used by the radio subsystem. So, if you use WiFI or Bluetooth the ADC2 converter is unavailable. only pins connected to ADC1 can be used for analog inputs if the radio subsystem is used.
https://github.com/espressif/arduino-esp32/issues/2557

1 Like

Looking at it again, your are not using these pins as analog pins. So this is probably not the problem.

if (deviceConnected) {
    for (int i = 0; i < COLS; i++) {
      digitalWrite(colPins[i], LOW);
      for (int j = 0; j < ROWS; j++) {
        if (digitalRead(rowPins[j]) == LOW) {
          compare_value = int(hexaKeys[calculate_index(j, i)]);
          pCharacteristic->setValue(results[multiMap(compare_value, Keys, results_ind, len)]);
          Serial.println(results[multiMap(compare_value, Keys, results_ind, len)]); //   ---> Uncomment to debug
          pCharacteristic->notify();
          value++;
          delay(250); //debounce delay
          break;
        }
      }
        digitalWrite(rowPins[i], HIGH);//}
    }

This line is the source of the GPIO error.
digitalWrite(rowPins[i], HIGH);//}

const byte ROWS = 8;
const byte COLS = 11;

In the code block, i is an index for columns but you are applying it to rows, and are trying to write to pins not in the array.
It's also not clear what you are trying to do here, as writing HIGH or LOW to an input pin, may be turning on or off the internal pullup.

You can confirm the source of the error by limiting the row indexes and no errors are seen.

if (i < ROWS)
        digitalWrite(rowPins[i], HIGH);//}

Yeah I just didnt want to put all of the uuid on here do to privacy concerns

Were you seeing the panic in your testing with the complete uuids?
The writing out of the array bounds is a separate issue, and didn't lead to the panic in my testing.

I appreciate the help. The code loads fine. It only overloads when I try to connect via bluetooth.

This fixed the rebooting issue. It now connects fine but isnt registering the right button presses. I can hit the same button 3 times and get 3 different numbers

I understand that. But with the code you provided when I completed the uuids, there was no panic exception with bluetooth connection.

Are you aware of the exception decoder tool for the esp32.
https://github.com/me-no-dev/EspExceptionDecoder

It will help analyse the error you see when the code crashes.

I will check it out thanks

This is a separate issue with the keyboard matrix and the multimap. I would work on this in a separate test sketch without the BLE and the value update. Just work first on reading the keyboard.

Sorry if im stating the obvious but with you fix of if (i < ) ROWS it connects fine if I take it out it reboots on Connection. So I believe that issue is solved. I think now its an issue of something that is wrong with this.

int calculate_index(int row, int col){
  int index = (row*COLS)+col;
  return(index);
}
void loop() {

  if (deviceConnected) {
     for(int i = 0; i < COLS; i++){
        digitalWrite(colPins[i], LOW);
        for(int j = 0; j < ROWS; j++){
            if(digitalRead(rowPins[j]) == LOW){
                compare_value = int(hexaKeys[calculate_index(j,i)]);
                pCharacteristic->setValue(results[multiMap(compare_value, Keys, results_ind, len)]);
                Serial.println(results[multiMap(compare_value, Keys, results_ind, len)]); //   ---> Uncomment to debug
                pCharacteristic->notify();
                value++;
                delay(250); //debounce delay
                break;

Yes, I agree. There may also be a hardware issue with the wiring and connection of the keyboard to the esp32.

If the same key repeats, then there may be an issue of the debounce. If you are getting different keys, then that maybe something else.

yeah Im getting differnet keys. This is accually a dartboard. The wiring is fine because it works with an older version but just isnt fast enough. It will randomly miss throws. tis is the old code. With this code every thing registers as it should.


#include <Keypad.h>
#include <BLEDevice.h>
#include <BLE2902.h>
#include "MultiMap.h"
BLEServer* pServer = NULL;

BLECharacteristic* pCharacteristic = NULL;
BLECharacteristic* pCharacteristic2 = NULL;

bool deviceConnected = false;
bool oldDeviceConnected = false;
int32_t value = 0;
int txValue = 0;
char customKey = 0;
int compare_value = 0;

#define SERVICE_UUID        "442f1570-8a00-9a28-cbe1-e1d4212d53eb"
#define CHARACTERISTIC_UUID "442f1571-8a00-9a28-cbe1-e1d4212d53eb"
#define CHARACTERISTIC_UUID2 "442f1572-8a00-9a28-cbe1-e1d4212d53eb"

class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      deviceConnected = true;
      BLEDevice::startAdvertising();
    }

    void onDisconnect(BLEServer* pServer) {
      deviceConnected = false;
    }
};

const byte ROWS = 8;
const byte COLS = 11;

char hexaKeys[] = {
  'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
  'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'X', 'T', 'U', 'V',
  'W', 'S', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7',
  '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
  'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  'u', 'v', 'w', 'x', 'y', 'z', '!', '@', '#', '$', '%',
  '^', '&', '*', '(', ')', '-', '?', ';', ':', '"', '[',
  '{', ']', '}', '_', '=', '+', ',', '<', '.', '>', '/',
};

int Keys[] = {
  33, 34, 35, 36, 37, 38, 40, 41, 42, 43, 44, 45, 46, 47,                  // !, ", #, $, %, &, (, ), *, +, ,, -, ., /,
  49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,                  //1, 2, 3, 4, 5, 6, 7, 8, 9, :, ;, <, =, >,
  63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,                  //?, @, A, B, C, D, E, F, G, H, I, J, K, L,
  77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,                  //M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
  91, 92, 93, 94, 95, 97, 98, 99, 100, 101, 102, 103, 104, 105,            //[, \, ], ^, _, a, b, c, d, e, f, g, h, i,
  106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,    //j, k, l, m, n, o, p, q, r, s, t, u, v, w,
  120, 121, 122, 123, 125,                                                 //x, y, z, {, }
};

char *results[] = {
  "10.4@", "5.4@", "5.3@", "5.2@", "3.4@", "6.5@", "11.4@", "6.3@", "11.5@", "BTN@", "10.6@", "BTN@",  "5.6@", "3.6@",
  "7.2@", "BTN@", "0.4@", "0.5@", "1.5@", "1.3@", "2.5@", "4.3@", "4.2@", "5.5@", "9.5@", "9.6@", "8.5@", "4.6@",
  "10.5@", "9.4@", "2.0@", "3.0@", "9.1@", "10.1@", "7.1@", "BTN@", "0.0@", "0.1@", "1.2@", "1.0@", "2.3@", "2.1@",
  "3.1@", "9.0@", "10.0@", "7.0@", "BTN@", "0.2@", "3.2@", "1.4@", "1.1@", "2.4@", "2.2@", "0.3@", "9.2@", "10.2@",
  "3.5@", "BTN@", "6.6@", "7.5@", "8.6@", "8.2@", "8.3@", "8.4@", "BTN@", "4.5@", "0.6@", "1.6@", "4.4@", "2.6@",
  "7.3@", "6.2@", "11.0@", "11.1@", "6.1@", "4.0@", "10.3@", "9.3@", "5.0@", "5.1@", "3.3@", "7.4@", "6.4@", "11.3@",
  "11.2@", "6.0@", "8.0@", "7.6@", "11.6@",
};

int results_ind[89];
int len = sizeof(Keys) / sizeof(Keys[0]);

byte colPins[COLS] = {12, 13, 14, 15, 18, 23, 25, 26, 27, 32, 33,};
byte rowPins[ROWS] =  {0, 2, 3, 4, 5, 19, 21, 22,};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

String inputString;
long inputInt;

void setup()
{
  Serial.begin(115200);

  for (int i = 0; i < ROWS; i++) {
    pinMode(rowPins[i], INPUT_PULLUP);
  }

  for (int i = 0; i < COLS; i++) {
    pinMode(colPins[i], OUTPUT);
    digitalWrite(colPins[i], HIGH);
  }
  
  inputString.reserve(10); // maximum number of digit for a number is 10, change if needed

  BLEDevice::init("GRANBOARD");
  pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());
  BLEService *pService = pServer->createService(SERVICE_UUID);
  pCharacteristic = pService->createCharacteristic(
                      CHARACTERISTIC_UUID,
                      BLECharacteristic::PROPERTY_NOTIFY |
                      BLECharacteristic::PROPERTY_READ
                    );
  pCharacteristic2 = pService->createCharacteristic(
                       CHARACTERISTIC_UUID2,
                       BLECharacteristic::PROPERTY_WRITE_NR
                     );
  pCharacteristic->addDescriptor(new BLE2902());

  pService->start();

  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->setScanResponse(true);
  pAdvertising->setMinPreferred(0x00);  // functions that help with iPhone connections issue
  pAdvertising->setMinPreferred(0x00);
  BLEDevice::startAdvertising();

  customKeypad.setHoldTime(50);
  customKeypad.setDebounceTime(20);

  for (int i = 0; i < 89; i++) {
    results_ind[i] = i;
  }
  Serial.println("Waiting a client connection to notify...");
}
void loop() {

  if (deviceConnected) {
    customKey = customKeypad.waitForKey();
    compare_value = int(customKey);
   // delay(20);    //delay is from time segment is hit till it reads in app
    if (customKey != NO_KEY)
    {
      pCharacteristic->setValue(results[multiMap(compare_value, Keys, results_ind, len)]);
      Serial.println(results[multiMap(compare_value, Keys, results_ind, len)]);
      pCharacteristic->notify();
      value++;
      // delay(20);
      // break;
    }
    pCharacteristic->setValue("");
    customKey = NULL;
    compare_value = 0;
    // break;
    //  delay(20);
  }
}

The occasional comment would help. I see how you're managing the scan logic but the connection code is pretty dense.

It will randomly miss throws

Are you saying that the code which uses Keypad.h is accurate for the reading, but sometimes misses a reading, while the currently used matrix scanning code is not accurate and produces different reading for the same location, but never misses a reading?

Why do you think that the missed readings are due to the library useage?

for(int i = 0; i < COLS; i++){
        digitalWrite(colPins[i], LOW);
        for(int j = 0; j < ROWS; j++){
            if(digitalRead(rowPins[j]) == LOW){
                compare_value = int(hexaKeys[calculate_index(j,i)])

Are the three different numbers all the same column for three consecutive rows? I am thinking that if the dart holds down the key for a period of time, the scanning of the rows( i value scans) will pick the column value(j value scans) through multiple scans.

In other words are the three values of
int(hexaKeys[calculate_index(j,i)]);

index j,i
index j,i+1
index j,i+2

Sorry for the delay in response. yes the 3 different numbers are all in the same column.