Micro 8 Servo contr. Arduino, code for a Dummy

High Guys,

I just started in the uC programming (now aged 48). Had some coding experince in school (you can figure out how long ago). So an old Dummy needs your help.

Aruino 2009
Micro serial controller 8 Servos
USB powering Arduino and controller, 5V Supply for seros (GND connected to both supply)
Jumper set for easy 3 byte transmission
Servo at connector 0

After figuring out to connect to digital port :blush: pins instead of the anlog ones (had same numbering on the screw boards) AND connecting RX/TX instead of RX/RX :exclamation:
The green light is blinking in the sequence of write signals to Serial. Removing the delay will trigger the red led. Will be someone so nice and change my code that I have a servo jumping beetween two position.
The servo centers and holds, uses up to 1A current when tried to move by hand
I know it can be done with loops and much more sofisticated, but for my better understanding please keep it as simple as possible, I want to start from a working example to write a better code- for my learning.
Simple standard 3 Byte transmission for the beginning required.
Tryed many different print comands with no sucess.(servopos+220 was my last attempt to get a yellow lamp- did not work either- so I know my transmitting is wrong)
And a little description would be very much appreciated.
The code I found here is a little to heavy for me at this time.

many thanks in advance
b.r.
Wolfgang

#include <NewSoftSerial.h>

NewSoftSerial mySerial(3,2); //name of connection, set RX Pin, set TX Pin

int servonr=0;
int servopos=127;

void setup()  
{

  // set the data rate for the NewSoftSerial port
  mySerial.begin(2400);
  

}

void loop()                     // run over and over again
{
delay(100);
      mySerial.print(0xAA,BYTE);
      mySerial.print(0,BYTE);
      mySerial.println(127,BYTE);
 servopos=servopos+220;
  delay(100);
   mySerial.print(0xAA,BYTE);
   mySerial.print(0,BYTE);
   mySerial.println(147,BYTE);
   servopos=servopos-220;
}

Hello there B.R.

At a glance I see two critical problems with your code, and one minor one.

First, the start-byte at the beginning of the 3 byte protocol should be 0xFF (=255), not 0xAA (=170).

Second, you’re using “mySerial.println” for the third byte of each servo command, when you should always be using “mySerial.print”. “println” adds two extra characters (a carriage return and a line feed) to the end of whatever bytes you’re sending, indicating the start of a new line. That’s fine when you’re sending human-readable text over a serial port, but it breaks the format of the three-byte protocol.

Finally, a byte can only hold the values 0-255, so that is the range of your possible servo positions. On an Arduino an int is a two-byte variable, so when you add 220 to servopos it does get up to 347, but if you were to send using mySerial.print(servopos,BYTE) you would only be sending the lower of the two bytes, 91 (347%256). I would recommend changing “int servopos” to “unsigned char servopos”. A char (for character) is a single-byte variable, which is all you want to send for the protocol.

Good luck, and welcome to the wonderful world of uC programming!

-Adam

Dear Adam,

many thanks for taking care about my problem, tried byte bevor - no luck.

Now I did not do any maths, no variables, just fix numbers with no luck,
still the same result as bevor.
I think I do something wrong with call of myserial…
many thanks
b.r.
Wolfgang

#include <NewSoftSerial.h>

NewSoftSerial mySerial(3,2); //name of connection, set RX Pin, set TX Pin

void setup()  
{
  // set the data rate for the NewSoftSerial port
  mySerial.begin(2400);
  delay(500);
  mySerial.print(0x65,BYTE); //Send one byte for baud detection
  delay(500);
}

void loop()                     // run over and over again
{
delay(500);
      mySerial.print(0xFF,BYTE); // 255
      mySerial.print(0x01,BYTE); // changed to servo 1
      mySerial.print(0x7F,BYTE); // position 127
 
  delay(500);
   mySerial.print(0xFF,BYTE); //255  
   mySerial.print(0x01,BYTE); //changed to servo 1
   mySerial.print(0x93,BYTE); // position 147
   
}

I would take out the line in your setup function that prints an 0x65 byte, again that’s going outside of the 3-byte protocol.

-Adam

High Adam,

many thanks, it now works even with this recognise byte.

How?
The USB was powering both boards, a second source the servos.
GND was all connected.
By simply changing the power supply to Servo 5V supply, the servo started moving.
A light grumbling and relief on the other side of the ocean!

many thanks again
b.r.
Wolfgang

High Guys,
it´s me again,
this code now works perfectly with baud rate at 2400.

If I change to 9600, which the book says it can do, I get the red led on the servo controller flashing.
If I set a delay of about 10ms beetween the commands, the servo runs jerky and misfiring some times, but the red LED does not come on.
What am I doing wrong here again?
Pin to be set Input/ output (high/low)?
Thanks in advance,
b.r.
Wolfgang

#include <NewSoftSerial.h>

NewSoftSerial mySerial(3,2); //name of connection, set RX Pin, set TX Pin


void setup()  
{

  // set the data rate for the NewSoftSerial port
  mySerial.begin(9600);

}

void loop()                     // run over and over again
{
delay(300);
      mySerial.print(255,BYTE);
      mySerial.print(1,BYTE);
      mySerial.print(127,BYTE);

  delay(300);
   mySerial.print(255,BYTE);
   mySerial.print(1,BYTE);
   mySerial.print(200,BYTE);
  }

High Guys,

this is now a Servo resolution checker for all 8 Servos with Serial monitor for PC. Moving the servos slowly it shows if the Servo follows accordingly to the 256 Steps. By checking two different Servos at the same time, you will notice the difference in quality!
And it should help to check the board, wiring and useage for other Beginners like me.

b.r.
Wolfgang

#include <NewSoftSerial.h>

NewSoftSerial mySerial(3,2); //name of connection, set RX Pin, set TX Pin

#define sstart 255

void setup()  
{
  mySerial.begin(2400); //data rate Micro Servo controller
  Serial.begin(9600); // data rate Serial monitor PC
}

void loop()                     // run
{
for (int i=0; i<254;i=i++) {  // Servopos 0 to 255
for (int j=0; j<8; j++){  // Servonr 0 to 7
      mySerial.print(sstart,BYTE); //send startbyte 255
      mySerial.print(j,BYTE); // send servonumber j
      mySerial.print(i,BYTE); // send servoposition i
      Serial.println(i);   // send servoposition serial monitor PC
}
}

for (int i=255; i>1;i=i--) {  // Servopos 255 to 0
for (int j=0; j<8; j++){  // Servonr 0 to 7
      mySerial.print(sstart,BYTE); //send startbyte 255
      mySerial.print(j,BYTE); // send servonumber j
      mySerial.print(i,BYTE); // send servoposition i
      Serial.println(i);  // send servoposition serial monitor PC
}
}
}