I would like to use the Motorshield to turn the arduino into a simple servo controller that can recieve serial commands from RoboRealm and move the servos.
Robo realm can output the Mini-Serial-Servo-Controller (Mini-SSC) protocol which looks really simple, it sends 3 bytes, 255 (Start Bit?) followed by the servo you want to control, then the angle you want it to go to.
I dont have any of my hardware here, so this is so far theoretical but does this code look like it'll accept the 3 bytes and act upon them?
thanks!
#include <ServoTimer1.h>
ServoTimer1 servoA;
ServoTimer1 servoB;
void setup() {
serial.begin(9600);
servoA.attach(9);
servoB.attach(10);
}
void loop() {
if (Serial.available()) {
byte c = Serial.read();
if (c == '255') {
byte Servo = Serial.read();
byte Angle = Serial.read();
if ( (Servo > 0) && (Servo < 3)) {
ServoMove(Servo, Angle);
}
}
}
void ServoMove(byte S, byte A) {
if (S == 1) {
ServoA.write(A);
}
else
if (S == 2) {
ServoB.write(A);
}
}
You may want to reset a counter every time you receive a 255, and increase the count for every byte that isn't a 255. Then once you have received 3 bytes, go on to the servo writing function. This will make your code more failsafe in case you lose a byte here or there.
The way you have it right now, your code will try to read all three bytes as soon as it reads the first byte. This is bad because you will be trying to read the bytes faster than they arrive, and you'll just get -1 for the following two bytes.
So I would (and I've actually used the Arduino as a Mini-SSC emulator before) change the code like so:
Your Code:
if (Serial.available()) {
byte c = Serial.read();
if (c == '255') {
byte Servo = Serial.read();
byte Angle = Serial.read();
if ( (Servo > 0) && (Servo < 3)) {
ServoMove(Servo, Angle);
}
}
Serial.available would let you check if there were three bytes, yes...but you have no way of knowing if those three bytes start with 255, or if you missed a byte somewhere and are out of sync.
Yes, by resetting a byte count every time I saw a 255, I was able to get very reliable control with the Mini-SSC protocol.
Thanks for your help so far! It seems to be working in as much as i get three bytes, and only if the first byte is 255! however when i get it to echo the Servo and Angle bytes back i get a 255 everytime. Am i doing something wrong here?
byte Servo = 0;
byte c = 0;
byte Angle = 0;
int SerialCount = 99;
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
c = Serial.read();
if (c == 255) {
SerialCount = 0;
}
else if ((c != 255) && (SerialCount == 0)) {
Servo = Serial.read();
SerialCount++;
}
else if (c != 255 && SerialCount == 1) {
Angle = Serial.read();
SerialCount++;
A mistake on my part when I changed your code. It should be "byte Servo = c;" and "byte Angle = c;" since we've already read that byte to see if it's 255 or not.
ok, so what its doing is actually loading the serial data into c everytime, then deciding what to do with it depending on whether serialcount is 0 or 1.