I am trying to write a program to control 4 motors through the Serial port. I planned on achieving this by sending the motors' speed and direction instructions as a string, which is then converted into numerical values that represent speed and direction. Unfortunately, the conversion process is not working and my motors will not spin.

int motorGroup1_Direction = 13;
int motorGroup1_Speed = 12;

int motorGroup2_Direction = 11;
int motorGroup2_Speed = 10;

int motorGroup3_Direction = 9;
int motorGroup3_Speed = 8;

int motorGroup4_Direction = 7;
int motorGroup4_Speed = 6;

String command; //First digit is for direction, the next 3 digits are for speed. A space acts a delimiter, repeat for the
//For the next three motors
char hold;
char letter;
char send[8];

void setup() {
  pinMode(motorGroup1_Direction, OUTPUT);

  pinMode(motorGroup1_Speed, OUTPUT);

  pinMode(motorGroup2_Direction, OUTPUT);
  pinMode(motorGroup2_Speed, OUTPUT);

  pinMode(motorGroup3_Direction, OUTPUT);
  pinMode(motorGroup3_Speed, OUTPUT);

  pinMode(motorGroup4_Direction, OUTPUT);
  pinMode(motorGroup4_Speed, OUTPUT);

void loop() {
  int trackParse = 0;
  int trackString = 0;

  while (Serial.available() > 0) {
    hold = Serial.read();

    if (hold == ' ') {

      send[trackParse] = (char)command.toInt();
    else {
      command[trackString] = hold;

  digitalWrite(motorGroup1_Direction, send[0]);

  analogWrite(motorGroup1_Speed, send[1]);

  digitalWrite(motorGroup2_Direction, send[2]);

  analogWrite(motorGroup2_Speed, send[3]);

  digitalWrite(motorGroup3_Direction, send[4]);

  analogWrite(motorGroup3_Speed, send[5]);

  digitalWrite(motorGroup4_Direction, send[6]);

  analogWrite(motorGroup4_Speed, send[7]);

  • 1
    i am not really sure how you want the program to work ... this may help ... ascii.cl
    – jsotola
    Jan 24 '18 at 6:57
  • 1
    I'm voting to close this question as off-topic because it's a pure programming question, where Arduino is not really involved.
    – user31481
    Jan 24 '18 at 10:08
  • Need more information about what do you expect. Put some comment on your code to let others know what the program supposed to work. @LookAlterno I think this is still within the Specific questions about Arduino boards, code, and the Arduino IDE.
    – dhimaspw
    Feb 9 '18 at 1:31
  • @dhimaspw. Is problem is parsing a string, nothing Arduino specific. You can delete all mention to pins and motors and it is still the same problem.
    – user31481
    Feb 9 '18 at 9:27
  1. You try to convert the whole command string everytime a space character appears. This works good for the first occurence, but then the string will become longer and longer and the converted integer will be garbage. You should reset the string with command = "" everytime you have processed a number, or just using the current part of it with the substring member function.

  2. Currently you echo the converted integer with Serial.write(). With this, the pure byte code is send over the serial. Your terminal program will interpret the bytes as characters, so you may get human readable numbers. Try the function Serial.println(), which converts the number to a human readable form and prints a newline after it as a delimiter.

  3. You don't need to write to your outputs on every loop cycle. Especially the Serial prints will make the debugging more difficult. I think you should only write a new value to your outputs, when you received new values over Serial.

  4. Am I right, that your protocol looks something like this: "0 255 1 100 1 150 0 80" ?

  5. In your current code, if not the whole string is received in one loop cycle (maybe if there was some delay when sending from the computer) the rest of the string is than written to the first motor values. You can use a packet delimiter character like "\n" after a full packet. Then you can collect all received characters in your command string until you receive the "\n", which marks the end of the current package. Then you would process the packet as a whole.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.