guys, servo value is 1-180. I'm using servo library to control a bldc motor. i need to use a keypad to key in the servo value, then the value is relayed to the ESC then to the bldc motor. How do i program so that the keypad input is use to control the bldc. i read something about atoi, somehow i still don't quite understand. the program below shows how i control the bldc with servo library using the computer keyboard to enter the servo value, how do i enter the servo value using a 4x4 matric keypad?? Please help..... i'm new to programming...
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(A0,A1,A2,A3,A4,A5);
// read the hall effect sensor on pin 2
const int hallPin=2;
const unsigned long sampleTime=1000;
const int maxRPM = 9000;
#include <Servo.h>
Servo esc;
int escPin = 11;
int minPulseRate = 700;
int maxPulseRate = 2000;
int throttleChangeDelay = 100;
void setup() {
Serial.begin(9600);
Serial.setTimeout(500);
// Attach the the servo to the correct pin and set the pulse range
esc.attach(escPin, minPulseRate, maxPulseRate);
// Write a minimum value (most ESCs require this correct startup)
esc.write(0);
pinMode(hallPin,INPUT);
lcd.begin(16, 2);
lcd.print("initializing");
delay(1000);
lcd.clear();
}
void loop() {
// Wait for some input
if (Serial.available() > 0) {
// Read the new throttle value
int throttle = normalizeThrottle( Serial.parseInt() );
// Print it out
Serial.print("Setting throttle to: ");
Serial.println(throttle);
// Change throttle to the new value
changeThrottle(throttle);
delay(100);
int rpm=getRPM();
lcd.clear();
displayRPM(rpm);
}
}
void changeThrottle(int throttle) {
// Read the current throttle value
int currentThrottle = readThrottle();
// Are we going up or down?
int step = 1;
if( throttle < currentThrottle )
step = -1;
// Slowly move to the new throttle value
while( currentThrottle != throttle ) {
esc.write(currentThrottle + step);
currentThrottle = readThrottle();
delay(throttleChangeDelay);
}
}
int readThrottle() {
int throttle = esc.read();
Serial.print("Current throttle is: ");
Serial.println(throttle);
return throttle;
}
// Ensure the throttle value is between 0 - 180
int normalizeThrottle(int value) {
if( value < 0 )
return 0;
if( value > 180 )
return 180;
return value;
}
int getRPM()
{
// sample for sampleTime in millisecs
int kount=0;
boolean kflag=LOW;
unsigned long currentTime=0;
unsigned long startTime=millis();
while (currentTime<=sampleTime)
{
if (digitalRead(hallPin)==HIGH)
{
kflag=HIGH;
}
if (digitalRead(hallPin)==LOW && kflag==HIGH)
{
kount++;
kflag=LOW;
}
currentTime=millis()-startTime;
}
int kount2rpm = int(60000./float(sampleTime))*kount;
return kount2rpm;
}
void displayRPM(int rpm)
{
lcd.clear();
// set the cursor to column 0, line 1
lcd.setCursor(0, 0);
// print the number of seconds since reset:
lcd.print(rpm,DEC);
lcd.setCursor(7,0);
lcd.print("RPM");
}
void displayBar(int rpm)
{
int numOfBars=map(rpm,0,maxRPM,0,15);
lcd.setCursor(0,1);
if (rpm!=0)
{
for (int i=0; i<=numOfBars; i++)
{
lcd.setCursor(i,1);
lcd.write(1023);
}
}
}