#define speed 50
#define charLenth 6
#define xyLenth charLenth/2
#define roundPerCentimeter 1
char xCordinate[xyLenth];
char yCordinate[xyLenth];
int actualXLenth;
int actualYLenth;
int xValue1;
int xValue2;
int yValue1;
int yValue2;
int xNumRound; // how many rotate motor
int yNumRound; // how many rotate motor
int i;
int j = 0;
int k = 0;
char cordinate[charLenth];
int drillHeightRound =3;
int points = 0;
int fwdsteps = 0;
int bwdsteps = 0;
void xStepperForward(int xRoundForwrd){
for(i=1;i<xRoundForwrd+1;i++){
portd = 1;
delay_ms(speed);
portd = 2;
delay_ms(speed);
portd = 4;
delay_ms(speed);
portd = 8;
delay_ms(speed);
portd = 1;
}
}
void xStepperBackward(int xRoundBackwrd){
for(i=1;i<xRoundBackwrd+1;i++){
portd = 8;
delay_ms(speed);
portd = 4;
delay_ms(speed);
portd = 2;
delay_ms(speed);
portd = 1;
delay_ms(speed);
portd = 8;
}
}
void yStepperForward(int yRoundForwrd1){
for(i=1;i<yRoundForwrd1+1;i++){
portd = 16;
delay_ms(speed);
portd = 32;
delay_ms(speed);
portd = 64;
delay_ms(speed);
portd = 128;
delay_ms(speed);
portd = 16;
}
}
void yStepperBackward(int yRoundBackwrd){
for(i=1;i<yRoundBackwrd+1;i++){
portd = 128;
delay_ms(speed);
portd = 64;
delay_ms(speed);
portd = 32;
delay_ms(speed);
portd = 16;
delay_ms(speed);
portd = 128;
}
}
void drillStepperForward(){
for(i=0;i<drillHeightRound;i++){
portb = 1;
delay_ms(speed);
portb = 2;
delay_ms(speed);
portb = 4;
delay_ms(speed);
portb = 8;
delay_ms(speed);
portb = 1;
}
}
void drillStepperBackward(){
for(i=0;i<drillHeightRound;i++){
portb = 8;
delay_ms(speed);
portb = 4;
delay_ms(speed);
portb = 2;
delay_ms(speed);
portb = 1;
delay_ms(speed);
portb = 8;
}
}
void Read_Serial(){
Delay_ms(100); // Wait for UART module to stabilize
UART1_Write_Text("Enter X cordinate and Y cordinate : ");
while (1) { // Endless loop
for(i=0;i<charLenth;)
{
if(UART1_Data_Ready()) // if data ready
{
cordinate[i] = UART1_Read();
// read data
i++;
}
}
break;
}
}
void main() {
ADCON1 = 0x06; // To turn off analog to digital converters
UART1_Init(9600); // Initialize UART module at 9600 bps
TRISC=0x81; //input for RX Serial input
PORTC=0x00;
TRISD=0x00; // Initialize motor X and y
PORTD=0x00;
TRISB=0x00; // Initialize motor drill
PORTB=0x00;
TRISA=0x00;
PORTA = 0x00;
Read_Serial();
Delay_ms(100);
points = sizeof(xCordinate)/sizeof(xCordinate[0]);
for(i = 0; i < points; i++){
if(i == 0)
{
xNumRound = roundPerCentimeter*xCordinate[i] - '0';
yNumRound = roundPerCentimeter*yCordinate[i] - '0';
xStepperForward(xNumRound);
Delay_ms(100);
// stepper forword
yStepperForward(yNumRound);
Delay_ms(100);
}else{
//x logic
if(xCordinate[i - 1] < xCordinate[i]){
//forward
fwdsteps = xCordinate[i] - xCordinate[i-1];
xStepperForward(roundPerCentimeter*fwdsteps - '0');
}else{
//backward
bwdsteps = xCordinate[i-1] - xCordinate[i];
xStepperBackward(roundPerCentimeter*bwdsteps - '0');
}
}
}
}