How can I make this code more sensitive/ faster

Please help. The dartboard code works as it should but isn't quite sensitive enough. I believe it needs to be faster. It will miss random dart hits. It reads about 90 percent of darts thrown. Ant help would be appreciated. Please be gentle I am new to this.

dartboard.ino (5.7 KB)

#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;
//int customKey = 0;
char customKey = 0;
int compare_value = 0;

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

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@", "XXXX", "10.6@", "XXXX",  "5.6@", "3.6@",
  "7.2@", "XXXX", "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@", "XXXX", "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@", "XXXX", "6.6@", "7.5@", "8.6@", "8.2@", "8.3@", "8.4@", "XXXX", "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;
//BluetoothSerial SerialBT;

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

  //Setup pin states
  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

  // Create the BLE Device
  BLEDevice::init("GRANBOARD");

  // Create the BLE Server
  pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  // Create the BLE Service
  BLEService *pService = pServer->createService(SERVICE_UUID);
  // Create a BLE Characteristic
  pCharacteristic = pService->createCharacteristic(
                      CHARACTERISTIC_UUID,
                      BLECharacteristic::PROPERTY_READ   |
                      BLECharacteristic::PROPERTY_NOTIFY
                    );
  pCharacteristic2 = pService->createCharacteristic(
                       CHARACTERISTIC_UUID2,
                       BLECharacteristic::PROPERTY_WRITE
                     );
  // Create a BLE Descriptor
  pCharacteristic->addDescriptor(new BLE2902());

  // Start the service
  pService->start();

  // Start advertising
  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(10);
  customKeypad.setDebounceTime(0);

  //Initialize the indexing array
  for (int i = 0; i < 89; i++) {
    results_ind[i] = i;
    //  delay(150);
  }

  Serial.println("Waiting a client connection to notify...");
}
void loop() {

  while (deviceConnected) {
    customKey = customKeypad.waitForKey();
    compare_value = int(customKey);
    delay(1);    //delay is from time segment is hit till it reads in app
    // break;
    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();
      // break;
      value++;
      delay(200);
      // break;
    }
    pCharacteristic->setValue("");
    customKey = NULL;
    compare_value = 0;
    break;
    // delay(500);
  }
}

If you're missing responses, this is probably part of the issue.

Your loop only runs once. Check out the blink without delay example to see how to fix it and remove the while loop.

I will check it out. But the misses are random. It nay read dart 1 not dart 2 then it will read dart 3

If the darts are thrown randomly, I'd expect the misses to be random as well. I usually use a gap of about 40ms for key presses. 200ms seems like a huge opportunity to miss inputs.

Would be better served

void loop() {

  if (deviceConnected) {

The extra ";" is wonky.

Ok thanks ill check it out

Ok cool

Ive taken all the delays completely off and still have random darts not being read. Any more ideas???

This is my modified code with all of your suggestions.




#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;
//int customKey = 0;
char customKey = 0;
int compare_value = 0;

#define SERVICE_UUID        "442f1570-8a00-9a28-cbe1-e"
#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@", "XXXX", "10.6@", "XXXX",  "5.6@", "3.6@",
  "7.2@", "XXXX", "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@", "XXXX", "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@", "XXXX", "6.6@", "7.5@", "8.6@", "8.2@", "8.3@", "8.4@", "XXXX", "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;
//BluetoothSerial SerialBT;

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

  //Setup pin states
  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

  // Create the BLE Device
  BLEDevice::init("GRANBOARD");

  // Create the BLE Server
  pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  // Create the BLE Service
  BLEService *pService = pServer->createService(SERVICE_UUID);
  // Create a BLE Characteristic
  pCharacteristic = pService->createCharacteristic(
                      CHARACTERISTIC_UUID,
                      BLECharacteristic::PROPERTY_READ   |
                      BLECharacteristic::PROPERTY_NOTIFY
                    );
  pCharacteristic2 = pService->createCharacteristic(
                       CHARACTERISTIC_UUID2,
                       BLECharacteristic::PROPERTY_WRITE
                     );
  // Create a BLE Descriptor
  pCharacteristic->addDescriptor(new BLE2902());

  // Start the service
  pService->start();

  // Start advertising
  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(10);
  customKeypad.setDebounceTime(0);

  //Initialize the indexing array
  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(1);    //delay is from time segment is hit till it reads in app
    // break;
    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();
      // break;
      value++;
     // delay(1);
      // break;
    }
    pCharacteristic->setValue("");
    customKey = NULL;
    compare_value = 0;
   // break;
   //  delay(1);
  }
}

I looked at the blink without delay. Im not sure how to relate it to this code. Im sure you are right, Im just a newbie

Just out of curiosity does the skipping occur when BLE is not used?

Its not really skipping. Its mainly on the bullseye. Sometimes its the first dart and other times it not. Its completely random. I have several dartboards that are brand new and its the same on each one.

Yes it does

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.