I am trying to key in the timing for how long the bldc motor needs to spin via keypad. I hope you can help me with this. I'm using delay() to set the timing of bldc motor spin. How do I key in a series of numbers in seconds, and then put it in the delay()?

The idea for this code is to enter the speed and timing via keypad. After entering the speed and timing, button '#' is used to move the bldc motor according to the speed and timing entered. My question is how do I make my code to compile where speed is keyed in first, timing second then button '#' to run the motor?

Here is my code so far...

#include "Keypad.h"
#include "LiquidCrystal.h"
#include "Servo.h"

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(A0,A1,A2,A3,A4,A5);

Servo myservo;

const byte ROWS = 4; //four rows
const byte COLS = 3; //four columns
char keys[ROWS][COLS] =
byte rowPins[ROWS] = {9,8,7,6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5,4,3}; //connect to the column pinouts of the keypad
int count=0;

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()

  lcd.setCursor (0,1);
  lcd.print ("T:");


void loop()
    static char buffer[4], spintime[4];
    static byte i = 0, a = 0;
    char key = keypad.getKey();

    // i < 3: prevent buffer overflow
    if  ('0' <= key && key <= '9')   // this is for speed entry
        buffer[i] = key;
        buffer[i] =0;  // null-terminate buffer
        lcd.setCursor (2,0);

     if  ('0' <= key && key <= '9') // this is for timing entry
        spintime[a] = key;
        spintime[a] =0;  // null-terminate buffer
        lcd.setCursor (2,1);
    else if (key == '#')            // this is to run the motor after speed and timing are entered
        int value = atoi(buffer);
        myservo.write(value);       //speed of motor
        int timing = atoi(spintime);
        delay (timing*1000);        // keep the motor spin for ( timing entered) second
        i = 0; 

1 Answer 1


As I understand it, you want to enter the speed, then the time, then '#'.

Let's imagine you want a speed of 22 and a time of 3. The device will see 2, 2, 3, #. How will it know whether you're asking for a speed of 22 and a time of 3, or a speed of 2 and a time of 23? Simple answer: it cannot know. So we need to find a way to tell it.

My suggestion: we'll enter speed followed by *, and time followed by #. (There are other possible solutions to this.)

We also need to decide the longest number the user can enter, and what happens if they enter a longer one, or if they don't enter one at all? Let's say the longest number is 3 digits, and if they enter a longer one, we will ignore the extra digits. So if they enter 123456789#, we will register 123#. (Again, there are other solutions.)

There's some stuff in your code about an LCD, but you don't mention it in the question, so I have ignored it.

Lastly, I am assuming your code to drive the motor and read keys is correct.

Here's some loop code which I hope will do the above. It will read numbers until it finds either a * or a #, convert the number to an integer, and store it as either time or speed. When it has both speed and time, it will spin the motor and reset the values.

void loop()
    // Where we will store the number as we type it.
    // This buffer is large enough for a 3-digit number, pull a null terminator
    char buffer[4];

    // The speed of the motor
    static int speed = 0;

    // The time to spin for
    static int time = 0;

    // Input up to 3 numbers until we find a * or #
    int i=0;
    while (1)
        char key = keypad.getKey();

        // If it's a number AND we have space left, add to our string
        if ('0' <= key && key <= '9' && i<3)
            buffer[i] = key;
        // If it's a * or #, end
        else if ('#' == key || '*' == key)
            // Null terminate
            buffer[i] = 0;

            // Convert to an integer
            int value = atoi(buffer);

            // Set the appropriate value
            if ('#' == key) time = value; 
            if ('*' == key) speed = value; 

    // If we have both speed and time...
    if (speed > 0 && time > 0)
        // ...spin the motor at the required speed
        // ...and for the required time
        // ...then stop it

        // Reset the values for next time
        time = 0;
        speed = 0;
  • thanks for the insight mark smith... but I wanted it to do like this (code below) how to I enter the speed first, then enter the timing second... after that... pressed # for the motor to move to the desired speed according to the timing... how do I combine the speed entry and time entry...
    – rjadkins
    Commented Mar 4, 2017 at 13:30
  • Could you edit your question to state the actual question, please? Also please include code to set the speed, if you have it, because I don't know how to control your motor. I'll try to help with getting input.
    – Mark Smith
    Commented Mar 4, 2017 at 14:09
  • i have change it mr. Mark... i hope you can really help me with this.
    – rjadkins
    Commented Mar 4, 2017 at 15:47
  • Answer updated.
    – Mark Smith
    Commented Mar 5, 2017 at 11:04
  • Mr Mark... i have tried your code... I enter speed of 123 then pressed #... after that I enter 10 then pressed *... after entering all the entries... the motor suppose to spin according to the time given...however nothing seem to happen... your code seems right tho...does pressing * after to time entry suppose to activate the motor??
    – rjadkins
    Commented Mar 5, 2017 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.