I have been using delay() as the function for my motor spinning time. However, at the same time, I need to calculate the rpm of my motor. Delay() function have to make it impossible for me to get the rpm whilst the motor spin. I have learned that I needed to use Elapsed millis as a replacement for delay() for my timing.
I have tried it and to no avail, I did not get the result in wanted. Yes, the motor spin according to the speed that I desired, however, it does not spin according to the time I wanted. It kept on spinning non-stop till I have to disconnect the power supply.
So, my question is, how do I implement the elapsed millis as my timer for my spinning motor? please, guys, help me with this... Just guide me through and give me guidance.
This is my code that I have tried using elapsed millis. Any suggestion is welcome.
#include <elapsedMillis.h>
#include <Keypad.h>
#include <Wire.h> // Comes with Arduino IDE
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
Servo myservo;
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
elapsedMillis timeElapsed;
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] =
{
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {9,8,7,6}; //row pinouts of the keypad (L1, L2, L3, L4)
byte colPins[COLS] = {5,4,3}; //column pinouts of the keypad (R1, R2, R3)
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
Serial.begin(9600);
lcd.begin(20,4);
myservo.attach(11);
lcd.setCursor(0,0);
lcd.print("S=");
lcd.setCursor(0,1);
lcd.print("T=");
lcd.setCursor(0,2);
lcd.print("S=");
lcd.setCursor(0,3);
lcd.print("T=");
lcd.setCursor(10,0);
lcd.print("S=");
lcd.setCursor(10,1);
lcd.print("T=");
lcd.setCursor(10,2);
lcd.print("RPM");
}
void loop()
{
int stage1speed = getTheNumber();
lcd.setCursor(2,0);
lcd.print(stage1speed);
lcd.print("sv");
int stage1time = getTheNumber();
lcd.setCursor(2,1);
lcd.print(stage1time);
lcd.print("sec");
int stage2speed = getTheNumber();
lcd.setCursor(2,2);
lcd.print(stage2speed);
lcd.print("sv");
int stage2time = getTheNumber();
lcd.setCursor(2,3);
lcd.print(stage2time);
lcd.print("sec");
int stage3speed = getTheNumber();
lcd.setCursor(12,0);
lcd.print(stage3speed);
lcd.print("sv");
int stage3time = getTheNumber();
lcd.setCursor(12,1);
lcd.print(stage3time);
lcd.print("sec");
if ( stage1speed > 0 && stage1time > 0 && stage2speed > 0 && stage2time > 0 && stage3speed > 0
&& stage3time > 0 )
{
if(timeElapsed <= 1000*stage1time)
{
myservo.write(stage1speed);
}
if(timeElapsed <= 1000*stage2time)
{
myservo.write(stage2speed);
}
if(timeElapsed <= 1000*stage3time)
{
myservo.write(stage3speed);
}
}
}
int getTheNumber()
{
char buffer[4];
// 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 && i > 0)
{
// Null terminate
buffer[i] =0;
int value = atoi(buffer); // Convert to an integer
break;
}
}
return atoi(buffer);
}