Go Down

Topic: How to use SoftwareSerial library in creating a new library (Read 216 times) previous topic - next topic

samvrit

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:
Code: [Select]

#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");
}




pert

Do the include in your .h file and depending on which IDE version you are using you may also need to include it in your sketch. Then you can use the library just as you use it in the sketch. Give it a try and then post your library code if you run into problems.
.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy