Polulu Meastro and Spark

I would like to use a Polulu Meastro (http://www.pololu.com/product/1352/) for my project. I have copied the code I have working with my Arduino but I get errors when I try to upload it to my spark specifically about the SoftwareSerial.h library. I am very new to spark and would really appreciate any help with this. Attached is my code that currently works with my arduino but not my spark:

include <``SoftwareSerial.h>

const int DEFAULT_BAUD = 9600;
const int SERVO_CONTROLLER_RX_PIN = 11; // The SERVO CONTROLLER'S RX PIN.
const int SERVO_CONTROLLER_TX_PIN = 12; // The SERVO CONTROLLER'S TX PIN.

SoftwareSerial ServoController = SoftwareSerial(SERVO_CONTROLLER_RX_PIN, SERVO_CONTROLLER_TX_PIN);

void setup()
{
   ServoController.begin(DEFAULT_BAUD);
   delay(500);
}

void moveServo(int ServoChannel, int target)
{
   //656ms PWM pulse represents a servo angle of 0 degrees.
   //2000ms PWM pulse represents a servo angele of 180 degrees.
   //These values could vary based on the servo you use, check your servo's 
   //spec documentation and verify what PWM pulses are needed to move it.

   byte serialBytes[4]; //Create the byte array object that will hold the communication packet.

   target = (map(target, 0, 180, 656, 2000) * 4); //Map the target angle to the corresponding PWM pulse.

   serialBytes[0] = 0x84; // Command byte: Set Target.
   serialBytes[1] = ServoChannel; // First byte holds channel number.
   serialBytes[2] = target & 0x7F; // Second byte holds the lower 7 bits of target.
   serialBytes[3] = (target >> 7) & 0x7F; // Third byte holds the bits 7-13 of target.

   ServoController.write(serialBytes, sizeof(serialBytes)); //Write the byte array to the serial port.
}

void loop()
{

   moveServo(0, 77); //Move the servo on channel 0 to an angle of 77 degrees
   moveServo(1, 77); //Move the servo on channel 1 to an angle of 77 degrees

}

@Kevin3651, SoftwareSerial is used with single UART Arduino boards and it implements a software serial. The Spark has actual hardware serial ports available to the user. The first is Serial1 using the Rx/Tx pins on the Spark.

For your code to work, remove the SoftwareSerial.h include and this line:

SoftwareSerial ServoController = SoftwareSerial(SERVO_CONTROLLER_RX_PIN, SERVO_CONTROLLER_TX_PIN);

Then add this #define before setup():

#define ServoController  Serial1

That will cause the compile to “substitute” ServoController with Serial1 in the code. Don’t forget to connect your Meastro serial pins to the rx/tx pins on the Spark. Keep me posted on how it goes :smile:

2 Likes

What if I wanted to use the Serial2 option and use the D0 D1. how would i change it?

It did work though so thank you! I’m just curious.

@Kevin3651, unless you are compiling locally with the latest master branch of the core-firmware repo, you cannot use Serial2 yet. When it is released, you would most likely do an #include “Serial2.h” at the top and then change the ServoController define to:

#define ServoController  Serial2

:slight_smile:

The firmware has all been pushed the makefile doesn’t seem to be updated when I did a test in the morning.

1 Like