1

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] =
 {{'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
  };
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()
{
  Serial.begin(9600);

  lcd.begin(16,2);
  lcd.setCursor(0,0);
  lcd.print("S:");
  lcd.setCursor (0,1);
  lcd.print ("T:");

  myservo.attach(11);
}

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;
        ++i;
        buffer[i] =0;  // null-terminate buffer
        lcd.setCursor (2,0);
        lcd.print(buffer);
    }


     if  ('0' <= key && key <= '9') // this is for timing entry
     {
        spintime[a] = key;
        ++a;
        spintime[a] =0;  // null-terminate buffer
        lcd.setCursor (2,1);
        lcd.print(spintime);
    }
    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
        myservo.write(0);  
        i = 0; 
    }
}
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;
            i++;
        }
        // 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; 
            break;
        }    
    }

    // If we have both speed and time...
    if (speed > 0 && time > 0)
    {
        // ...spin the motor at the required speed
        myservo.write(speed);
        // ...and for the required time
        delay(time*1000);
        // ...then stop it
        myservo.write(0);

        // 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 Mar 4 '17 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 Mar 4 '17 at 14:09
  • i have change it mr. Mark... i hope you can really help me with this. – rjadkins Mar 4 '17 at 15:47
  • Answer updated. – Mark Smith Mar 5 '17 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 Mar 5 '17 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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