I am trying to write a library to control a Servo DC motor. The motor uses UART communication to run. For example, if the motor has to rotate by 300 counts in the inbuilt optical encoder, the command "G300" has to be written into the UART buffer. I want to use SoftwareSerial library to connect and control the motor. I am using the tutorial in the arduino website, but it does not show how to include another library.
How do I include and use the SoftwareSerial library for my new library? Example(s) would be really helpful as I am not from a coding background.
The .ino file which I am using to convert to library is:
#include <SoftwareSerial.h>
int rx_pin = 9, tx_pin = 10;
SoftwareSerial motor(rx_pin,tx_pin); //RX,TX
int y;
void setup()
{
Serial.begin(9600);
motor.begin(9600);
motor.listen();
delay(2000);
motor.write("P0\r\n");
delay(1000);
}
void loop()
{
gotopos("200");
delay(2000);
gotoangle("60.76");
while(1);
}
void gotopos(String pos)
{
pos = "G"+pos;
char s[pos.length()+2];
pos.toCharArray(s,pos.length()+1);
Serial.write(s);
Serial.write("\r\n");
motor.write(s);
motor.write("\r\n");
}
void gotoangle(String angle)
{
float angle1 = angle.toFloat();
angle1 *= 5;
angle1 = ceil(angle1);
int angle2 = (int)angle1;
angle = "G"+String(angle2);
char s[angle.length()+2];
angle.toCharArray(s,angle.length()+1);
Serial.write(s);
Serial.write("\r\n");
motor.write(s);
motor.write("\r\n");
}