How do I add Millis instead of delay

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);
  }
}

see Blink without delay

1 Like

Hello
Take a view into the IDE example "BLINKWITHOUTDELAY".
Have a nice day and enjoy coding in C++.

For extra information and examples look at Using millis() for timing. A beginners guide and Several things at the same time

also can you explain why you have delays in the first place in that code? what's their purpose?

1 Like

The code is a bluetooth Dartboard. It runs as it should with or without the delays but the loop randomly seems to wait and does not register the dart throw Im hoping millis will fix the problem I am very new to this

I'd bet this waitForKey() is the culprit.

https://playground.arduino.cc/Code/Keypad/#Functions

See if you can use the other functions.

Do you have any suggestions on another way

I use to have it like this but still had the sane issue

while (deviceConnected) {
customKey = customKeypad.getKey();
compare_value = int(customKey);
delay(1);
if (customKey != NO_KEY)

instead of

//doing stuff
delay(x);
//doing more stuff after
delay(y);

Try the following structure

if (millis() - xTimestamp >= xdelayInterval) {//continuously check if it's time to do the stuff
  xTimestamp = millis(); // alternatively xTimestamp += xdelayInterval; but watch out!
  //do the stuff
}
if (millis() - yTimestamp >= ydelayInterval) {//after checking if it's time above check if it's time to do this stuff.
  yTimestamp = millis();
  //do the other stuff
}

x and y can happen independently of one another now, but each in their own respective intervals.

Thanks

Maybe you are losing connection? It's got to be doing something.

I dump that delay() and turn the while (deviceConnected) {...} into a if (deviceConnected){...} else{Serial.print("X");}

ok thanks

So I apologize for my ingnorance but would you mind showing me this using my code. Im very new to this and im not sure I understand thanks

well you have uncommented all delays in the code you have posted.
The delays are pretty short: 0,02 seconds. This might be a delay that is nescessary in the communication-process.

You should post a detailed description of the behaviour that your device is showing and a detailed description of how the behaviour should be.

non-blocking timing based on millis() would enable that the code after the delays could be executed immidiately. It is unclear which parts of the code inside loop() should be executed at high speed and which parts must be executed after a certain delay.

I have no experience with this kind of blue-tooth-communication.
anyway posting a detailed description of the observed behaviour and what problems the observed behaviour creates would help very much to find a solution.

best regards Stefan

I will ask how much is your code? You have several libraries, which I understand are important for complicated things like Bluetooth communication, but then you have a bunch of pointers and advanced coding concepts sprinkled among a very rudimentary structure. The whole thing smells of copy & paste, so before we move forward, I need to know if you understand every letter or symbol in your code? Do you understand why a line is there? Do you understand the behavior of the code written so far? If not, this whole thread is an x-y problem. So, if your code is not your own, I'd like to start with your code. This is akin to jumping into the deep end of the pool and asking a guy in the water to teach you to swim.

1 Like

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