Kontrol Posisi Motor DC Input Keypad

Berikut adalah contoh kontrol posisi motor DC dengan input dari keypad. Untuk memasukan posisi maka harus menekan *. Setelah itu memasukan angka posisi lalu tekan #. Maka motor akan bergerak ke posisi yang diinginkan. Sayangnya, simulasi di tinkercad tidak bisa menjalankan simulasi ini mungkin terlalu berat sehingga motor nge-hang.

Klik link project tinkercad.

#include <Keypad.h>
/*=================
	Keypad Init
===================*/
const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad

//keymap defines the key pressed according to the row and columns just as appears on the keypad
char keymap[numRows][numCols]= 
{
{'1', '2', '3', 'A'}, 
{'4', '5', '6', 'B'}, 
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};

//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {11,12,A5,A4}; //Rows 0 to 3
byte colPins[numCols] = {A3,A2,A1,A0}; //Columns 0 to 3

//initializes an instance of the Keypad class
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
String inputString;
/*======================
	Motor Controll Init
========================*/
//motor directory
#define CW 	0
#define CCW 1

// motor control pin
#define 	motorDirPin  7
#define 	motorPWMPin  9
#define 	EnablePin 	 8

// encoder pin
#define 	encoderPinA  2
#define 	encoderPinB  4

//encoder var
int	encoderPos 			= 0;

// PID control
float 	Kp 				= 3.2;
int 	targetPos		= 0;
int 	error;
int 	control;
int 	velocity;

void doEncoderA()
{  	
  digitalRead(encoderPinB)?encoderPos--:encoderPos++;
}

void setup()
{  
  //setup interrupt
  pinMode(encoderPinA, INPUT_PULLUP);
  pinMode(encoderPinB, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(encoderPinA), doEncoderA, RISING);
 
  //setup motor driver
  pinMode		(motorDirPin, 	OUTPUT);
  pinMode		(EnablePin, 	OUTPUT);
  digitalWrite	(EnablePin, 	HIGH);
   
  Serial.begin(9600);
}

void loop()
{  	
    error 		= targetPos - encoderPos;
  	control 	= (Kp*error);
  
    velocity = min(max(control,-255), 255);
  	if(velocity>=0)
    {
        digitalWrite(motorDirPin, CW);
  		analogWrite(motorPWMPin, velocity);
  	}
  	else 
  	{
        digitalWrite(motorDirPin, CCW);  		
  		analogWrite(motorPWMPin,(255+velocity));
  	} 
  	Serial.println(encoderPos);
  	delay(10); //time delay untuk kontrol motor
  
  	char keypressed = myKeypad.getKey();
	if (keypressed == '*')
  	{
      	Serial.print("Masukan Input Motor: ");
      	while(1) //loop selama belum enter #
        {
        	char keypressed = myKeypad.getKey();
          	delay(100); //anti bounching keypad delay
          	if (keypressed >= '0' && keypressed <= '9') // only act on numeric keys
            {   
              	Serial.print(keypressed);
      			inputString += keypressed;       // append new character to input string
    		}
          	else if (keypressed == '#') break;
        }
        targetPos = inputString.toInt(); // YOU GOT AN INTEGER NUMBER
        inputString = "";                // clear input
		Serial.println("");
      	Serial.print("Berputar ke posisi ");
      	Serial.println(targetPos);
	}
}