I would like to get rid of the delays and add millis. Any help would be appreciated.
#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-"
#define CHARACTERISTIC_UUID "442f1571-8a00-9a28-c"
#define CHARACTERISTIC_UUID2 "442f1572-8a00-9a28-"
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_READ |
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);
}
}