# 4x 3 matrix Keypad control speed motor

hey guys, im trouble to find project of using 4x3 keypad to control the speed of motor (pwm). anyone can share ? or give some idea how to map the keypad number for pwm. thanx in advanced ...

my project idea is to key in number from keypad ("1234") then use ("#") to enter the number and map it to pwm... im using dc motor as for my project..

Did you by any chance do a search in the forum for 4x3 keypad? Me and a few other people helped one guy with a keypad, and his code is somewhere in this forum. You just need to find it.

yeay, i did do my search on keypad prob....but no related coding for my prob... =( .....

You did a search in this forum and found nothing? Funny, because I found this. It's not 100% what you want to do but if you tweak it, it will work.

yeay, i had test it..thx hazard!!! i really appreciate it.... thx a lot man.... have a gud day!!!!>.. XD XD XD XD XD

``````/* @file HelloKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates the simplest use of the matrix Keypad library.
|| #
*/

int keyIn;
int currentCommand = 0;
long DatA =0;

char Data[5];
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] = {22, 24, 26, 28}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {30, 32, 34}; //connect to the column pinouts of the keypad

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

///////////encoder///////////////////
volatile byte rpmcount;
unsigned int rpm;
unsigned long timeold;

////////motor//////////////////
int cw = 7;
int pwm = 6;
int button = 8;
int LED = 9;
int but = 0;

void setup(){
Serial.begin(9600);
pinMode(cw,OUTPUT);
pinMode(pwm,OUTPUT);
pinMode(button,INPUT);
pinMode(LED,OUTPUT);

////////key in data for encoder ///////////
digitalWrite(2, HIGH);
attachInterrupt(0, rpm_fun, RISING);
rpmcount = 0;
rpm = 0;
timeold = 0;

}

void loop(){

if (but == HIGH)//clockwise
{
int key = keypad.getKey();

if(key != NO_KEY) // Do nothing if no key is pressed, incorporated from PaulS's example.
{
if (key != '*') {
Data[currentCommand++] = key;
Data = map(Data, 0 , 1400, 0, 255);
Serial.println(key);
keyIn = atoi(Data);

//this should print the first digit in it's same spot and the 2nd and 3rd will follow like normal, when inputted.

}

else {
//keyIn = atoi(Data);
//lcd.setCursor(6,1);
//lcd.print(keyIn);
//lcd.print("     ");
//just to tell you * was pressed
Serial.println(keyIn);
analogWrite(LED, keyIn);
while(currentCommand !=0){   // This can be used for any array size,
Data[currentCommand--] = 0; //clear for new data
}
}
}
}
else {
}
if(rpmcount >=20)
{//Update RPM every 20 counts, increase this for better RPM resolution,
//decrease for faster update
float rpm = float (14*60)/(millis() - timeold)*rpmcount;
timeold = millis();
rpmcount = 0;
Serial.println(rpm,4);

}
}
void rpm_fun()
{
rpmcount++;
//Each rotation, this interrupt function is run twice
}
``````

im trouble in mapping whereby there is error “invalid conversion from char to long int”… can anyone help me… thx in advanced

I fixed your error, but I can’t make heads or tails of what you are trying to do with the keypad data. I mean I KNOW what you want to do, but your code has no relation with the keypad data. How are you calculating the RPM?

“NewData” is the new mapped variable from the keypad data. Instead of mapping the data, why not just enter 0 - 255 and get rid of the map altogether?

``````/* @file HelloKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates the simplest use of the matrix Keypad library.
|| #
|| @Edited by HazardsMind  1/6/2013
*/

int keyIn;
int currentCommand = 0;

char Data[5];
long NewData=0;
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] = {22, 24, 26, 28}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {30, 32, 34}; //connect to the column pinouts of the keypad

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

///////////encoder///////////////////
volatile byte rpmcount;
unsigned int rpm;
unsigned long timeold;

////////motor//////////////////
int cw = 7;
int pwm = 6;
int button = 8;
int LED = 9;
int but = 0;

void setup(){
Serial.begin(9600);
pinMode(cw,OUTPUT);
pinMode(pwm,OUTPUT);
pinMode(button,INPUT);
pinMode(LED,OUTPUT);

////////key in data for encoder ///////////
digitalWrite(2, HIGH);
attachInterrupt(0, rpm_fun(), RISING);
rpmcount = 0;
rpm = 0;
timeold = 0;

}

void loop(){
if (but == HIGH)//clockwise
{
int key = keypad.getKey();
if(key != NO_KEY) // Do nothing if no key is pressed, incorporated from PaulS's example.
{
if (key != '*') {
Data[currentCommand++] = key;
Serial.println(key);
keyIn = atoi(Data);
/* */ NewData = map(keyIn, 0 , 1400, 0, 255); //Right here is the new line
//this should print the first digit in it's same spot and the 2nd and 3rd will follow like normal, when inputted.
}

else {
//keyIn = atoi(Data);
//lcd.setCursor(6,1);
//lcd.print(keyIn);
//lcd.print("     ");
//just to tell you * was pressed
Serial.println(keyIn);
analogWrite(LED, keyIn);
while(currentCommand !=0){   // This can be used for any array size,
Data[currentCommand--] = 0; //clear for new data
}
}
}
}
else {
}
if(rpmcount >=20)
{//Update RPM every 20 counts, increase this for better RPM resolution,
//decrease for faster update
float rpm = float (14*60)/(millis() - timeold)*rpmcount;
timeold = millis();
rpmcount = 0;
Serial.println(rpm,4);

}
}
void rpm_fun()
{
rpmcount++;
//Each rotation, this interrupt function is run twice
}
``````

Tom@Jerry... i have the same project as yours and i'm having trouble to control the rpm with the pwm. Can you share your coding?? i would appreciate it